/* Javascript Document */
/* Author: Travis Cunningham */
/* Date: 02/2008 */
/* Description: dts Onload Functions */


$jquery(function(){
	$jquery('#midNav ul').tabs({ unselected: false, cache: false });
	$jquery('#techTabs ul').tabs({ unselected: false, cache: false });
	//$jquery('#manuTabs ul').tabs({ unselected: false, cache: false });
	
	$jquery('#btnPrint').click(function(){window.print()});
	
	$jquery('#contentResults tr:odd').addClass('oddRow'); 
	$jquery('#trailerResults tr:even').addClass('evenRow'); 
	
	$jquery(".trigger").click(function () {
		var expandReady = true;
		if(expandReady && !$jquery(this).parent().next().hasClass("opened")) {
		expandReady = false;
		$jquery(".expand").animate({height: 'hide'}, "normal"); 
		$jquery(".trigger").removeClass("activeTrig");
		$jquery(".trigger").html('+');
		$jquery(".opened").removeClass("opened");
		$jquery(this).parent().next().animate({height: 'show'}, "normal", function() { expandReady = true; });
		$jquery(this).addClass("activeTrig");
		$jquery(this).parent().next().addClass("opened");
		$jquery(this).html('-');
		} 
	});
	
	$jquery(".hover").click(function () {
		var expandReady2 = true;
		if(expandReady2 && !$jquery(this).next().hasClass("hoverOn")) {
		expandReady2 = false;
		$jquery(".expand2").animate({height: 'hide'}, "normal"); 
		$jquery(".hoverOn").removeClass("hoverOn");
		$jquery(this).next().animate({height: 'show'}, "normal", function() { expandReady2 = true; });
		$jquery(this).next().addClass("hoverOn");
		} 
	});
	
	$jquery(".answer").click(function () {
		$jquery(this).next().toggle({height: 'show'}, "normal");
		if($jquery(this).hasClass("openAns")) {
		  $jquery(this).removeClass("openAns"); 
		}
		else {
		  $jquery(this).addClass("openAns");
		  var thisId = this.getAttribute('id');
		  var thisQuestion = this.getAttribute('question');
		  var thisCategory = this.getAttribute('category');
		  _sTrackFAQ(thisQuestion, thisCategory);	
		  url = "/Support/FAQ/Hit.aspx?q=" + thisId;
    	initRequest("POST", url, true,function(){})		  
		}
	});
	
	var currentLoc = window.location.hash;
	var anchorId = currentLoc.replace(/#/,'');		
	var answers = $jquery('.answer');
	//Compare URL to toggle faq answer open
	for(var i=0,z=answers.length;i<z;i++){
		if(anchorId == answers[i].id){
			var foundId = $jquery(anchorId);
			$jquery(foundId).next().toggle({height: 'show'}, "normal");
			$jquery(foundId).addClass("openAns");
			url = "/Support/FAQ/Hit.aspx?q=" + anchorId;
			initRequest("POST", url, true,function(){})
		}
	}

	//Initialize cookie audioLandingVisited;
	cookieFactory.checkAudioVisited();

	//Initialize jCarousel for the homepage
	if (document.getElementById('promos')) {
	    jQuery('#promos').jcarousel();
	}	
	
	/* ribbonObj= new Ribbon("ribbonCont","mainNavCont","ribbonObj"); */
	
	var innerLength = $jquery('#softSelectInner .softLink').length;
	if(innerLength > 0) {
		$jquery('#softSelectInner').width(innerLength * 150);
	}
	else {
		return false;
	}
	
	// Handle google link tracking
	initGoogleLinks();
});


function navbar(){
    this.container = "#mainNavCont";
    this.items = ["nav_consumer","nav_professional","nav_licensee"];
    this.timeout = 1000;
    this.timeIt = null;
    this.menu = null;
    this.menuDefault = "nav_consumer";
  }
  
navbar.prototype  = {
  setMenu : function(c){
      NAVBAR.menu = c;
  },
  doHover    : function(c) {
    this.setMenu(c);
    //$jquery("#navBlock div.secondaryNav").show();
    $jquery("#secondaryNavCont ul.subnav").hide();
    $jquery("#sub"+this.menu.id).css('display', 'inline');
    $jquery(this).css('background-position', '0 0');
    this.stopTimer();
    //this.timer();
  },
  doHoverOut : function(c){
    this.setMenu(c);
    $jquery("#secondaryNavCont ul.subnav").hide();
    //alert(this.menuDefault.id);
    $jquery("#sub"+this.menuDefault).css('display', 'inline');
    //var rdn=Math.floor(Math.random()*9 + 1);
    //$jquery("body").css({'background-color' : '#2'+rdn+'2'+rdn+'2'+rdn});
    if($jquery("ul.subnav").hover()){
      this.stopTimer();
    }
    else{
      this.timer();
    }
  },
  doClose : function(){
    $jquery("#secondaryNavCont ul.subnav").css('display', 'none');
    $jquery("#navBlock div.secondaryNav").css('display', 'none');
    this.stopTimer();
  },
  doClick : function(c) {
  	this.setMenu(c);
  	this.menuDefault = this.menu.id;
  	$jquery("#navBlock div.primaryNav a.btn_sprite").removeAttr("style");
    $jquery(this.menu).css('background-position', '0 0'); 
	  $jquery("#sub"+this.menu.id).css('display', 'inline');
	  this.stopTimer();
  },
  timer : function(c){
  	this.setMenu(c);
    this.timeIt = setTimeout('NAVBAR.doClose()', this.timeout);
  },
  stopTimer : function(c){
  	this.setMenu(c);
    if(this.timeIt){  
      clearTimeout(this.timeIt);
      this.timeIt = null;
    }
  }
};
  
//NAVBAR = new navbar();

function initGoogleLinks() {
	// Handle all interactive demo links
	// They need a rel="interactivedemo"
	// And rev="[label]"
	$('a[rel=interactivedemo]').click(function() {
		var rv=$(this).attr('rev');
		pageTracker._trackEvent("Engagement", "Interactive Demo", rv);
	});
	
	// Handle all social networks links
	// They need a rel="socialnetworks"
	// And rev="[label]"
	$('a[rel=socialnetworks]').click(function() {
		var rv=$(this).attr('rev');
		pageTracker._trackEvent("Engagement", "Social Networks", rv);
	});
	
	// Handle all logo download links
	// They need a rel="logodownload"
	// And rev="[label]"
	$('a[rel=logodownload]').click(function() {
		var rv=$(this).attr('rev');
		pageTracker._trackEvent("Engagement", "Logo Download", rv);
	});
	
	// Handle all video tutorial links
	// They need a rel="videotutorial"
	// And rev="[label]"
	$('a[rel=videotutorial]').click(function() {
		var rv=$(this).attr('rev');
		pageTracker._trackEvent("Engagement", "Video Tutorial", rv);
	});
	
	// Handle all form submissions
	// They need a rel="formcomplete"
	// And rev="[label]"
	$('form[rel=formcomplete]').submit(function() {
		var rv=$(this).attr('rev');
		pageTracker._trackEvent("Engagement", "Form Complete", rv);
	});
}

/**
 * Cookie factory
 */
cookieFactory = {
	/**
	 * check for audioPage and for previous visit
	 * 
	 * NOTE: abstracted initCarousel out of the document load section
	 * - when hiding that portion of the page, jCarousel complains
	 * 
	 * this.make("audioVisited", "true", -1); testing
	 **/
	checkAudioVisited:function(){
		//check for cookie presence
		if(this.audioLandingVisited()==-1){
			
			/* class .hdAudioHighlights resides only on Audio landing */
			if($jquery('.hdAudioHighlights').length){
				/*make cookie 365 days shelf-life */
				this.make("audioVisited", "true", 365); 
			}
			
			/* activate Audio page intro */
			else{ audioIntroInit(); }
		} 
		
		/* else allow jquery carousel on the home page. */
		else{ initCarousel(); }
	}, 
	/** check for audioLanding cookie **/
	audioLandingVisited:function(){
		return document.cookie.search("audioVisited=true");
	},

	/**
	* MAKE A BROWSER COOKIE
	* @param {string} id    - cookie index
	* @param {string} value - cookie value
	* @param {int} days     - life of the cookie
	*/
	make:function(id, value, days)
	{
	    document.cookie = id + "=" + escape(value);
	    if(days!==null && !isNaN(days))
	    {
	        var life = new Date();
	        life.setDate(life.getDate()+days);
	
	        document.cookie += "; expires=" + life.toUTCString() + ";";
	    }
	    document.cookie += ";path=./";
	}
}

/**
 * Enable and disable Audo intro on the home page
 * @return boolean for disabling <a> click event handled action
 */
function audioIntroInit(){
	//hide main content
	$jquery("div.content.home .section").fadeOut("fast");
	//show intro
	$jquery("div.content.home .intro").fadeIn("fast", function(){
		//enable skip button action
		$jquery(".skip").click(function(){
			//hide intro
			$jquery("div.content.home .intro").fadeOut("fast", function(){
				//show actual page
				$jquery("div.content.home .section").fadeIn("fast");
				//init jCarousel now if skip button is pressed
				initCarousel();
			});	
			//stop normal click action
			return false;
		});
	});
}


//Initialize jCarousel for the homepage
function initCarousel() {
	
	if($jquery('.section #promos').length && $jquery(".section").css("display") === "block" ){
		$jquery('#promos').jcarousel(); 
	}
}
