﻿var debug = function(what){
	if(window.console && window.console.firebug){
		console.log(what);
	}	
	else{
		//alert(what);
	}
}		
	

debug('loading...');

	$(document).ready(function() {
	  debug('dom ready');
	  init();
	  setEventListeners();
	});


	var init = function() {
		
	  $(document).pngFix(); 	
		
	  displaySegment();
	  
	  rotateAds();// display random ads
	    
	  preloadImages();// preload branding bg images
	  	  
	  //toggleClassPlut.init('plutOverlay', 'newCustomer','existingCustomer','0');
	  
	  toggleClass.init('loginTabs','pyb','cs','0');

	  // Survey Monkey Link Takeover
  	  /*
	  $('a:not(".dontHijack")').click(function(ev){
		  ev.preventDefault();
		  debug('>>clicked a<<');
		  var $self=$(this);		  
		  $.ajax({
				type: "GET",
				url: "http://www.surveymonkey.com/jsPop.aspx?sm=z7RnRxSlFRC9Kwb85nnMiA_3d_3d",
				dataType: "script",
				error: function(){
				  debug('>>Error loading Script');
			    },
			    success: function(data){
				  debug(">>Script Loaded: " + data);
				  document.location = $self.attr('href');
		        }
		  });
	  });
	  */

	
	  urchinTracker();
	}
	
	var setEventListeners = function() {
		debug('set listeners');
		$("#navigation-residential-link-main").click(function(){debug('click res');menuchange('res_bundles');});
		//$("#navigation-business-link-main").click(function(){debug('click bus');menuchange('business');});
		//$("#navigation-wholesale-link-main").click(function(){debug('click who');menuchange('wholesale');});
		
		$("#Business .mainNavNormal").click(function(){window.location = $('#navigation-business-link-main').attr('href');});
		$("#Business .mainNavHover").click(function(){window.location = $('#navigation-business-link-main').attr('href');});
		
		$("#csOffLink").click(function(){toggleClass.toggle('1');});
		$("#pybOffLink").click(function(){toggleClass.toggle('0');});
		$("#gsOffLink").click(function(){toggleClass.toggle('0');});
						
		$('#existingCustomerOffLink').click(function(){debug('existing clicked');loginswitch();});
		$('#newCustomerOffLink').click(function(){debug('new clicked');loginswitchback();});
		
		$("input").focus(function () {
        	$(this).addClass('hasFocus');
    	});

		$("input").blur(function () {
        	$(this).removeClass('hasFocus');
    	});

		
	}
	

var displaySegment = function(){
	debug('displaySegment()');
	var segment = $.query.get('segment');
	debug('segment = ' + segment );
	if($.query.get('segment')){	
		switch (segment)	
		{      
			case 'residential':
			    debug('- segment residential');
				menuchange('res_bundles');
				break;
			case 'business':
			    debug('- segment business');
				menuchange('business');
				break;
			case 'wholesale':
			    debug('- segment wholesale');
				menuchange('wholesale');
				break;
			default:
			  debug('- segment default');
				break;
		}
	} else {
		nextRotation('res_bb');//start rotation onload	
	}
	
	
}	
	
      //preload images
	var  preloadImages = function(){	
 		    //existing user white bg
		    var plutWhtBG= new Image(214,183); 
		        plutWhtBG.src="images/Default/plutexistingBg.gif"; 
			   			   				   
            //main BG  
            var resBG_bb= new Image(956, 274);
                resBG_bb.src ="/images/Default/mainBg_res_bb.jpg";

            var resBG_ph= new Image(956, 274);
                resBG_ph.src ="/images/Default/mainBg_res_ph.jpg";
               
            var resBG_tv= new Image(956, 274);
                resBG_tv.src ="/images/Default/mainBg_res_tv.jpg";               
               
            var resBG_tv= new Image(956, 274);
                resBG_tv.src ="/images/Default/mainBg_res_bundle.jpg";    
               
            var smbBG= new Image(956, 274);
                smbBG.src ="/images/Default/mainBg_business.jpg"; 
		}    
    
      var rotateAds = function() {                    
            var AdAArray = new Array('RotateA', 'RotateA1', 'RotateA2', 'RotateA3', 'RotateA4');            
            var AdBArray = new Array('RotateB', 'RotateB1', 'RotateB2');
			var AdCArray = new Array('RotateC', 'RotateC1', 'RotateC2');
 
            var AdArrayArray = new Array(AdAArray, AdBArray, AdCArray);

            
            for (i=0; i<AdArrayArray.length;i++){
              var t = 1 + parseInt(Math.floor(Math.random()*(AdArrayArray[i].length - 1)));
              //console.log('t:' + t);
              var adToDisplay = 0;
              for (j=0; j<AdArrayArray[i].length;j++){
                toggleAd(AdArrayArray[i][j], (j == t));         
              }
            } 
        }   
        
    
        function toggleAd(ad, isVisible){
            if(ad != 'Placeholder')
            {
				ad = '#' + ad;
                elm = $(ad);
                if (isVisible) {
                  elm.css('display','inline');
                  elm.css('position','relative');
                } else {
                  elm.css('display','none');
                  elm.css('position','absolute');
                }
            }
        }

		//Branding top Navigation LI
		var TopNavLIResidential = $("#Residential");
		var TopNavLIBusiness = $("#Business");
		var TopNavLIWholesale = $("#Wholesale");
		  var BrandingTopNavigation = [TopNavLIResidential, TopNavLIBusiness, TopNavLIWholesale];
						
		//Branding top Navigation Links
		 var TopNavLinkResidential = $("#navigation-residential-link-main");
		 var TopNavLinkBusiness = $("#navigation-business-link-main");
		 var TopNavLinkWholesale = $("#navigation-wholesale-link-main");
		   var BrandingTopNavigationLink = [TopNavLinkResidential, TopNavLinkBusiness, TopNavLinkWholesale];
		 
		//Branding background image
		var mainBG = $("#HTMLBody");
		
		//Branding Left Navigation
		var LeftNavResidential = $("#typeNavRes");
		var LeftNavResidentialOptions = $("#typeNavResOptions");
		var LeftNavBusiness = $("#typeNavSmb");
		var LeftNavWholesale = $("#typeNavLrb");
		var BrandingLeftNavigation = [LeftNavResidential, LeftNavBusiness, LeftNavWholesale];			
								
		// Branding content
		var residentialbundles = $("#residential-bundles");  			
		var residentialinternet = $("#residential-internet");
		var residentialphone = $("#residential-phone");
		var residentialtv = $("#residential-tv");
		var BusinessContent = $("#business-small");
		var WholesaleContent = $("#business-enterprise");
		  var BrandingContent = [residentialbundles, residentialinternet, residentialphone, residentialtv, BusinessContent, WholesaleContent];
		
		// Branding Residential Rotating Links
		var bundleslink = $("#navigation-residential-bundles");		
		var internetlink = $("#navigation-residential-internet");
		var phonelink = $("#navigation-residential-phone");
		var tvlink = $("#navigation-residential-tv");			
		  var BrandingResidentialLinks = [bundleslink, internetlink, phonelink, tvlink];
		
		// Branding PLUT overlay
		var plut = $("#feature-highlight");	
				
		//Green Tabbed Content
		var tabbedBox = $("loginTabs");
		
		 var pybTabOnResidential = $("#pybOnLink");
		 var pybTabOnBusiness = $("#gsOnLink");
		   var pybOnTabs = [pybTabOnResidential, pybTabOnBusiness];
		 
		 var pybTabOffResidential = $("#pybOffLink");
		   var pybTabOffBusiness = $("#gsOffLink");
		   var pybOffTabs = [pybTabOffResidential, pybTabOffBusiness];
		 
		 
		 var pybContentResidential = $("#pybBdyResidential");
		 var pybContentBusiness = $("#pybBdyBusiness");
		   var pybContent = [pybContentResidential, pybContentBusiness];
					
		// Ad Row			
		var adsResidential = $("#adsResidential");
		var adsBusiness = $("#adsBusiness");
		  var ads = [adsResidential, adsBusiness];
		  
		 var adThreeRes = $("#adThreeRes");
		 var adThreeWholesale = $("#adThreeLrb");
		   var adThree = [adThreeRes, adThreeWholesale];


		function menuchange(tab, rotation) {
			
	      debug('menuchange (' + tab + ',' + rotation +')');

			debug('end of vars');
			
			var focusedInput = $('.hasFocus');
			
			switch (tab) 
			{      
				case 'res_bb':
					updateClassName(BrandingTopNavigation, TopNavLIResidential, 'inactive', 'active');
					 updateClassName(BrandingTopNavigationLink, TopNavLinkResidential, 'inactive', 'active');
					
					mainBG.removeClass('residentialPhoneBG').removeClass('residentialTVBG').addClass('residentialBBBG').removeClass('residentialBundlesBG').removeClass('businessBG');
				    //mainBG.attr("style", "background-image:url(/images/Default/mainBg_res_bb.jpg);_background:transparent url(/images/Default/mainBg_res_bb.jpg) left top no-repeat !important;");
                     mainBG.removeClass('brandingBusiness');
					 plut.css("display","block");	
					 
					updateCSS(BrandingLeftNavigation, LeftNavResidential, 'display', 'none', 'block');
					 LeftNavResidentialOptions.css("display","inline");				
									
					updateCSS(BrandingContent, residentialinternet, 'display', 'none', 'block');
		             updateClassName(BrandingResidentialLinks, internetlink, 'inactive', 'active');
			
					updateCSS(pybOnTabs, pybTabOnResidential, 'display', 'none', 'block');  
					updateCSS(pybOffTabs, pybTabOffResidential, 'display', 'none', 'block');  
					updateCSS(pybContent, pybContentResidential, 'display', 'none', 'block');				
					
					updateCSS(ads, adsResidential, 'display', 'none', 'block'); 
					 updateCSS(adThree, adThreeRes, 'display', 'none', 'block');	
						
				    if(rotation){
				       nextRotation('res_ph');
				     } else {
				       stopRotation();
				     }
				     $("#marketSegment").value = 3;
				    
					break;
					
				case 'res_ph':
					updateClassName(BrandingTopNavigation, TopNavLIResidential, 'inactive', 'active');
					 updateClassName(BrandingTopNavigationLink, TopNavLinkResidential, 'inactive', 'active');
			      
				    mainBG.addClass('residentialPhoneBG').removeClass('residentialTVBG').removeClass('residentialBBBG').removeClass('residentialBundlesBG').removeClass('businessBG');
				    //mainBG.attr("style", "background-image:url(/images/Default/mainBg_res_ph.jpg);_background:transparent url(/images/Default/mainBg_res_ph.jpg) left top no-repeat !important;");
                     mainBG.removeClass('brandingBusiness');
                     plut.css("display","block");

					updateCSS(BrandingLeftNavigation, LeftNavResidential, 'display', 'none', 'block');
					 LeftNavResidentialOptions.css("display","inline");
	
					updateCSS(BrandingContent, residentialphone, 'display', 'none', 'block');
					 updateClassName(BrandingResidentialLinks, phonelink, 'inactive', 'active');
									
					updateCSS(pybOnTabs, pybTabOnResidential, 'display', 'none', 'block');  
					updateCSS(pybOffTabs, pybTabOffResidential, 'display', 'none', 'block');   
					updateCSS(pybContent, pybContentResidential, 'display', 'none', 'block');				
					
					updateCSS(ads, adsResidential, 'display', 'none', 'block'); 
					 updateCSS(adThree, adThreeRes, 'display', 'none', 'block');	
	
				    if(rotation){
				       nextRotation('res_tv');
				     } else {
				       stopRotation();
				     }
				     $("#marketSegment").value = 3;
				    
					break;
					
				case 'res_tv':
	                updateClassName(BrandingTopNavigation, TopNavLIResidential, 'inactive', 'active');
					 updateClassName(BrandingTopNavigationLink, TopNavLinkResidential, 'inactive', 'active');
			      
				    mainBG.removeClass('residentialPhoneBG').addClass('residentialTVBG').removeClass('residentialBBBG').removeClass('residentialBundlesBG').removeClass('businessBG');
				    //mainBG.attr("style", "background-image:url(/images/Default/mainBg_res_tv.jpg);_background:transparent url(/images/Default/mainBg_res_tv.jpg) left top no-repeat !important;");
                     mainBG.removeClass('brandingBusiness');
                     plut.css("display","block");

					updateCSS(BrandingLeftNavigation, LeftNavResidential, 'display', 'none', 'block');
					 LeftNavResidentialOptions.css("display","inline");
	
					updateCSS(BrandingContent, residentialtv, 'display', 'none', 'block');
					 updateClassName(BrandingResidentialLinks, tvlink, 'inactive', 'active');
									
					updateCSS(pybOnTabs, pybTabOnResidential, 'display', 'none', 'block');  
					updateCSS(pybOffTabs, pybTabOffResidential, 'display', 'none', 'block');  
					updateCSS(pybContent, pybContentResidential, 'display', 'none', 'block');					
					
					updateCSS(ads, adsResidential, 'display', 'none', 'block'); 
					 updateCSS(adThree, adThreeRes, 'display', 'none', 'block');	
					 
				    if(rotation){
				       nextRotation('res_bundles');
				     } else {
				       stopRotation();
				     }
				     $("#marketSegment").value = 3;
				    
					break;	
																
				case 'res_bundles':
					debug('case: bundles');
					updateClassName(BrandingTopNavigation, TopNavLIResidential, 'inactive', 'active');
					 updateClassName(BrandingTopNavigationLink, TopNavLinkResidential, 'inactive', 'active');
					 
			       mainBG.removeClass('residentialPhoneBG').removeClass('residentialTVBG').removeClass('residentialBBBG').addClass('residentialBundlesBG').removeClass('businessBG');
				    //mainBG.attr("style", "background-image:url(/images/Default/mainBg_res_bundle.jpg);_background:transparent url(/images/Default/mainBg_res_bundle.jpg) left top no-repeat !important;");
                     mainBG.removeClass('brandingBusiness');
                     plut.css("display","block");

					updateCSS(BrandingLeftNavigation, LeftNavResidential, 'display', 'none', 'block');
					 LeftNavResidentialOptions.css("display","inline");
	
					updateCSS(BrandingContent, residentialbundles, 'display', 'none', 'block');
					 updateClassName(BrandingResidentialLinks, bundleslink, 'inactive', 'active');
									
					updateCSS(pybOnTabs, pybTabOnResidential, 'display', 'none', 'block');  
					updateCSS(pybOffTabs, pybTabOffResidential, 'display', 'none', 'block');   
					updateCSS(pybContent, pybContentResidential, 'display', 'none', 'block');					
					
					updateCSS(ads, adsResidential, 'display', 'none', 'block'); 
					 updateCSS(adThree, adThreeRes, 'display', 'none', 'block');	
					 
					stopCarouselTimer(); //Stop business image rotator
					 
				    if(rotation){
				       nextRotation('res_bb')
				     } else {
				       stopRotation();
				     }
				     $("#marketSegment").value = 3;
				     
					break;	
					
				case 'business':
						menuchange('res_bundles', true);
					break;
					
				case 'wholesale':
						menuchange('res_bundles', true);
				    break;
				
				default:
				
					debug('case: default : ' + tab);
					
					break;
			}
			
			if(focusedInput){
				focusedInput.focus();	
			}
			
			//alert('defaultJS: ' + $("#marketSegment").value);
		}
		
		var t;
		
		function nextRotation(tab) {
		    //console.log('nextRotation');
		    stopRotation();  
		    var nextCall = "menuchange('" + tab + "',true)";
		    t=setTimeout(nextCall,7000);  
		}
		
		function stopRotation(){
		    if(t) {
		        clearTimeout(t);
		    }   
		}
		
		
		function updateCSS (elementArray, selectedElement, elementCSS, defaultValue, selectedValue)
		{
			debug('updateCSS :' + selectedElement.id );
			
			for(i=0;i < elementArray.length; i++)
			{
			  elementArray[i].css(elementCSS, defaultValue);
			}
			if (selectedElement !== null)
			{
			  selectedElement.css(elementCSS, selectedValue);
			}
			return true;	
		}
		
		function overwriteClassName(elementArray, selectedElement, defaultValue, selectedValue)
		{
			for(i=0;i < elementArray.length; i++)
			{
			  elementArray[i].className = defaultValue;
			}
			if (selectedElement !== null)
			{
			  selectedElement.className = selectedValue;
			}
			return true;	
		}
		
	
		function updateClassName(elementArray, selectedElement, defaultValue, selectedValue)
		{
			for(i=0;i < elementArray.length; i++)
			{
			  elementArray[i].removeClass(selectedValue);
			}
			if (selectedElement !== null)
			{
			  selectedElement.addClass(selectedValue);
			}
			return true;	
		}	
		
	

            var intRand = rndNumber = Math.floor(Math.random()*2);
            var strFlash = "";
        	
            switch (intRand){
                case 0:
	                strImg = '<a href="/residential/equipment.aspx?tab=2&anc=lmc"><img src="/images/cta/homeCarsCallout.gif" alt="Talk in Style" width="231" height="90" border="0"/></a>';
	                break;
                case 1:
	                strImg = '<a href="/bigairjump/"><img src="/images/cta/homeJumpTheTruck.gif" alt="Jump The Truck" width="231" height="90" border="0"/></a>';
	                break;
                default:
	                strImg = '<a href="/residential/equipment.aspx?tab=2"><img src="/images/cta/homeCarsCallout.gif" alt="Talk in Style" width="231" height="90" border="0"/></a>';
	                break;
            }
        	
            function chkFormBG() {

            }

	        function ValidateBillPayLogin() {
		        if (($("#UserIDTextBox").value == "") || ($("#PasswordTextBox").value == "")) {
			        alert("You must enter a User ID and Password to continue.");
			        return false;
		        } else {
			        return true;
		        }
	        }

    function autotab(current, to) {
        if (current.getAttribute &&
                current.value.length == current.getAttribute("maxlength")) {
            to.focus()
        }
    }

    function loginswitch() {
		debug('loginswitch');
        document.getElementById('newCustomer').style.display = 'none';
        document.getElementById('existingCustomer').style.display = 'block';
        document.getElementById('plutOverlay').className = 'existingCustomer';
    }

    function loginswitchback() {
		debug('loginswitchback');
        document.getElementById('newCustomer').style.display = 'block';
        document.getElementById('existingCustomer').style.display = 'none';
        document.getElementById('plutOverlay').className = 'newCustomer';
    }

    function validatePlut() {
        if (document.getElementById('existingCustomer').style.display == 'none') {
            return ValidateCityState();
        } else if (document.getElementById('newCustomer').style.display == 'none') {
        return ValidateLogin();
        } else {
            //console.log('!');
            return false;
        }
    }

    function ValidateCityState() {
        if (document.getElementById('LookupForm1_StateDropDownList').selectedIndex == 0) {
            alert("You must select a city and state to proceed.");
            return false;
        } else {
            return true;
        }
    }

    function ValidateLogin() {
        if (document.getElementById('LookupForm1_BillAmount').value == '' || document.getElementById('LookupForm1_NPA').value == '' || document.getElementById('LookupForm1_NXX').value == '' || document.getElementById('LookupForm1_Station').value == '') {
            alert("Please enter phone number and bill amount.");
            return false;
        } else {
            return true;
        }
    }
	