//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();
	bindPrintPage();
	bindBookmarkPage();
}

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

function bindPrintPage()
{
  $j('.printPage').click(function(){
    window.print();
  });
}

function bindBookmarkPage()
{
  $j('.bookmarkPage').click(function(){
         var url = window.location.href;
         var title = document.title;

         if (window.sidebar) { // Mozilla Firefox Bookmark
             window.sidebar.addPanel(title, url,"");
         } else if( window.external ) { // IE Favorite
             window.external.AddFavorite( url, title);
         } else if(window.opera) { // Opera 7+
             return false; // do nothing - the rel="sidebar" should do the trick
         } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
              alert('Unfortunately, this browser does not support the requested action,'
              + ' please bookmark this page manually.');
         }
  });
}

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

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

function swapVideos(obj)
{
  if($j(obj).hasClass('usa'))
  {
    if( $j('#CAVideo').length >0 )
    {
      $j('#CAVideo').html('');
      var so = new SWFObject('assets/flash/interactive_conversation_en_us.swf','USVideo','100%','400px','8');
      so.write('USVideo');
    }
  }
  else if($j(obj).hasClass('canada'))
  {
    if( $j('#USVideo').length >0 )
    {
      $j('#USVideo').html('');
      var so = new SWFObject('assets/flash/interactive_conversation_en_ca.swf','CAVideo','100%','400px','8');
      so.write('CAVideo');
    }
  }
}

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();

			$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('#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){
		}
	});
}
