//Initiating jQuery to work with Prototype;
//var $j = jQuery.noConflict(); located inside jquery-1.3.2.min.js file

$j(document).ready(function() { onDocumentLoad(); });
$j(window).load(function() { onWindowLoad(); });

//Global Variables

/*
 * Standard page loading functions for prototype / jQuery
 */
function onDocumentLoad(){
	flag();
	bindSwapLogo();
}

//Stuff put into this function doesnt run until the entire document is loaded flash and all
function onWindowLoad(){
	
}

function bindSwapLogo()
{
  $j('#nav-locale li.active').each(function(){
    swapLogo(this);
  });

  $j('#nav-locale li').click(function(){
    swapLogo(this);
  });
}

function swapLogo(obj)
{
  if($j(obj).hasClass('usa'))
    swapLogoToUSA();

  else if($j(obj).hasClass('canada'))
    swapLogoToCanada();
}

function swapLogoToCanada()
{
  $j('#product_logo').removeClass('usa');
  $j('#product_logo').addClass('canada');
}

function swapLogoToUSA()
{
  $j('#product_logo').removeClass('canada');
  $j('#product_logo').addClass('usa');
}

//Chooses and sets which content to display based on user preference
//Default is usa.
function flag(){
	if(flagObj['currentFlag']!=null){
		$j('#nav-locale-home-div').hide();
		$j('#nav-locale').show();
		$j('#find_out').show();
		if($j('#discussionContent').css("display")=='none'){
			$j("#nav-locale li.usa").click(function () {
				$j(".usa").addClass('active');
				$j(".canada").removeClass('active');
				
				ajax("usa");
				
				if($j("#canContent :not(:empty)").length!=0){
					$j("#usContent").show();
					$j("#canContent").hide();
				}
			});
			
			$j("#nav-locale li.canada").click(function () {
				$j(".canada").addClass('active');
				$j(".usa").removeClass('active');
		
				ajax("canada");
		
				if($j("#canContent :not(:empty)").length!=0){
					$j("#usContent").hide();
					$j("#canContent").show();
				}
			});
			
			if(flagObj['currentFlag']=="canada"&&$j("#canContent :not(:empty)").length!=0){
				if($j("#nav-locale li."+flagObj['currentFlag']).length!=0){
					$j("#nav-locale li."+flagObj['currentFlag']).trigger("click");
				} else {
					$j("#canContent").show();
				}
			} else {
				$j("#usContent").show();
			}
		
			$j("."+flagObj['currentFlag']).addClass('active');
		} else {
			$j('#mainHeading').hide();
		}
	} else {
		$j('#nav-locale-home-div').show();
		$j('#usa').click(function() {
			$j(".usa").addClass('active');
			$j(".canada").removeClass('active');

			ajax("usa");

			if($j("#canContent :not(:empty)").length!=0){
				$j("#usContent").show();
				$j("#canContent").hide();
			} else {
				$j("#usContent").show();
			}

			flag();
		});

		$j("#canada").click(function () {
			$j(".canada").addClass('active');
			$j(".usa").removeClass('active');

			ajax("canada");

			if($j("#canContent :not(:empty)").length!=0){
				$j("#usContent").hide();
				$j("#canContent").show();
			} else {
				
			}

			flag();
		});
	}
}

function ajax(setFlag){                                 
	flagObj['currentFlag'] = setFlag;
	$j.ajax({
		type: "POST",
		url: "assets/snippets/FlagUpdate/FlagUpdate.ajax.php",
		data: flagObj,
		success: function(msg){
		}
	});
/*

    setCookie('prepare-locale',setFlag,0,'','','');
*/
	
}

function setCookie( name, value, expires, path, domain, secure )
{
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );
  
  /*
  if the expires variable is set, make the correct
  expires time, the current script below will set
  it for x number of days, to make it for hours,
  delete * 24, for minutes, delete * 60 * 24
  */
  if ( expires )
  {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );
  
  document.cookie = name + "=" +escape( value ) +
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
  ( ( path ) ? ";path=" + path : "" ) +
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ( ( secure ) ? ";secure" : "" );
}
