var HostAddress="";
var CurrentSection="";
var Assetspath;

$(document).ready(function(){
  $("#fldsThankYou").css({display:"none"});
  
  Preloader("");
  $("img[@src$=png]").pngfix();
  AssignRollOvers();
  BindHanders("Navigation0");
  BindHanders("Navigation1");
  BindHanders("ClickHere");
  BindHanders("Category");
  BindHanders("Thumbnail");
  BindHanders("Highlight");
  Hover("Category");
  Hover("Highlight");
  Hover("ClickHere");
  //Hover("Navigation1");
  Hover("Thumbnail");
  Catalog();
  CloseCatalog();
  $(".Navigation1 li a").css({cursor:"pointer"});
  $(".Category li a").css({cursor:"pointer"});
  $(".Highlight a").css({cursor:"pointer"});
  $(".Thumbnail li a").css({cursor:"pointer"});

  AutoClear();

  $("h1 a").css({cursor:"pointer"});
  $("h1 a")
  .bind("click",function(){
  	setHash($(this).attr("href").replace(HostAddress,".."));
  	return false;
  	});
  renderFlashContent();

//IE background flickering
	if(jQuery.browser.msie && jQuery.browser.version==6.0) 
	 {
	 document.execCommand("BackgroundImageCache",false,true);
   }
   
  HostAddress=location.href.split("/")[0]+"//"+location.href.split("/")[2];

  //HostAddress = "http://localhost:49573";
  
  Register();



});

function Register()
{
   $('.Submit1').click(function(){
    $(".ErrorMessage").hide();
    if(!isValidEmail($('.Highlight input').val()))
    {
     $(".ErrorMessage").show();
     $("#ltlError").hide();
     $("#pRegister").show();
     return false;
    }
    else
    {
     $(".ErrorMessage").hide();
     
     var Email = $("input#txtEmail").val();

     var dataString = 'txtEmail='+ Email;

	 $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "SaveData.aspx/Save",
      data: "{'strEmail': '" + Email + "','strLanguage': 'en'}",
      dataType: "json",
      success: function(msg) {
        if(msg == "-1")
        {
         $("#fldsForm").show();
         $("#fldsThankYou").hide();
         $("#pRegister").hide();
         $("#ltlError").show();
         $("#ltlError").html("The email address you have filled in is already registered.");
        }
        else if(msg =="-2")
        {
         $("#fldsForm").show();
         $("#fldsThankYou").hide();
         $("#pRegister").hide();
         $("#ltlError").show();
         $("#ltlError").html("Due to an unforseen error, your request could not be proceed. Please try later.");
        }
        else
        {
         $("#fldsForm").hide();
         $("#fldsThankYou").show();
         $("#pRegister").show();
        }
      }
     })
      return false;
    }


   });
}


 
function Hover(className)
{
 $("."+ className + " a")
  .bind("mouseover",function(e){
  	$(this).css({"opacity": "0.2"});
  });
  
 $("."+ className + " a")
  .bind("mouseout",function(e){
  	$(this).css({"opacity": "1"});
  });
}
 
//FUNCTION ROLLOVER
function AssignRollOvers()
{
 $(".Navigation0 li").css({"opacity": "0.95"});
 $(".Navigation0 li a")
 .bind("mouseover", function(e){
  if(!$(this).parent("li").hasClass("Active"))
  {   
   $(this).parent("li").addClass("Active");
   IsActive=1;
  }
  $(this).parent("li").stop().animate({width:'250px'},{queue:false, duration:150, easing: 'easeOutQuad'})
 });
  
 $(".Navigation0 li a")
 .bind("mouseout", function(e)
  {
   if(IsActive==1)
   {
    $(this).parent("li").removeClass("Active");
    IsActive=0;
   }
	 if($(this).parent("li").hasClass("Home"))
	  $(this).parent("li").stop().animate({width:'203px'},{queue:false, duration:450, easing: 'easeOutQuad'});
	 else if($(this).parent("li").hasClass("Products"))
	  $(this).parent("li").stop().animate({width:'169px'},{queue:false, duration:450, easing: 'easeOutQuad'});
	 else if($(this).parent("li").hasClass("Sampling"))
	  $(this).parent("li").stop().animate({width:'129px'},{queue:false, duration:450, easing: 'easeOutQuad'});
	 else if($(this).parent("li").hasClass("Challenge"))
	  $(this).parent("li").stop().animate({width:'146px'},{queue:false, duration:450, easing: 'easeOutQuad'});
	 else
	  $(this).parent("li").stop().animate({width:'146px'},{queue:false, duration:450, easing: 'easeOutQuad'});
  }); 
  
	$(".Navigation0 a").click(function(){
		$(this).blur();
	});
}


//FUNCTION PRELOADER - EXECUTES AT AJAX CALLS AND PAGE LOAD
function Preloader(targetURL)
 {
  if(targetURL=="")
   {
   	PageAnimate();
   }
  else
   {  
    $(".Preloader").fadeIn("slow",function(){LoadPage(targetURL);});
   }
 }
 
 
//FUNCTION POSTLOADER - EXECUTES AT AJAX CALLS AND PAGE LOAD END
function Postloader()
{
 $(".Preloader").fadeOut("slow");
}


//FUNCTION BindHanders - BINDS CLICK HANDLERS TO CLICKABLE ITEMS
function BindHanders(NavClass)
 {
  $("."+NavClass+" a")
  .bind("click",function()
   {
   	var targetURL;
    targetURL=$(this).attr("href"); 
    if($(this).attr("rel")!=undefined)
     CurrentSection=$(this).attr("rel");
     setHash($(this).attr("href").replace(HostAddress,".."));

    return false;
   }); 
 } 


//SWF ADDRESS SETHASH FUNCTION
function setHash(targetURL)
 {
   var hashVal=targetURL;
   hashVal=hashVal.replace(HostAddress,"");


    if(location.href.indexOf(CurrentSection)>=0 && CurrentSection!="")
    {
     //if(location.href.indexOf(targetURL.replace("..",""))<0)
      //$(".MainWrap .Page").animate({opacity:.2},500);
     //$(".Master .Preloader").clone().appendTo($(".MainWrap").eq(0));
     //$(".MainWrap").eq(0).find(".Preloader").css({marginTop:-23});
    }

   
   SWFAddress.setValue(hashVal.replace("../",""));
   
 }


//SWF ADDRESS CHANGEHANDLER FUNCTION
function changeHandler(Event)
 {

  if(Event.value=="/") return;

  var hashLength = Event.value.split("/").length;
  
  if(hashLength<3)
  Handlepath=Event.value.split("/")[1];


  else if(hashLength>3)
  Handlepath=Event.value.split("/")[1]+"/"+Event.value.split("/")[2]+"/"+Event.value.split("/")[3];  

  else
  Handlepath=Event.value.split("/")[1]+"/"+Event.value.split("/")[2];
  
  
  Assetspath=Handlepath;   
  Animateout();
 }

//SWF ADDRESS LISTENER CALL
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, changeHandler);


//LOADPAGE FUNCTION TO LOAD AJAX CALLBACK CONTENTS
function LoadPage(targetURL)
 {      
 	if(location.href.replace(HostAddress,"").split("/")[1].indexOf("Default")<0 && location.href.replace(HostAddress,"").split("/")[1].indexOf("Sampling")<0 && location.href.replace(HostAddress,"").split("/")[1].indexOf("7DayChallenge")<0)
 	targetURL="../"+targetURL;
  
  $.get(targetURL+"?decache="+(Math.random()*999999),function(data)
   {
   	var Markup;
   	
    var obj = $(".MainWrap").parent().get(0).tagName;

    if($(data).hasClass("Skin2"))
    {
     $("div").filter(".Master").removeClass("Skin1").addClass("Skin2");
    }
    else
    {
     $("div").filter(".Master").removeClass("Skin2").addClass("Skin1");
    }

   	if(location.href.replace(HostAddress,"").split("/").length<5 && location.href.replace(HostAddress,"").split("/")[1].indexOf("Default")<0 && location.href.replace(HostAddress,"").split("/")[1].indexOf("Sampling")<0 && location.href.replace(HostAddress,"").split("/")[1].indexOf("7DayChallenge")<0)
   	 Markup=data.replace(/Common\//g,"../Common/");
    
    else if(location.href.replace(HostAddress,"").split("/")[1].indexOf("Default")<0 && location.href.replace(HostAddress,"").split("/")[1].indexOf("7DayChallenge")<0 && location.href.replace(HostAddress,"").split("/")[1].indexOf("Sampling")<0)
     Markup=data;
   	   	 
   	else
   	 Markup=data.replace(/\.\.\//g,"");



    
    Markup=$(Markup).find(".MainWrap").html();
    $(".MainWrap div").remove();
    
    if(location.href.indexOf(CurrentSection)>=0 && CurrentSection!="")
    {
     $(".MainWrap").html(Markup);

     CurrentSection="";
     Postloader();
     Animatein("Inner");
    }
    else
    {
     $(".MainWrap").html(Markup);
     Postloader();
     Animatein("Home");
    }
     $("#fldsThankYou").css({display:"none"});
     BindHanders("Navigation0");
     BindHanders("Navigation1");
     BindHanders("Category");
     BindHanders("ClickHere");
     BindHanders("Highlight");
     AssignRollOvers();
     $(".Navigation1 li a").css({cursor:"pointer"});
     $(".Category li a").css({cursor:"pointer"});
     $(".Thumbnail li a").css({cursor:"pointer"});
     $(".Highlight a").css({cursor:"pointer"});
     Catalog();
     CloseCatalog();
     AutoClear();
     Register();
     Hover("Category");
     Hover("Highlight");
     Hover("ClickHere");
     //Hover("Navigation1");
     Hover("Thumbnail");
     $("img[@src$=png]").pngfix();
     $("h1 a").css({cursor:"pointer"});
     $("h1 a")
     .bind("click",function(){
  	  setHash($(this).attr("href").replace(HostAddress,".."));
  	  return false;
  	 });
  	 renderFlashContent();
   }); 

 }

//function to clear textbox
function AutoClear()
{
	$('.AutoClear')
	.each(function(){
		var autoText = "";
		var defaultText = $(this).val();
		$(this).bind('focus',function(){
		autoText = $(this).val();
		if(autoText == defaultText) $(this).val("");
		})
		.bind('blur',function(){
			if($(this).val() == "")
			$(this).val(defaultText);
		});
	});
}




//FUNCTION ANIMATEOUT TO DELEGATE PAGE SECTION UNLOAD ANIMATIONS
function Animateout()
{
  $("object,embed").remove();	
  $(".Page").animate({opacity:".2"},250,function(){
  Preloader(Assetspath);
  });

  if(location.href.indexOf(CurrentSection)>=0 && CurrentSection!="")
   Preloader(Assetspath);
}


//FUNCTION ANIMATEIN TO DELEGATE PAGE SECTION LOAD ANIMATIONS
function Animatein(AnimSection)
{
 if(AnimSection=="Inner")
 {
  $.blockUI();
	$(".Page").animate({left:"0px"},{duration:1500,easing:"easeOutQuad"}); 
 	$.unblockUI();
 }
 
 if(AnimSection=="Home")
 {
  $.blockUI();
  $(".Page").animate({left:"0px"},{duration:1500,easing:"easeOutQuad"}); 
 	$.unblockUI();
 }
}


//FUNCTION PAGEANIMATE TO DELEGATE PAGE REFRESH ANIMATIONS
function PageAnimate()
{
 //if(location.href.indexOf("#")<0)
 {
  $.blockUI();   
  $(".Preloader").show();
  PreloadImages();
  $(".Page").animate({left:"0px"},{duration:1500,easing:"easeOutQuad"}); 
  $(".Preloader").hide();
  $.unblockUI();
 }
}


/* FUNCTION PRELOADIMAGES CHECKS IF ALL THE IMAGES OF A PAGE ARE LOADED */
var ImagesLoaded=0; 
function PreloadImages()
 {
   if(ImagesLoaded+1==$("img").length)
    {
     $("img").eq(ImagesLoaded).load(function(){$(".Preloader").fadeOut("slow");});
     return;
    }
   else
    {
  	 ImagesLoaded=ImagesLoaded+1;
  	 PreloadImages();
    }
}

//catalog function
function Catalog()
{
  var swfObject, swfObjectWrite;
  $(".Thumbnail a")
  .bind("click",function(e)
   {
   	 $(".Toogle").hide();
   	 
     if($(this).parents("li").eq(0).hasClass("ARepairCream"))
     {
     	 $("#ARepairCream").show();
         swfObject = "../Common/Flash/ARepairCream.swf";
         swfObjectWrite = "ARepairCreamSwf";
     }
     else if($(this).parents("li").eq(0).hasClass("AFacialFoam"))
     {
     	 $("#AFacialFoam").show();
         swfObject = "../Common/Flash/AFacialFoam.swf";
         swfObjectWrite = "AFacialFoamSwf";
     }
     else if($(this).parents("li").eq(0).hasClass("AEyeTherapy"))
     {
     	 $("#AEyeTherapy").show();
         swfObject = "../Common/Flash/AEyeTherapy.swf";
         swfObjectWrite = "AEyeTherapySwf";
     }
     else if($(this).parents("li").eq(0).hasClass("AMPC"))
     {
     	 $("#AMPC").show();
         swfObject = "../Common/Flash/AMPC.swf";
         swfObjectWrite = "AMPCSwf";
     }
     else if($(this).parents("li").eq(0).hasClass("ADailyCream"))
     {
     	 $("#ADailyCream").show();
         swfObject = "../Common/Flash/ADailyCream.swf";
         swfObjectWrite = "ADailyCreamSwf";
     }
     else if($(this).parents("li").eq(0).hasClass("FacialFoam"))
     {
         $("#FacialFoam").show();
         swfObject = "../Common/Flash/FacialFoam.swf";
         swfObjectWrite = "FacialFoamSwf";
     }
     else if($(this).parents("li").eq(0).hasClass("DayCream"))
     {
         $("#DayCream").show();
         swfObject = "../Common/Flash/DayCream.swf";
         swfObjectWrite = "DayCreamSwf";
     }
     else if($(this).parents("li").eq(0).hasClass("DayLotion"))
     {
         $("#DayLotion").show();
         swfObject = "../Common/Flash/DayLotion.swf";
         swfObjectWrite = "DayLotionSwf";
     }
     else if($(this).parents("li").eq(0).hasClass("NightTreatment"))
     {
         $("#NightTreatment").show();
         swfObject = "../Common/Flash/NightTreatment.swf";
         swfObjectWrite = "NightTreatmentSwf";
     }
     else if($(this).parents("li").eq(0).hasClass("WhiteningSerum"))
     {
         $("#WhiteningSerum").show();
         swfObject = "../Common/Flash/WhiteningSerum.swf";
         swfObjectWrite = "WhiteningSerumSwf";
     }
     else
     	 $("#ARepairCream").show();
     	 
    $(".Columns").animate({width:"1300px"},200,function(){
     $(".Columns").animate({left:"-284px"},{duration:600,easing:"easeOutQuad"}); 
     $(".Column3").show().animate({left:"-36px"},{duration:600,easing:"easeOutQuad"}); 
     $(".Column3").fadeIn({duration:600,easing:"easeOutQuad"});
    });
    
	var loader = new SWFObject(swfObject, "Product", "433", "650", "8", "#ffffff");
    loader.addParam("wmode", "transparent");
	loader.addParam("scale", "noscale");
    loader.write(swfObjectWrite);

    return false;
   }); 
}

//function catalog close
function CloseCatalog()
{
  $(".Close a")
  .bind("click",function()
   { 
     $("object,embed").remove();
     $(".Columns").animate({width:"867px"},200,function(){
     $(".Column3").animate({left:"-436px"},{duration:600,easing:"easeOutQuad"}).hide(); 
     $(".Column3").fadeIn({duration:600,easing:"easeOutQuad"});
     $(".Columns").animate({left:"0px"},{duration:600,easing:"easeOutQuad"}); 
   });

    return false;
   }); 
}

 //function to check valid email address
 function isValidEmail(strEmail){
  
  validRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  
  var Email = strEmail;

   // search email text for regular exp matches
   if (Email.search(validRegExp) == -1 || Email.length == 0) 
   {
      return false;
   } 
    return true; 
  }
  
  
/* render flash contents */
	function renderFlashContent(){

		/** Homepage video player */
		if($("#ProductList").length)
			main.write("ProductList");
		else
		 $("#ProductList").find("object,embed").remove();

		if($("#HomeSpotlight").length)
			main.write("HomeSpotlight");
		else
		 $("#HomeSpotlight").find("object,embed").remove();

		if($("#AgemiracleSpotlight").length)
			main.write("AgemiracleSpotlight");
		else
		 $("#AgemiracleSpotlight").find("object,embed").remove();		 
		 
		if($("#FLSpotlight").length)
			main.write("FLSpotlight");
		else
		 $("#FLSpotlight").find("object,embed").remove();	

	}