// global.js

var ie6 = ($.browser.msie && jQuery.browser.version == "6.0");

var global = ({
   init: function(){
   	var urls = ["/images/nav/events_on","/images/nav/home_on","/images/nav/news_on","/images/nav/nv-lineup_on","/images/nav/our-story_on","/images/buttons/get-updates_on"];
   	$.preload( urls, {
   		ext:'.png'
   	});
      
      global.externals();
      global.textReplace();
      global.otherNissanSites();
      global.footerLinks();

      global.loadAddThis();
      
   },
   preloadImages: function(loadThese){
      // array of images to be pre-loaded
      $.preLoadImages(loadThese);
   },
   externals: function(){
      var eLinks = $(".ext");
      //var newWinStr = "Click to open this link in a new window";
      
      $(eLinks).each(function(){
         $(this).attr("target","_blank");
         //$(this).attr("title",newWinStr);
         //$(this).attr("alt",newWinStr);
      });
   },
   footerLinks: function(){
     var links = $("#footer-links li a.link, #other-container li a, #navigation-top .ext");
     
     $(links).bind("click",function(ev){
        ev.preventDefault();
        
        //store original destination
        var dest = $(this).attr("href");
        
        //call Omniture pageview tracking (stored in 799.js)
        crmEvent99(dest);

        //proceed along :-)
        if($(this).hasClass("ext")){
           window.open(dest);
        }else{
           window.location.href = dest;
        }
        
     });
      
   },
   textReplace: function(){
      
      sIFR.replace(grot, {
        selector: '#home-content-left div.small-promo div.copy h3',
        wmode: 'transparent',
        css: '.sIFR-root { color: #666666; text-transform: uppercase;}'
      });

      sIFR.replace(grot, {
        selector: '#content #leftcol h2',
        wmode: 'transparent',
        css: '.sIFR-root { color: #666666;}'
      });

      sIFR.replace(grot, {
        selector: '#content h3.red-headline',
        wmode: 'transparent',
        css: '.sIFR-root { color: #CC0000; text-transform: uppercase}'
      });

      sIFR.replace(grot, {
        selector: '#content #rightcol h2',
        wmode: 'transparent',
        css: '.sIFR-root { color: #666666; text-transform: uppercase;}'
      });

      sIFR.replace(grot, {
        selector: '#content #rightcol h3.upper',
        wmode: 'transparent',
        css: '.sIFR-root { color: #666666; text-transform: uppercase;}'
      });

      sIFR.replace(grot, {
        selector: '#content #rightcol h3',
        wmode: 'transparent',
        css: '.sIFR-root { color: #666666; leading: 7;}'
      });

      sIFR.replace(grot, {
        selector: '#home-content-left div.large-promo div.copy h3.small',
        wmode: 'transparent',
        tuneHeight: -5,
        css: '.sIFR-root { color: #666666; text-transform: uppercase;}'
      });

      sIFR.replace(grot, {
        selector: '#home-content-left div.large-promo div.copy h3.large',
        wmode: 'transparent',
        css: '.sIFR-root { color: #666666; text-transform: uppercase;}'
      });

      sIFR.replace(grot, {
        selector: '#feature-content #sub-nav-container h2',
        wmode: 'transparent',
        css: '.sIFR-root { color: #8997a0; text-transform: uppercase;}'
      });

      sIFR.replace(grot, {
        selector: '#player-nav h3',
        wmode: 'transparent',
        tuneHeight: 5,
        css: '.sIFR-root { color: #ffffff; leading: 7;}'
      });
      
      sIFR.replace(grot, {
        selector: '#hero-container #hero-copy h3',
        wmode: 'transparent',
        css: '.sIFR-root { color: #ffffff;}'
      });

      sIFR.replace(grot, {
        selector: '#title-box h2',
        wmode: 'transparent',
        css: '.sIFR-root { color: #8997a0; text-transform: uppercase;}'
      });
      
      sIFR.replace(grot, {
        selector: 'h3.headline, h4.headline',
        wmode: 'transparent',
        css: '.sIFR-root { color: #3e4f58;}'
      });
      
      sIFR.replace(grot, {
        selector: '#content .tier h4',
        wmode: 'transparent',
        css: '.sIFR-root { color: #505050;}',
        filters: {
           GradientBevel: {
              angle: 90,
              colors: '#505050,#ab0707',
              strength: 100
           }
        }
      });
     
       sIFR.replace(grot, {
        selector: 'h3.title',
        wmode: 'transparent',
        css: '.sIFR-root { color: #666666; text-transform: uppercase;}'
      });
      
      sIFR.replace(grot, {
        selector: '#accordion h2',
        wmode: 'transparent',
        css: '.sIFR-root { color: #ffffff; text-transform: uppercase;}'
      });
      
      sIFR.replace(grot, {
        selector: '#title-box .featured-date',
        wmode: 'transparent',
        css: '.sIFR-root { color: #9aabbb;}'
      });

      sIFR.replace(grot, {
        selector: '#title-box #video-frame .countdown',
        wmode: 'transparent',
        css: '.sIFR-root { color: #c30707;}',
        filters: {
           DropShadow: {
              angle: 131,
              distance: 3,
              strength: 3
           },
           GradientBevel: {
              angle: 90,
              colors: '#c70808,#ab0707',
              strength: 100
           }
        }
      });
      
   },
   otherNissanSites: function(){
      var oLink = $("a[href='/other-nissan-sites.php']",$("#footer"));
      var oContainer = $("#other-container");
      var closer = $(".closer",$(oContainer));
      
      $(oLink).bind("click",function(ev){
         ev.preventDefault();
         
         if(parseFloat($(oContainer).css("height")) !== 85){

            $(oContainer).stop().animate({
               paddingTop: 18,
               paddingBottom: 8,
               height: 85
            }).css({
               borderTop: "solid 1px #686868"
            });
            
         }else{

            $(closer).trigger("click");
            
         }
         
      });
      
      
      $(closer).bind("click",function(ev){
         ev.preventDefault();
         
         $(oContainer).stop().animate({
            paddingTop: 0,
            paddingBottom: 0,
            height: 0
         }).css({
            borderTop: 0
         });
         
      });
      
   },
   fireCrmEvent: function(pageName,args){
		switch(pageName){
			case "NVC_Home":
		      // args: none
			   crmEvent1(true);
			break;
			case "NVC_Home_Comment_Submit":
			   // args: none
			   crmEvent2();
			break;
			case "NVC_Home_Photo_View":
			   // args: image name (whatever.jpg)
			   crmEvent3(args[0]);
			break;
			case "NVC_Home_Quote_View":
			   // args: quote ID (maps to quote ID in DB)
			   crmEvent4(args[0]);
			break;
			case "NVC_Home_Video_View":
			   // args: video name (whatever.flv)
			   crmEvent5(args[0]);
			break;
			case "NVC_Home_Poll_Submission":
			   // args: poll id, answer
			   crmEvent6(args[0],args[1]);
			break;
			case "Features_360_Color_Change":
			   crmEvent7(args,false,false,false,false);
			break;
			case "Features_360_Height_Change":
			   crmEvent7(false,args,false,false,false);
			break;
			case "Features_360_Background_Change":
			   crmEvent7(false,false,false,args,false);
			break;
			case "Features_360_Interior_View":
			   crmEvent7(false,false,false,false,true);
			break;
			case "Our_Story":
		      // args: none
		      crmEvent1();
			break;
			case "News_Home":
   	      // args: none
   	      crmEvent1();
			break;
			case "News_Home_Video_View":
   	      crmEvent2();
			break;
			case "News_Home_News_Link":
   	      crmEvent3();
			break;
			case "News_<title>":
   	      // args: none
   	      crmEvent1();
			break;
			case "Events_Home":
   	      // args: none
   	      crmEvent1();
			break;
			case "Events_NTEA_Webcast":
   	      // args: none
   	      crmEvent1();
			break;
			case "Events_NTEA_Webcast_View":
   	      crmEvent2();
			break;
			case "Events_Ext_Link":
   	      crmEvent3(args);
			break;
			case "Get_Updates_Form":
   	      // args: none
   	      crmEvent1();
			break;
			case "Get_Updates_Error":
   	      crmEvent2();
			break;
			case "Get_Updates_Confirm":
   	      // args: none
   	      crmEvent1();
			break;
			case "Site_Map":
   	      // args: none
   	      crmEvent1();
			break;
			case "Privacy_Policy":
   	      // args: none
   	      crmEvent1();
			break;
			case "Copyright":
   	      // args: none
   	      crmEvent1();
			break;
			case "Global_Link":
   	      // args: none
   	      crmEvent1();
			break;			
			case "Answers_Video_Start":
            // args: videoStart,percent25,percent50,percent75,percent80,percent100,channel_title,channel_photo,channel_video,panelAction
   	      crmEvent2(true,false,false,false,false,false,false,false,args[1],false);
			break;			
			case "Answers_Video_25":
            // args: videoStart,percent25,percent50,percent75,percent80,percent100,channel_title,channel_photo,channel_video,panelAction
   	      crmEvent2(false,true,false,false,false,false,false,false,args[1],false);
			break;			
			case "Answers_Video_50":
            // args: videoStart,percent25,percent50,percent75,percent80,percent100,channel_title,channel_photo,channel_video,panelAction
   	      crmEvent2(false,false,true,false,false,false,false,false,args[1],false);
			break;			
			case "Answers_Video_75":
            // args: videoStart,percent25,percent50,percent75,percent80,percent100,channel_title,channel_photo,channel_video,panelAction
   	      crmEvent2(false,false,false,true,false,false,false,false,args[1],false);
			break;			
			case "Answers_Video_80":
            // args: videoStart,percent25,percent50,percent75,percent80,percent100,channel_title,channel_photo,channel_video,panelAction
   	      crmEvent2(false,false,false,false,true,false,false,false,args[1],false);
			break;			
			case "Answers_Video_100":
            // args: videoStart,percent25,percent50,percent75,percent80,percent100,channel_title,channel_photo,channel_video,panelAction
   	      crmEvent2(false,false,false,false,false,true,false,false,args[1],false);
			break;			
			case "Answers_Video_Overlay":
            // args: videoStart,percent25,percent50,percent75,percent80,percent100,channel_title,channel_photo,channel_video,panelAction
   	      crmEvent2(false,false,false,false,false,false,args[0],args[1],false,args[2]);
			break;			
			case "Global_Video_View":
			   crmEvent98(args[0],args[1]);
			break;
			case "Global_Photo_View":
			   crmEvent97(args[0],args[1]);
			break;
			default: 
   	      // args: none
   	      crmEvent1(true);
			break;
		}
		
		
      
   },
   crmCallback: function(){
		var ev = Shadowbox.getCurrent();
		if(ev.player === "img"){
		   var loc = window.location.href.split("/").reverse();
	   	   var args = [ loc[0] + " | " + ev.content ,loc[0]];
	
	   	   global.fireCrmEvent("Global_Photo_View",args);
	
		}
   },
   getFlashMovie: function(movieName){
 			var isIE = navigator.appName.indexOf("Microsoft") != -1;
  			return (isIE) ? window[movieName] : document[movieName];      
   },
   loadAddThis: function(){
     if(!ie6){
        var at = '<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=ncvnews"></script>';
  		
  		   $("#global-script").after(at);
        
     } 
   },
   setupDisclaimers: function(){
      
      var dis = $(".dis");
      
      $(dis).each(function(){
         
         var link = "<a href='#' class='disclaimer'>Disclaimer</a>";
         var disContext = $(this).attr("rel");
         
         var parent = $(this).parent();
         
         $(parent).append(link);
         
         var dislink = $('.disclaimer',$(parent));
         var classNames = $(this).attr("class").split(" ");
         
         $(dislink).bind("click",function(ev){
            ev.preventDefault();
            
            // fetch disclaimer copy based on keywords
               
               var disclaimerEl = "<div class='dis-box'><ul>";
               classNames.splice(0,1);
               var str="";
               for(i=0;i<classNames.length;i++){
                  str += classNames[i] + "+";
               }
               str = str.substring(0,str.length-1);
               
               var theLink = $(this);
               
               $.get("/includes/tools/fetch_disclaimer.php?getThese=" + str,function(data){
                  
                  disclaimerEl += data + "</ul></div>";
                  
                  Shadowbox.open({
                     content: disclaimerEl,
                     player: 'html'
                  });
                  
               });
               
         });
         
         
      });
      
      
   }
   
});