﻿/*******************************************************************************
Module: Page analytics using Omniture sitecatalyst.
File: jquery.Tagging.js
Initial Author: Manish
Date Created: 5/22/2009 
Purpose: jQuery code for tagging links on a page using Omniture sitecatalyst.
Copyright (c) 2008 Vail Resorts, Inc.
*******************************************************************************/

// Binds click event on each link fetched through the given selector.
function attachClick(selector, func){
    
    $(selector).each(function(){
        // Copy the default onclick handler
        var handler = this.onclick; 
    	
        // Remove the default onclick handler
        this.onclick = function(){}; 

        // Attach click on link
        singleClick(this, handler, func);
    });
}

// Binds click event on a link after a delay of 500ms
function singleClick(obj, handler, func){
    // Bind click event only for one time.
    $(obj).one("click", function(){
        
        // Handle the validation of results
        var result = true;
    
        // Get the page hierarchy
        var seoHierarchy = $(obj).attr("name");
        
        // Set analytics variables
	    func(seoHierarchy);
	    
	    // Call the default onclick function
	    if(handler!= null)
	    {
	        result = handler();
	    }
	    
	    // Delay the click event by 500ms
	    setTimeout(function(){
	        singleClick(obj, handler, function(seoHierarchy){
	            func(seoHierarchy);
	        });
	    }, 500);
	    
	    return result;
    });
    
}
	
function BindLodgingData()
{
    // Tag plan a Lodging button
	attachClick(".tagLodgingData", function(seoHierarchy){
        var tagArray = omniturePixels.split(":");
        
        //set variables
        linkTrackVars='products';
        linkTrackEvents='scAdd';  
        s.events='scAdd, event33';
        s.products = tagArray[tagArray.length - 1];
        s.tl(this, 'o', omniturePixels);   
	});
}	
function BindLodgingDetail()
{       
    // Tag plan a Lodging button
    attachClick(".tagcontforcart", function(seoHierarchy){
    var seoHierarchy = $(".tagcontforcart").prev().attr("value");
   var productCode=$("input[type=hidden][name$=hdnLodgingProduct]").val(); 
   //Modified by Naveen Kumar
     if(seoHierarchy != null)
        {
    linkTrackVars='products';
    linkTrackEvents='scAdd';  
     s.events='scAdd, event33';
     s.products = productCode;
     s.tl(this, 'o', seoHierarchy); 
	}});
}	

function TagEquipmentRentalResults()
{
    if(document.getElementById('hdnEquipmentRentals') != null)
    {
        attachClick(".tagEquipmentRentalData", function(seoHierarchy){
            var hdtag='';   
            $('.dropItem li').each(function(i){              
               if(hdtag=='')
                {      
                    hdtag=$(this).find('.boxData #hdSelectedSKU').val();
                }
                else
                {
                    hdtag+=","+$(this).find('.boxData #hdSelectedSKU').val();
                }
            });
            //Modified by Naveen to correct to tagging of Equipment Rental: 21st Oct.
            var omniturePixels=document.getElementById('hdnEquipmentRentals').value;
            linkTrackVars='products';
            linkTrackEvents='scAdd';
            s.events='scAdd';
            s.products = hdtag;
            s.tl(this, 'o', omniturePixels);
            
	    });
	}
}

function TagLessonResults()
{

  // Tag plan for Lift Ticket
	attachClick(".tagClassesData", function(seoHierarchy){      
	    var hdtag='';    
        $("ul.dropItem li").each(function(i){   
            if(hdtag=='')
                {
                    var hdnValue = $(this).find("input[type=hidden][name=purchasedLessonID]").val();
                    if(hdnValue != '')
                    {
                        var ArrProductCodes = hdnValue.split('|');
                        hdtag = ArrProductCodes[0];
                    }
                }
                else
                {
                    var hdnValue = $(this).find("input[type=hidden][name=purchasedLessonID]").val();
                    if(hdnValue != '')
                    {
                        var ArrProductCodes = hdnValue.split('|');
                        hdtag += ","+ArrProductCodes[0];
                    }
                }
        });
                 
                 
      
	    var seoHierarchy = $(".tagClassesData").prev().attr("value");
        
        document.getElementById('hdnLessonDetails').value +=":"+hdtag;
        var completeHierarchy = document.getElementById('hdnLessonDetails').value ;
        if(completeHierarchy != null)
        {
            var tagArray = completeHierarchy.split(":");
            //set variables
            linkTrackVars='products';
            linkTrackEvents='scAdd';
            s.events = 'scAdd';
            s.products = tagArray[tagArray.length - 1];
            s.tl(this, 'o', seoHierarchy);
        }
        
	});
}

function BindFlightTags()
{
//Modified by Naveen to correct tagging for air: 20th Oct.
    attachClick(".tagContinueData", function(seoHierarchy){
        var seoHierarchy = $(".tagContinueData").prev().attr("value");
        var productCode=$("input[type=hidden][name=flightSearch]").val();
        if(seoHierarchy != null)
        {
            //set variables
            linkTrackVars='products';
            linkTrackEvents='scAdd';
            s.events = 'scAdd';
            s.products = productCode;
            s.tl(this, 'o', seoHierarchy);
        }
	});	
}
//Bind Child care nursery continue button tagging
function BindCCNContinueTags()
{
    var seoHierarchy = "";
    $("#ChildCareTypeContainer .radioButtonList").each(function() {
        if ($(this).find(":radio").length > 0) {
            var value = $(this).find(":radio:selected").val();
        } else {
            var value = $(this).find(":hidden:eq(0)").val();
        }
        seoHierarchy += ";"+ value ;
    });
        if(seoHierarchy != null)
        {
            linkTrackVars='products';
            linkTrackEvents='scAdd';
            s.events = 'scAdd';
            s.products = seoHierarchy;
            s.tl(this, 'o',$(".ccnseoHierarchy").val());
        }
}

//Bind Dining Book it Tagging
function BindDiningBookItTags()
{
    var seoHierarchy = "";
    seoHierarchy = ";" +  $("#merchantId").val();
    if(seoHierarchy != null)
    {
        linkTrackVars='products';
        linkTrackEvents='scAdd';
        s.events = 'scAdd';
        s.products = seoHierarchy;
        var val = $(".DiningProduct").val();
        s.tl(this, 'o',val);
    }
}

//Binding Save Item to Trip Tagging
function BindSaveItineraryTags() 
{
    //alert($(".SaveItinerary_pixels").val());

    if ($(".SaveItinerary_pixels").val() != "") 
    {
        //alert($(".SaveItinerary_pixels").val());
        s.tl(this, 'o', $(".SaveItinerary_pixels").val());
        s.events = "event15";
    }
}
	
// Attach custom click event on a link on page load.	
$(function(){
     // Tag save itinerary button
    if ($(".tagSaveItinerary").length > 0) {
        //debugger;
        attachClick(".tagSaveItinerary",function(){
            BindSaveItineraryTags();
        });
    }

    if($(".tagBookDining").length>0)
	{
        //Tag BookIt Dining button
        attachClick(".tagBookDining",function(){
            BindDiningBookItTags();
        });
    }

    //Tag Childcare nursery continue button
	if($(".tagCCNContinue").length>0)
	{
	    attachClick(".tagCCNContinue", function(){
	        //alert('attaching event');
	        BindCCNContinueTags();
	    });
    }

	// Tag promotional links
	attachClick(".tagPromoLink", function(seoHierarchy){
	s.evar5 = 'INT_PROMO_ID';
	s.events = 'event1';
	s.tl(s.pageURL, 'o', seoHierarchy);
	});
	
	// Tag plan a vacation button
	attachClick(".tagPlanButton", function(seoHierarchy){
        var seoHierarchy = $(".tagPlanButton").prev().attr("value");
        s.evar35 = seoHierarchy;
        s.tl(s.pageURL, 'o', seoHierarchy);
	});
	
	// Tag plan a vacation button
	attachClick(".inputButton", function(seoHierarchy){
        var seoHierarchy = $(".inputButton").prev().attr("value");
        s.evar35 = seoHierarchy;
        s.tl(s.pageURL, 'o', seoHierarchy);
	});
	
	
	// Tag quick links
	attachClick(".tagQuickLink", function(seoHierarchy){
        if(seoHierarchy != null)
        {
            seoHierarchy=seoHierarchy.replace("http://","").replace("http%3A//","");
            s.tl(s.pageURL, 'o', seoHierarchy);
        }
	});
	
	// Bind Lodging Data
	if($(".tagLodgingData").length>0)
	{
	    BindLodgingData();
	}
	
	if($(".tagcontforcart").length>0)
	{
	    BindLodgingDetail();
	}
	
	// Tag flight search results
	if($(".tagContinueData").length>0)
	{
	    BindFlightTags();
	}
	
	 // Tag plan for Lift Ticket
	attachClick(".tagLiftTicketContinueData", function(seoHierarchy){          
	    var hdtag='';    
        $("ul.dropItem li").each(function(i){   
            if(hdtag=='')
                {
                 hdtag=$(this).find("input[type=hidden][name=SelectedSKU]").val();                             
                          
                }
                else
                {
                 hdtag+=","+$(this).find("input[type=hidden][name=SelectedSKU]").val();
                }
        
                    
        });
                 
        document.getElementById('hdnLiftTicket').value+=":"+hdtag;
	    var seoHierarchy = $(".tagLiftTicketContinueData").prev().attr("value");
        
        if(seoHierarchy != null)
        {
            var tagArray = seoHierarchy.split(":");
            //set variables
            linkTrackVars='products';
            linkTrackEvents='scAdd';
            s.events = 'scAdd';
            s.products = tagArray[tagArray.length - 1];
            s.tl(this, 'o', seoHierarchy);
        }
	});
	
	// Tag plan for Lessons
	 if($(".tagClassesData").length>0)
	 {
	    TagLessonResults();
	 }
	 // Tag plan for Equipment Rental
	 if($(".tagEquipmentRentalData").length>0)
	 {
	    TagEquipmentRentalResults();
	 }
	 
	 // Tag plan for Equipment Rental
	 attachClick(".tagSeasonPassesExplorerData", function(seoHierarchy){
	    
        var hdtag='';   
        $("ul.dropItem li").each(function(i){  
         var div1 = $(this).find(".boxData"); 
         var div2 = div1.find(".amount");
         var sSKU = div2.find("input[type=hidden][class='hdnSKU']").val();
         
           if(hdtag=='')
            {      
                hdtag=sSKU ;
            }
            else
            {
               
                hdtag+=","+sSKU ;
            }
            
           
        });
          
          var seoHierarchy = document.getElementById('hdnSeasonPasses').value;

        if(hdtag != null)
        {
       
            linkTrackVars='products';
            linkTrackEvents='scAdd';
            s.events = 'scAdd';
            s.products = hdtag;
            s.tl(this, 'o', seoHierarchy);
        }
	});
	
	
	// Tag plan a Ground Transportation Continue
	attachClick(".btnRight", function(seoHierarchy){
        var seoHierarchy = $(".btnRight").prev().attr("value");
        if(seoHierarchy != null)
        {
            var tagArray = seoHierarchy.split(":");
            //set variables
            linkTrackVars='products';
            linkTrackEvents='scAdd';
            s.events = 'scAdd';
            s.products = tagArray[tagArray.length - 1];
            s.tl(this, 'o', seoHierarchy);
		}
        
	});
	
	// Tag plan a Offer Explorer Continue
	attachClick(".openModalForTraveller", function(seoHierarchy){
        var seoHierarchy = $(".openModalForTraveller").prev().attr("value");
        
        if(seoHierarchy != null)
        {
            var tagArray = seoHierarchy.split(":");
            //set variables
            linkTrackVars='products';
            linkTrackEvents='scAdd';
            s.events = 'scAdd';
            s.products = tagArray[tagArray.length - 1];
            s.tl(this, 'o', seoHierarchy);
		}
        
	});
	
	// Tag keyword search links
	attachClick("ul.resultPanel li h4 a", function(seoHierarchy){
        linkTrackVars = 'events';
        linkTrackEvents = 'event32';
        s.events = 'event32';
        s.tl(this, 'o', 'keystoneresort:internal search clicked');
	});
	
	// Tag learn more link on vacation deals widget
	attachClick(".tagLearnMore", function(seoHierarchy){
	    if(seoHierarchy != null)
	    {
	        var tagArray = seoHierarchy.split(":");
    	    
            //set variables
	        s.evar5 = tagArray[tagArray.length - 1];
	        s.events = 'event1';
	        s.tl(s.pageURL, 'o', seoHierarchy);
        }
	});
});

// Code for tagging the links on RSS feeds signup page i.e. RSSLanding.aspx.
(function(A){
    A.fn.rssLinks=function(B){
        return this.each(function(){
            var E=A(this);
            var G=A.extend({},A.fn.rssLinks.defaults,B);
            var F=A(G.target||A(this).attr("target"));
            var C=[];
            G.url=G.url||this.tagName.toLowerCase()=="a"?A(this).attr("href").split("#")[0]:A(this).attr("rel");
            G.url=G.url.replace("http://","").replace("http%3A//","");
            for(var D=0;D<G.services.length;D++){
                C[D]='<a href="'+G.serviceList[G.services[D]].pattern+'" name="'+G.services[D]+'"><img src="'+G.serviceList[G.services[D]].image+'" alt="Add to '+G.services[D]+'" /></a>';
                C[D]=C[D].replace(/\{url\}/gi,escape(unescape(G.url)))
            }
            C=A(C.join(""));
            F[G.insertionType](C);
            if(G.wrapper){
                C.each(function(){
                    A(this).wrap(G.wrapper)
                })
            }
            if (G.taggingPattern && s && s.tl) {
                C.each(function(){
                    A(this).click(function(){
                    s.tl(this, "o", G.taggingPattern.replace(/\{name\}/gi, E.attr("name")).replace(/\{service\}/gi, A(this).attr("name")))
                    })
                })
            }
        })
     };
    A.fn.rssLinks.defaults={
        target:false,
        url:false,
        wrapper:false,
        insertionType:"html",
        taggingPattern:"keystoneresort:rss feeds:{name}:{service}",
        services:["my yahoo","my aol","google","windows live"],
        serviceList:{
            MyYahoo:{
                pattern:"http://us.rd.yahoo.com/my/atm/*http://add.my.yahoo.com/rss?url=http%3A//{url}",
                image:"/vailresorts/sites/snow/assets/img/rss/rss_yahoo.gif"
            },
            MyAOL:{
                pattern:"http://feeds.my.aol.com/?url=http%3A//{url}",
                image:"/vailresorts/sites/snow/assets/img/rss/rss_aol.gif"
            },
            Google:{
                pattern:"http://fusion.google.com/add?source=atgs&feedurl=http%3A//{url}",
                image:"/vailresorts/sites/snow/assets/img/rss/rss_google.gif"
            },
            WindowsLive:{
                pattern:"http://www.live.com/?add=http://{url}",
                image:"/vailresorts/sites/snow/assets/img/rss/rss_windows.gif"
            }
       }
   }
})(jQuery);
