var openSpeed = 400;
var closeSpeed = 400;

var nAgt = navigator.userAgent;
var verOffset;
// In MSIE, the true version is after "MSIE" in userAgent
if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
 browserName = "Microsoft Internet Explorer";
 fullVersion = nAgt.substring(verOffset+5);
 fullVersion = fullVersion.substring(0,1);
 //tren ie 6  peformance thap nen toc do 400 khong hien thi tot
 if(fullVersion == 6){
	 closeSpeed = 400;
 }
  closeSpeed = 400;
}
var ajaxUrl = "";
var initMenuIsInteracted = false;
var isLoading = false;
var currentAnchor = document.location.hash;
var newAnchor;

$(document).ready(function(){
	document.title = "Strategis Partners";
	if(currentAnchor){
		processPastedLink();
	}
	
	//http://strategispartners.com.au/#/index.php?/eng/Intro
	var wloc = window.location+"";
	//document.title = wloc;
	if(wloc.indexOf("#/index.php?/eng/Intro") != -1 || wloc.indexOf("#") == -1){
		$("#introFlash").show();
	}else{
		$("#introFlash").hide();
	}
	
	$("#mainmenu_strategis").parent().mouseover(function(){
		$("#mainmenu_strategis").css({
			"background-position": "204px 0px"
		});
	});
	
	$(".dropdownmenu").each(function(){
		var $this = $(this);
		$this.mouseover(function(){
			var $$this = $(this);
			$$this.parent().children("a").css({
				//"background-position": "137px 0px"
			});
		});
		$this.mouseout(function(){
			var $$this = $(this);
			$$this.parent().children("a").css({
				//"background-position": "0px 0px"
			});
		});
		$this.parent().children("a").mouseover(function(){
			var $$this = $(this);
			$$this.parent().children("a").css({
				//"background-position": "137px 0px"
			});
		});
		$this.parent().children("a").mouseout(function(){
			var $$this = $(this);
			$$this.parent().children("a").css({
				//"background-position": "0px 0px"
			});
		});
	});
	
	$("#mainmenu_strategis").parent().mouseout(function(){
		$("#mainmenu_strategis").css({
			"background-position": "0px 0px"
		});
	});
	
	window.onresize = resize;	
	resize();
	
	//use visibility instead of display, to avoid image blinking
	$("#mainMenu").css("visibility","visible");
	hideLoader();
	$("#loaderImg").css("display","block");
	$("#mainMenu li ul").css("display","block"); 
	$("#mainMenu li ul").css("visibility","hidden");
	
	$("a").hover(function(event){
						  event.preventDefault();
						  window.status="Strategis Partners";
//						  alert('a');
						  });
   	$("#mainMenu a").click(function(event){
								   loaderSwitch = "main";
								   event.preventDefault();
								  if(isLoading)
									   return;
									$("#mainMenu a.active").removeClass("active");
									$(this).addClass("active");
									processAnchor($(this),1);
									$("#introFlash").animate({
												"height": "0px"
											}, function(){
												$("#mainMenu").mouseover();
											});
								   }
	);//end #mainMenu a.click
   	$(".jslidemore").click(function(event){
											event.preventDefault();
											loaderSwitch = "main";
											if(isLoading)
												return;
											$("#introFlash").animate({
												"height": "0px"
											}, function(){
												$("#mainMenu").mouseover();
											});
											processAnchor($(this),1);
										}
	);
   $("#mainMenu #mainmenu_strategis").unbind("click");
										$("#mainMenu #mainmenu_strategis").click(function(event){
										event.preventDefault();
										window.location = domain;
										});
    $("#mainMenu").mouseenter(function(event){
	 if(initMenuIsInteracted == true) return;
	 initMenuIsInteracted = true;
	 $("#mainMenu #mainmenu_strategis").unbind("click");
	 $("#mainMenu #mainmenu_strategis").click(function(event){
										event.preventDefault();
										});
	 $('#mainMenu').fadeTo('slow', 1);
	 $("#coverMenu").animate({"top": "-90px"}, "slow", function(){
										if(isLoading){
										return;
										}
										$("#mainMenu a.active").removeClass("active");
										$("#mainmenu_strategis").addClass("active");	
										$("#mainmenu_strategis").css("background-position","204px 0");
										
										});	 
										$("#mainMenu #mainmenu_strategis").unbind("click");
										$("#mainMenu #mainmenu_strategis").click(function(event){
										event.preventDefault();
										window.location = domain;
										});
										
	});
	
	$("#mainMenu li").hover(
        function () {
		//$(this).children("ul").show();
		$(this).children("ul").css("visibility","visible"); 
        },function(){
		//$(this).children("ul").hide();
		$(this).children("ul").css("visibility","hidden"); 
	});//hover
	$("#mainMenu li").click(
			function () {
			//$(this).children("ul").hide();
			$(this).children("ul").css("visibility","hidden"); 
			});

 });//end document.ready

originalClientWidth = 0;
function resize(){
		if(document.body.clientWidth == originalClientWidth)
		return;
		if(document.body.clientWidth == originalClientWidth+16)//when scroller appear on, IE think window is resized, IE6,7 scroller width  = 16, IE 8 scroller width = 17
		return;
		if(document.body.clientWidth == originalClientWidth-16)
		return;
		if(document.body.clientWidth == originalClientWidth+17)//when scroller appear on, IE think window is resized, scroller width  = 16
		return;
		if(document.body.clientWidth == originalClientWidth-17)
		return;
		originalClientWidth = document.body.clientWidth;
		menuWidth = document.getElementById("mainMenu").offsetWidth;
		if(document.body.clientWidth > menuWidth){
		leftMargin = (document.body.clientWidth - menuWidth + 10) / 2;
		}
		else{
		leftMargin = 10;
		}
		$("#mainMenu").css("margin-left",leftMargin+"px");
		$("#copyright").css("margin-left",(leftMargin + 400)+"px");
		$("#loaderImg").css("left",leftMargin+"px");
		$("#content").css("margin-left",leftMargin+"px");	
		$("#coverMenu").css("margin-left",leftMargin+"px");
		$("#introFlash").css("margin-left",leftMargin+"px");	
}

function reloadPanels(panel1ID, panel1Content, panel2ID, panel2Content){		  		

				   
			$("#"+panel1ID).html(panel1Content);
			$("#"+panel2ID).html(panel2Content);
			//$("#tempLeftContent").html(panel1Content);
			//h = document.getElementById("tempLeftContent").offsetHeight;
			h = document.getElementById("db_rightContent").offsetHeight;
			$("#"+panel1ID).animate({"height":  h+"px"}, 1000,function(){						
						//$("#tempLeftContent").html(panel2Content);
						//h = document.getElementById("tempLeftContent").offsetHeight;
						h = document.getElementById("tempFullContent").offsetHeight;
						$("#"+panel2ID).animate({"height":  h+"px"}, 1000,function(){
										$("#introFlash").fadeOut(1000,function(){
											$("#introFlash").html("");		
										});					
				});//leftContent.animate.subFunction
});	
}
function reloadPanels2(panel1ID, panel1Content, panel2ID, panel2Content)
{
			

}
/***** SUB PAGES *****/
function subpage_click(anchorObject){
	//loaderSwitch = "sub";
	id = anchorObject.id;
	$("#subMenuOption2 li a.active").removeClass("active");
	$("#subMenu li a.active").removeClass("active");
	$(".subMenu li a.active").removeClass("active");
	$("#"+id).addClass("active");
	
	$("#"+id).click(function(event){
							 event.preventDefault();
							 });
	processAnchor($("#"+id),2);
}

function activeSlider(){
//size =	$("#featuredArticle ul > li").children(".slider").size();

$().ready(function(){
cnt =	$("div#rightContent div.rightContent div#featuredArticle ul.slider > li.slider").length;
if(cnt > 1){
$("#featuredArticle").easySlider({
				controlsBefore:	'<div id="controls">',
				controlsAfter:	'</div>',
				prevText: '&nbsp;',
				nextText: '&nbsp;',
				prevId: 'prevBtn',
				nextId: 'nextBtn'}
			);
}
cnt =	$("div#rightContent div.rightContent div#newsGroups ul.slider > li.slider").length;
if(cnt > 1){
$("#newsGroups").easySlider({
				controlsBefore:	'<div id="newsControls">',
				controlsAfter:	'</div>',
				prevText: 'newer',
				nextText: 'older',
				prevId: 'prevBtn2',
				nextId: 'nextBtn2'}
			);
}
});

}

ContactValidationFlag = 0;
function submitContactForm(){
	if(ContactValidationFlag == 0){
	validateForm("#contact_form");	
	ContactValidationFlag = 1;
	}	
	if($("#contact_form").valid())
	{
		$("#contactButton").unbind("click");
		$("#contactButton").attr("onclick","");
		$("#contactButton:hover").css("text-decoration","none");
		//$("#contactButton").animate({"width": "90px"}, "slow");
		$("#contactButton").html("Please wait...");
		$.post("contact.php", $("#contact_form").serialize(),function(data){
				$("#contactButton").css("width","490px");
				$("#contactButton").css("cursor","default");
				$("#contactButton").html(data);
				});
		}
}

SubscribeValidationFlag = 0;
function submitSubscribeForm(){
	if(SubscribeValidationFlag == 0){
	validateForm("#subscribe_form");	
	SubscribeValidationFlag = 1;
	}
	
	if($("#subscribe_form").valid())
	{
		$("#subscribeButton").unbind("click");
		$("#subscribeButton").attr("onclick","");
		$("#subscribe_form:hover").css("text-decoration","none");
		//$("#contactButton").animate({"width": "90px"}, "slow");
		$("#subscribeButton").html("Please wait...");
		$.post("subscribe.php", $("#subscribe_form").serialize(),function(data){
				$("#subscribeButton").css("width","290px");
				$("#subscribeButton").css("cursor","default");
				$("#subscribeButton").html("Thank you very much for your subscription.");
				});
		}
}

loaderImgSrc = "";
$(document).ready(function(){
loaderImgSrc = $("loaderImg").attr("src");
});

function hideLoader()
{
	$("#loaderImg").css("visibility","hidden");
}

function showLoader()
{
	$("#loaderImg").css("visibility","visible");
	//$("#loaderImg").attr("src",loaderImgSrc);
}

function activeSubscribeList(){ 
$(document).ready(function(){


$(".subscribeList a").click(function(event){
							event.preventDefault();
							cssClass = $(this).children(".checkbox").attr("class");
							//alert(class);
							if(cssClass == "checkbox"){
								$(this).children(".checkbox").attr("class","checkbox active");
							}
							else if(cssClass == "checkbox active"){
								$(this).children(".checkbox").attr("class","checkbox");
							}
							var title = $(this).children(".title").html();
									});

$("#requestPaperButton").click(function(event){
									event.preventDefault();
									validateForm("#publication_form");
									if(!$("#publication_form").valid())
									{
										return;
									}
									//$("#publication_form").validation();
									requests = new  Array();
									requests2 = new  Array();
									i=0;
									$("#group1.subscribeList' a").each(function(){
										cssClass = $(this).children(".checkbox").attr("class");
										if(cssClass == "checkbox active"){
											requests[i] = $(this).children(".title").html();
											i++;
										}
									});//each
									i=0;
									$("#group2.subscribeList' a").each(function(){
										cssClass = $(this).children(".checkbox").attr("class");
										if(cssClass == "checkbox active"){
											requests2[i] = $(this).children(".title").html();
											i++;
										}
									});//each
								if((requests.length+requests2.length)==0){
									$("#publication_form #notice").show(); 
									return;	
								}
								$("#publication_form #notice").hide(); 
								$("#requestPaperButton").html("Please wait...");
								$("#requestPaperButton").fadeIn("slow");
								$.post("publication.php", {'requests[]':requests,
															'requests2[]':requests2,
															'SendTo':$("#SendTo").attr("value"),
															'Name':$("#requestName").attr("value"),
															'Email':$("#requestEmail").attr("value")},
															function(data){
																$("#requestPaperButton").html("Your request was sent successful!");
																$("#requestPaperButton").fadeIn("slow");
																$("#requestPaperButton").css("height","30px");
																$("#requestPaperButton").css("cursor","default");
																$("#requestPaperButton").unbind("click");
																//  alert(data);
																  });
								});//requestPaperButton.click
});

// var fc = new FormCheck('contact_form');
}

function submitPublicationForm(){

}
function validateForm(formId){
	$(formId).validate({
														   
	showErrors: function(){
	
	this.defaultShowErrors();
	
	$(formId+" label.error").click(function(){								
	if($(this).prev("input")[0]){
	$(this).prev("input")[0].focus();
	}
	if($(this).prev("textarea")[0]){
	$(this).prev("textarea")[0].focus();
	}									
	$(this).fadeOut("fast");
	});
	
	$(formId+" input.error, "+formId+" textarea.error").focus(function(){
	$(this).next("label.error").fadeOut("fast");
	});
	
	},
	onkeyup: false,
	onclick: false,
	onfocusout: false
						   
						   }
						   
						   );
}

//aObj: $(this), $("anchorId")
//level: flag to process different anchor type, ex: main menu or submenu level1, level2
function processAnchor(aObj,level){
	newAnchor = aObj.attr("href");
	url = newAnchor;
	altUrl = url.replace(domain,"");
	altUrl = "#"+altUrl;
	document.location=altUrl;
	if(level == 1){
		loadMain();
		activeMainMenu();
	}
	else if(level == 2){
		loadSub();
		activeMainMenu();
	}
}

var pasteLinkIsProcessed = false;
function processPastedLink(){

	if(pasteLinkIsProcessed || initMenuIsInteracted){
		return;
	}
	//alert("the user pasted the link to new window!");
	$('#mainMenu').fadeTo(0, 1);
	$("#coverMenu").animate({"top": "-90px"}, 0, function(){
										});
	currentAnchor = "";
	pasteLinkIsProcessed = true;
	initMenuIsInteracted = true;
	
//	newAnchor = document.location.hash;
	newAnchor = document.location.href;//IE 6 remove url parametter of location.hash
	
	pos=newAnchor.indexOf("#");
	if((newAnchor != domain) && (pos >= 0) )
	{
		patt1=/#/gi;
		newAnchor = newAnchor.replace(patt1,"");//remove # to pass url to ajax load
	}
	loadMain();
	activeMainMenu();
}

function activeMainMenu(){
//active main menu
	currentFolders = new Array();
	currentFolders = newAnchor.split("/");
	currentFolder = currentFolders[currentFolders.length - 2];
	$("#mainMenu a.active").removeClass("active");
	$("#mainMenu li a").each(function(){
									    menuAnchor = $(this).attr("href");
										menuFolders = new Array();
										menuFolders = menuAnchor.split("/");
										menuFolder = menuFolders[menuFolders.length - 2];
										if(menuFolder == currentFolder){
										$(this).addClass("active");
										}
										});
}

function loadMain()
{
//Check if it has changes
//if(currentAnchor == document.location.hash)
if(currentAnchor == newAnchor)
{
	return;
}
//if(currentAnchor != document.location.hash)
if(currentAnchor != newAnchor)
{
	showLoader();
	currentAnchor = newAnchor;
	ajaxUrl  = currentAnchor;
	isLoading = true;
	//$.ajax({
	$.Tache.Get({
	url: ajaxUrl,
	cache: false,
	success: function(html){
		
		isLoading = false;
		hideLoader();
		//$("#copyright").css("display:none");
		$("#tempFullContent").html(html);
		$("#leftContent").animate({"height": "0px"}, closeSpeed);
		$("#rightContent").animate({"height": "0px"}, closeSpeed,function(){						   
			
		document.title = $("#meta_title").attr("value");

		$("#rightContent").html($("#db_rightContent").html());
		$("#leftContent").html($("#db_leftContent").html());
			h = document.getElementById("db_rightContent").offsetHeight;
			$("#rightContent").animate({"height":  h+"px"}, openSpeed,function(){
						h = document.getElementById("db_leftContent").offsetHeight;
						$("#leftContent").animate({"height":  h+"px"}, openSpeed,function(){
										$("#introFlash").fadeOut(1000,function(){
											$("#introFlash").html("");		
										});					
				});//leftContent.animate.subFunction
});	
		//$("#tempFullContent").html("");
		activeSlider();
		activeSubscribeList();
		});
	}
	//end tempLeftContent.html
	});
}
}

function loadSub()
{
//Check if it has changes
//if(currentAnchor == document.location.hash)
if(currentAnchor == newAnchor)
{
	return;
}
	currentAnchor = newAnchor;
	ajaxUrl  = currentAnchor;
	showLoader();			
		isLoading = true;
		$.Tache.Get({
		url: ajaxUrl,
		cache: true,
		success: function(html){
		isLoading = false;
		hideLoader();
		$("#leftContent").animate({"height": "0px"}, 400,function(){
			$("#tempFullContent").html(html);	
			document.title = $("#meta_title").attr("value");
					$("#tempLeftContent").html($("#db_leftContent").html());
					h = document.getElementById("tempLeftContent").offsetHeight;
					$("#leftContent").html($("#db_leftContent").html());
					$("#leftContent").animate({"height":  h+"px"}, openSpeed);
					$("#tempFullContent").html("");	
					activeSubscribeList();//leftContent.animate.subFunction		
		});//leftContent.animate
							
		}
		//end tempLeftContent.html
		});
// if currentAnchor
}//end function
