﻿// Wait for the DOM to load before we execute...
//if(window.location.host != "trustedchoice"){
//    if(window.location.host != "www.trustedchoice.com"){
//		(window.location.host = "www.trustedchoice.com");
//	}
//}

$(function($) {
	var sPath = window.location.pathname.toLowerCase();
	var hidePanelMenu=false
	if(sPath.indexOf('/forms/')>0){
		hidePanelMenu=true;
	} else if(sPath.indexOf('lists/')>0){
		hidePanelMenu=true;
	} else if(sPath.indexOf('_layouts')>0){
		hidePanelMenu=true;
	}
	
  	if(hidePanelMenu==true){
		$("#s4-leftpanel").hide();
		$("#ia-pageMarginLocationLinks").css('margin-left', '10px');
		$("#MSO_ContentTable").css('margin-left', '0px');
	} else {
		$("#s4-leftpanel ul.ia-Tabs").tabs("#s4-leftpanel div.ia-Panes > div"); 
	}
	
	if($("#ctl00_sel_IADepartment")[0].innerText=='Home Page'){
			$("ul.ia-tooltip-items li a.ia-tooltip-link").tooltip({
			effect:'fade',
			fadeOutSpeed: 100,
			predelay:300,
			position:'center right',
			relative: true,
			offset: [160, 100]
		});

	} else {
		$("ul.ia-tooltip-items li a.ia-tooltip-link").tooltip({
			effect:'fade',
			fadeOutSpeed: 100,
			predelay:300,
			position:'center right',
			relative: true,
			offset: [0, 6]
		});
	}

	// select the topnav tab that matches the article //
	if($("#ctl00_sel_IAPageTypeID").text() == '1'){
		//$("#zz15_TopNavigationMenuV4 ul li:eq(0)").removeClass('selected').find("a").removeClass('selected');
		var vD = $("#ctl00_sel_IADepartment").text();
		if(vD == 'Auto Insurance'){
			$("#zz15_TopNavigationMenuV4 ul li:eq(3)").addClass('selected').find("a").addClass('selected');
		} else if(vD == 'Home / Apartments') {
			$("#zz15_TopNavigationMenuV4 ul li:eq(4)").addClass('selected').find("a").addClass('selected');
		} else if(vD == 'Life / Health') {
			$("#zz15_TopNavigationMenuV4 ul li:eq(5)").addClass('selected').find("a").addClass('selected');
		} else if(vD == 'Business') {
			$("#zz15_TopNavigationMenuV4 ul li:eq(6)").addClass('selected').find("a").addClass('selected');
		}
		$("div#ia-pageMarginLocationLinks a[href='/Content/Articles']").attr('href','#');
	}
	var vID = $("#ctl00_sel_IAListitemID").text();
	var vSl = ".ia-tooltip-items li[listID="+vID+"]"
	$(vSl).addClass('selected');
	
	


	// ***** VIMEO MOVIE - Start *****//
	// build the vimeo ifram within a vimeo-movie div based on link properties //
	$("a.vimeo-link").live('click',function(){
		$(this).after(VimeoMovieFrame($(this).attr('href').replace('#',''), $(this).attr('title'))); //build frame //
		$("div.vimeo-movie").overlay({top:260,	mask:{color:'#fff',loadSpeed:200,opacity:0.3},closeOnClick:false,load:true}); //play movie in overlay//
		$("a.close").attr('title','Close Window');
	});
	//when movie is closed remove div.vimeo-movie from page.//
	$("div.vimeo-movie .close").live( "click", function(event, ui) {
		$("div.vimeo-movie").remove();
	});
	// fucntion - returns required html //
	var VimeoMovieFrame = function(MovieID, MovieTitle) {
		var vFrm= "<div class='vimeo-movie' id='"+MovieID+"' title='"+MovieTitle+"'><iframe src='http://player.vimeo.com/video/"+MovieID+"?title=0&amp;byline=0&amp;portrait=0&amp;color=306A98' width='400' height='300' frameborder='0'></iframe></div>"
	   	return vFrm
	};
	// ***** VIMEO MOVIE - End *****//
	
	// ***** CHANGE BACKGROUND WHEN NOT BROWSING - Start *****
	$("#Ribbon li a").mouseup(function(){
		var vT = $(this).attr('title');
		if( vT == 'Browse'){
			$("#s4-bodyContainer").removeClass("ia-not-browsing");
		} else {
			$("#s4-bodyContainer").addClass("ia-not-browsing");
		}
	});
	// ***** CHANGE BACKGROUND WHEN NOT BROWSING - End *****

	// ***** ACCORDION FIND-AN-AGENT PROFILE - START *****
	// QUICK FIX - Agency name on profile page (should add this to webpart) //
	$("td.ia-buttonleft-cell").text($("table.tcAgencyDetail td:first").text()).css('font-weight','bold').css('padding-left','14px').css('font-size','16pt');
	// Accordion agent profile - first: hide h3 tags if div tag is empty// 
	$(".ia-accordion-items h3 + div").each(function() {
	 	if(this.innerHTML.length == 0){
        	$(this.previousSibling).hide();
        }
    });   
	// Accordion agent profile - second: implement accordion //
	$(".ia-accordion-items").tabs(".ia-accordion-items div.pane", {tabs: 'h3', effect: 'slide', initialIndex: 0});
	// ***** ACCORDION FIND-AN-AGENT PROFILE - End *****
	
   	// ***** DYNAMIC COPYRIGHT - START *****
    var vD = new Date()
    var vYear = vD.getFullYear();
    $("#iaPageCopyright").html("<div id='copyright'>Copyright &copy; "+vYear+" Trusted Choice, Inc. All rights reserved. No portion of this site may be reproduced in any manner without the prior written consent of Trusted Choice&reg;</div><div align='center' id='iacopyright'><a href='http://www.trustedchoice.com/content/terms-of-use.aspx' target='_blank'>Terms of Use</a>&nbsp;|&nbsp;<a href='http://www.trustedchoice.com/content/privacy-policy.aspx' target='_blank'>Privacy Policy</a></div>");
	// ***** DYNAMIC COPYRIGHT - END *****
	  	
   	// ***** FIND AN AGENT - START***** //
   	// FIND AN AGENT FORM - SEARCH LINK //
    $("#iaFAALink").live('mousedown', function(event) {
        var sVal = $("#iaQuoteByZip").val();
        if(sVal.endsWith('b')){
        	$(this).attr('href', "/content/Find-An-Agent/SearchResults.aspx?Criteria="+sVal.replace('b',''));
        } else {
        	$(this).attr('href', "/content/Find-An-Agent/DynamicResults.aspx?Criteria="+sVal);
        }

    });
     
    $("#iaQuoteByZip").keypress(function(event) {
     	if(event.which == 13) {
     		var sVal = $(this).val();
     		if(sVal.endsWith('b')){
        		$("#iaFAALink").attr('href', "/content/Find-An-Agent/SearchResults.aspx?Criteria="+sVal.replace('b',''));
        	} else {
        		$("#iaFAALink").attr('href', "/content/Find-An-Agent/DynamicResults.aspx?Criteria="+sVal);
        	}
			$("#iaFAALink img").click();
      	}
    });
 
    $("#iaQuoteByZip").focusin(function(){
      	$(this).css('background-color','#FFFFFF');
    });
     
    $("#iaFindAnAgentTable").focusout(function(){
     	if($(this).find("#iaQuoteByZip").val().length<5) {
     		$(this).find("#iaQuoteByZip").css('background-color','transparent').val('');
     	}
    });
	
	// FIND AN AGENT MENU - SEARCH LINK //
    //$("#zz1_TopNavigationMenun0").live('mouseover', mouseoverActionFindAnAgent)
    //$("#zz15_TopNavigationMenuV4 ul.root li:eq(0)").live('mouseover', mouseoverActionFindAnAgent)
	var vFAA = "<table id='iaFAAMenuSearchTable'><tr><td><input name='textfield' type='text' id='iaMenuQuoteByZip' width='70px'><a id='iaMenuFAALink' href='/content/find-an-agent/SearchResults.aspx'><img src='/_layouts/images/blank.gif'/></a></td></tr></table>"
	$("div.s4-tn ul.root li:eq(0) ul.dynamic li:eq(0) a").replaceWith(vFAA);

    
	$("#iaMenuFAALink").live('click', function(event) {
   		var sVal = $("#iaMenuQuoteByZip").val();
	    if(sVal.endsWith('b')){
       		$("#iaMenuFAALink").attr('href', "/content/Find-An-Agent/SearchResults.aspx?Criteria="+sVal.replace('b',''));
       	} else {
       		$("#iaMenuFAALink").attr('href', "/content/Find-An-Agent/DynamicResults.aspx?Criteria="+sVal);
       	}
   	});
   	
    $("#iaMenuQuoteByZip").live('keypress', function(event) {
     	if(event.which == 13) {
     		var sVal = $(this).val();
     		if(sVal.endsWith('b')){
        		$("#iaMenuFAALink").attr('href', "/content/Find-An-Agent/SearchResults.aspx?Criteria="+sVal.replace('b',''));
        	} else {
        		$("#iaMenuFAALink").attr('href', "/content/Find-An-Agent/DynamicResults.aspx?Criteria="+sVal);
        	}
			$("#iaMenuFAALink img").click();
      	}
    });

}); 


// ***** REQUIRED FUNCTIONS ***** //

// ***** START - Find-An-Agent menu item zipcode input ***** //
function mouseoverActionFindAnAgent(event) {
	if($("#ia-FindAnAgent").length < 1){
		//var vFAA = "<ul id='ia-FindAnAgent'><li><table id='ia-FAAMenuSearchTable'><tr><td><input name='textfield' type='text' id='iaMenuQuoteByZip' width='70px'><a id='iaMenuFAALink' href='/content/find-an-agent/SearchResults.aspx'><img src='/_layouts/images/blank.gif'/></a></td></tr></table></li></ul>"
		//$("td.ms-bannerContainer").append(vFAA);
		//$("#zz15_TopNavigationMenuV4").append(vFAA);
		//var vFAA = "<li><table id='ia-FAAMenuSearchTable'><tr><td><input name='textfield' type='text' id='iaMenuQuoteByZip' width='70px'><a id='iaMenuFAALink' href='/content/find-an-agent/SearchResults.aspx'><img src='/_layouts/images/blank.gif'/></a></td></tr></table></li>"
		//$("#zz15_TopNavigationMenuV4 ul.root li:eq(0) ul.dynamic li:eq(0) a").replaceWith(vFAA);
     };
	 $("ul#ia-FindAnAgent").show();
	 $("#zz1_TopNavigationMenun0").addClass("ms-topNavHover");
	 if($("#iaMenuQuoteByZip").val().length > 0) {
	 	$("#iaMenuQuoteByZip").css('background-color','#FFFFFF');
	 } else {
	 	$("#iaMenuQuoteByZip").css('background-color','transparent');
	 };
}

function mouseleaveActionFindAnAgent(event) {
    if(event.toElement.innerText != 'FIND AN AGENT'){
		$("#ia-FindAnAgent").hide();
		$("#zz1_TopNavigationMenun0").removeClass("ms-topNavHover");
	}
}
function mouseleaveMenuFindAnAgent(event) {
    if(event.toElement.id == 'ia-FAAMenuSearchTable'){
		$("#zz1_TopNavigationMenun0").addClass("ms-topNavHover");
	} else {
		$("#ia-FindAnAgent").hide();
		$("#zz1_TopNavigationMenun0").removeClass("ms-topNavHover");
	}
}
// ***** END - Find-An-Agent menu item zipcode input ***** //


// ***** Cookie - Start *****//
jQuery.cookie = function (key, value, options) {

    // key and value given, set cookie...
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
// ***** Cookie - End *****//







