jQuery.fn.initpage = function () {

// BIND CLICKS ON MENU
$('#menu a').bind({
	click: function(event) {
	event.preventDefault();
	$(this).parent('li').siblings().removeClass('current');
	//$(this).parent('li').toggleClass('current');
	Cufon.refresh('#menu li');
	$(this).loadpage(event);
	}
});

// BIND CLICKS ON HEADER
$('#header a').bind({
	click: function(event) {
	event.preventDefault();
	$(this).loadpage(event);
	}
});

// REGISTRATION
$('#register').click(function () {
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#login-col').fadeOut("fast");
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#register-col').fadeIn("slow");
	}});
});

// IE FIX
var ver = -1;
if (navigator.appName == 'Microsoft Internet Explorer') {
	var ua = navigator.userAgent;
	var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua) != null) { ver = parseFloat(RegExp.$1); }
}
var visibility = 'hidden';
if (ver != -1) {
	if (ver >= 9.0) { visibility = 'hidden'; } else { visibility = 'visible'; }
}

// MENU LOCATION
var menuPos=document.URL.split('xMenu=')[1];

// A-SECTION BEHAVIOR
if (menuPos=='a'){
// HANDLE PAGE WITH C-LINKS
if ($('.c-section-list li').length > 1){

	$('.b-section-list').css('width', 'auto');
	var bmenucols = Math.ceil($('.b-section-list li').length / 6 );
	var bmenuwidth = $('.b-section-list').outerWidth() * bmenucols ;
	
	$('.b-section-list').width(0);
	$('.b-section-list ul').children('li').css('visibility', visibility);
	
	$('.c-section-list').css('width', 'auto');
	var cmenucols = Math.ceil($('.c-section-list li').length / 6 );
	var cmenuwidth = $('.c-section-list').outerWidth() * cmenucols ;
	
	$('.c-section-list').width(0);
	$('.c-section-list ul').children('li').css('visibility', visibility);
	$('.c-section-list ul').easyListSplitter({ colNumber: cmenucols });
	
	$("#1").animate({margin: 0}, { preDelay: 600, queue: "global", complete: function () {
		$('.b-section-list').animate({width: bmenuwidth });
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('.c-section-list').animate({width: cmenuwidth });
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('.b-section-list ul').children('li').css('visibility','visible').hide().fadeIn('fast');
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('.c-section-list ul').children('li').css('visibility','visible').hide().fadeIn('fast');
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#content').children('div').hide().fadeIn('slow');
	}});

// HANDLE PAGE WITH B-LINKS
} else if ($('.b-section-list li').length > 1){

	$('.b-section-list').css('width', 'auto');
	var menucols = Math.ceil($('.b-section-list li').length / 6 );
	var menuwidth = $('.b-section-list').outerWidth() * menucols ;
	
	$('.b-section-list').width(0);
	$('.b-section-list ul').children('li').css('visibility', visibility);
	$('.b-section-list ul').easyListSplitter({ colNumber: menucols });
	
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('.b-section-list').animate({width: menuwidth });
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('.b-section-list ul').children('li').css('visibility','visible').hide().fadeIn('fast');
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#content').children('div').hide().fadeIn('slow');
	}});

// HANDLE PAGE WITH A-LINKS
} else {

	$('#content').children('div').hide().fadeIn('slow');
}
}

// B-SECTION BEHAVIOR
if (menuPos=='b'){
// HANDLE PAGE WITH C-LINKS
if ($('.c-section-list li').length > 1){

	var bmenucols = Math.ceil($('.b-section-list li').length / 6 );
	var bmenuwidth = $('.b-section-list').outerWidth() * bmenucols ;
	
	$('.b-section-list').width(bmenuwidth);
	$('.b-section-list ul').easyListSplitter({ colNumber: bmenucols });
	
	$('.c-section-list').css('width', 'auto');
	var cmenucols = Math.ceil($('.c-section-list li').length / 6 );
	var cmenuwidth = $('.c-section-list').outerWidth() * cmenucols ;

	$('.c-section-list').width(0);
	$('.c-section-list ul').children('li').css('visibility', visibility);
	$('.c-section-list ul').easyListSplitter({ colNumber: cmenucols });
	
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('.c-section-list').animate({width: cmenuwidth });
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('.c-section-list ul').children('li').css('visibility','visible').hide().fadeIn('fast');
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#content').children('div').hide().fadeIn('slow');
	}});

// HANDLE PAGE WITH B-LINKS
} else if ($('.b-section-list li').length > 1){

	var menucols = Math.ceil($('.b-section-list li').length / 6 );
	var menuwidth = $('.b-section-list').outerWidth() * menucols ;

	$('.b-section-list').width(menuwidth);
	$('.b-section-list ul').easyListSplitter({ colNumber: menucols });
	
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#content').children('div').hide().fadeIn('slow');
	}});

// HANDLE PAGE WITH A-LINKS
} else {

	$('#content').children('div').hide().fadeIn('slow');
}
}

// C-SECTION BEHAVIOR
if (menuPos=='c'){

	var bmenucols = Math.ceil($('.b-section-list li').length / 6 );
	var bmenuwidth = $('.b-section-list').outerWidth() * bmenucols ;
	
	$('.b-section-list').width(bmenuwidth);
	$('.b-section-list ul').easyListSplitter({ colNumber: bmenucols });
	
	var cmenucols = Math.ceil($('.c-section-list li').length / 6 );
	var cmenuwidth = $('.c-section-list').outerWidth() * cmenucols ;
	
	$('.c-section-list').width(cmenuwidth);
	$('.c-section-list ul').easyListSplitter({ colNumber: cmenucols });
	
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#content').children('div').hide().fadeIn('slow');
	}});
}

if (menuPos==undefined){$('#content').children('div').hide().fadeIn('slow');}

};

jQuery.fn.loadpage = function(event) {

// PAGE URL LOACTION
var asec = $(this).closest('.a-section-list').attr('class');
var bsec = $(this).closest('.b-section-list').attr('class');
var csec = $(this).closest('.c-section-list').attr('class');

var menu;
if (asec=='a-section-list') {menu='&xMenu=a';};
if (bsec=='b-section-list') {menu='&xMenu=b';};
if (csec=='c-section-list') {menu='&xMenu=c';};

if (menu==undefined) {
var link=$(this).attr('href');
}else{
var link=$(this).attr('href')+menu;
}
var section = link.replace(/[^\d]/g, '');
var rand = Math.floor(Math.random()*4);


// TEST FOR IE
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		rv = parseFloat( RegExp.$1 );
}

if ( rv == 9.0 ) {
    document.location = link;
	return;
}

// REPLACE BACKGROUND IMAGE
var bgimage = $(this).attr('name');
$('#background-bottom').attr('src',bgimage);

if ( $(this).closest('div').attr('id') == 'header' ) { 

	var link='http://qualitas.com.au/index.php';

	// FADE CONTENT
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('.b-section-list').fadeOut('fast');
		$('.c-section-list').fadeOut('fast');
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#content').children('div').fadeOut('fast');
	}});
	
	// CHECK IF IMAGE IS ALREADY LOADED
	if ( $('#background-top').attr('src') != $('#background-bottom').attr('src') ) { 
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#background-top').fadeOut('slow');
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		document.location = link;
	}});
	}
	// CHANGE QUOTE PICK
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#quotes').children('ul').children('li').hide();
		var quotepick = Math.floor(Math.random() * $('#quotes li').length) + 1;
		$("#quotes").children('ul').children('li:nth-child('+quotepick+')').toggle();
		$('#index-sec').fadeIn('slow');

	}});

	return;

};

if ( $(this).parent('li').parent('ul').parent('div').hasClass('a-section-list') ) {
	
	$('.b-section-list').fadeOut('fast');
	$('.c-section-list').fadeOut('fast');
	
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#content').children('div').fadeOut('fast');
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#background-top').fadeOut();
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		document.location = link;
	}});
	
	return;

} else if ( $(this).parent('li').parent('ul').parent('div').hasClass('b-section-list') ) {

	
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#content').children('div').fadeOut('fast');
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('.c-section-list').fadeOut();
	}});
	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		document.location = link;
	}});
	
	return;

} else {

	$("#1").animate({margin: 0}, { queue: "global", complete: function () {
		$('#content').children('div').fadeOut('fast');
	}});
	
	document.location = link;
	return;

}

};
