$(document).ready(function () {
    preloadImages();

    $("#subPage #marquee ul li").bind("click", changeImage);
    $("#subPage #marquee ul li:first").trigger("click");

    $("#home .services .serviceWrapper.refer").bind("click", function () { window.location = "https://ww2.cox.com/business/referral-lead-generation.cox"; });
    $("#home .services .serviceWrapper.contact").bind("click", function () { window.location = "small/contact/"; });
    $("#subPage .services .serviceWrapper.phone").bind("click", function () { window.location = "../small/phone/voicemanager/"; });
    $("#subPage .services .serviceWrapper.internet").bind("click", function () { window.location = "../small/internet/businessinternet/"; });
    $("#subPage .services .serviceWrapper.tv").bind("click", function () { window.location = "../small/tv/packages/"; });
    $("#subPage .services .serviceWrapper.contactsmall").bind("click", function () { window.location = "../small/contact/"; });
    $("#subPage .services .serviceWrapper.servicesTout").bind("click", function () { window.location = "mediumlarge/services/coxopticalinternet/"; });
    $("#subPage .services .serviceWrapper.network").bind("click", function () { window.location = "mediumlarge/network/nationalbackbone/"; });
    $("#subPage .services .serviceWrapper.experts").bind("click", function () { window.location = "../mediumlarge/experts/"; });
    $("#subPage .services .serviceWrapper.contactlarge").bind("click", function () { window.location = "mediumlarge/experts/"; });

    $("#home .catWrapper.small").bind("click", function () { window.location = "small/"; });
    $("#home .catWrapper.medlarge").bind("click", function () { window.location = "mediumlarge/"; });

    $("#subPage .subNav nav ul.outerNav li").not(".seperator").not("ul.innerNav li").bind("mouseenter", openNav).bind("click", openNav);
	
	$("#subPage .subNav nav ul.outerNav li").not(".seperator").not("ul.innerNav li").bind("mouseleave", closeNav);
	
    $("#subPage .subNav nav ul.outerNav li ul.innerNav li").bind("click", openNav);

    $("#subPage .copy .bio a").not(".mailto").bind("click", showBio);
    $("#subPage .copy p .details").bind("click", showBio);
    $("#subPage .copy .list .details").bind("click", showBio);

	$("#home .services .serviceWrapper .service .inputWrapper #zip").attr("value", "Zip Code");
	$("header .headerRight .search input#searchBox").attr("value", "SEARCH");
	
	$("#subPage .copy .pdfs .pdf").bind("mouseenter", function(){$(this).find(".text").css("color", "#edae3f");});
	$("#subPage .copy .pdfs .pdf").bind("mouseleave", function(){$(this).find(".text").css("color", "#2575a7");});
	
	$("#subPage .copy .pdf.dropout").bind("click", pdfDropOut);
	
	$("header .headerRight .search a").bind("click", submitSearch);
	
	$("input[type=text]").keydown(function(e) {
        if (e.keyCode == 13){
            submitSearch(null);
		}
    });
	
	//open pdf in new windows
	$('.pdfLinks').attr('target', '_new');
	
    $('a.back').click(function(){
			parent.history.back();
			return false;
    });
	
	checkURL();
	
	 //Page Tabs
    if($('#tabInfo').length) {
		pageTabs();
    } //end if

	 //Page Tabs
    if($('#networkMap').length) {
		networkMap();
    } //end if
	
    adjustSidebar();
});

var currentImage;
var timer;

function preloadImages(){
	var imageCache = new Array();
	var images = $("#subPage #marquee ul li");
	
	for (var i=0; i < images.length; i++){
		var cacheImage = document.createElement('img');
		cacheImage.src = $(images[i]).children('a').attr("href");
		imageCache.push(cacheImage);
	}
	
	currentImage = 0;
}

function changeImage(e){
	if ($(this).hasClass("selected")) return false;
	
	var prevImage = $("#marquee .imageMask").find("img");
	
	$("#subPage #marquee ul li").removeClass();
	$(this).addClass("selected");
	
	var newImage = $('<img/>').attr('src',$(this).find("a").attr("href")).attr("rel",$(this).find("a").attr("rel")).addClass("roundedtopleftbottomright");
	$("#marquee .imageMask").prepend(newImage);
	
	currentImage = $(this).index();
	
	
	$("#marquee .imageMask").stop().animate({opacity:'0'},500,function(){prevImage.remove();$("#marquee .imageMask").animate({opacity:'1'},500);});
	
	if(newImage.attr("rel") != ""){
		newImage.bind("click", function(){window.location = newImage.attr("rel");});
	}
	
	e.preventDefault();
	
	autoRotate();
}

function autoRotate(){
	clearTimeout(timer);
	var newIndex = currentImage + 1;
	if(newIndex > ($("#subPage #marquee ul li").length - 1)){
		newIndex = 0;
	}
	
	timer = setTimeout(function(){ $("#subPage #marquee ul li").eq(newIndex).trigger("click");autoRotate();}, 5000);
}

function clearInputText(input) {
	if(input.value == $(input).attr("alt")){
		$(input).attr("alt", input.value);
		input.value = "";
	}
}

function returnInputText(input) {
	if(input.value == ""){
		input.value = $(input).attr("alt");
	}
}//returnInputText

function closeNav(){

	$("#subPage .subNav nav ul.outerNav li").css("backgroundColor", "transparent");
	$("#subPage .subNav nav ul.outerNav li").find("a").not(".on .navHeader").css("color", "#2575a7");
	$("#subPage .subNav nav ul.outerNav li").find("a").not(".on .navHeader").hover( function() {
			$(this).not('.navHeader').css("color", "#d96406");
		},
		function() {
			$(this).not('.navHeader').css("color", "#2575a7");
	});//li hover

	$(".innerNav").css("display", "none");

	$(document).unbind("click", closeNav);

}//closeNav

function openNav(e){

	closeNav();

	e.preventDefault();

	if($(this).children(".innerNav").children().size() > 0){
		$(this).find("a").css("cursor", "default");
		$(this).find("ul li a").not('.navHeader').css("cursor", "pointer");
		$(this).css("backgroundColor", "#1e4660");
		$(this).find("a").not(".on .navHeader").css("color", "#ffffff");

		$(this).find("a").not(".on .navHeader").hover(function() {
			$(this).not('.navHeader').css("color", "#d96406");
		}, 
		function() {
			$(this).css("color", "#ffffff");
		});

		var pos = $(this).position();
		
		$(this).find(".innerNav").css("top", pos.top + 25 + "px");
		$(this).find(".innerNav").css("left", pos.left + "px");
		$(this).find(".innerNav").css("display", "block");

		setTimeout(function(){$(document).bind("click", closeNav);}, 10);

	} else if(e.type != "mouseenter") {

 		if ($(this).children('.newWindow').length > 0) {

			window.open('http://ww2.cox.com/business/arizona/shop/new-offer.cox?sc_id=AZB_smlbiznav_azwebsite_BIZB_bzside_z&pc=CBSAVE&zip=85015');

		} else { 
			window.location = $(this).find("a").attr("href");
		}
	}else{
		
	}

}//openNav

function adjustSidebar(){
	var newHeight = $("#subPage .contentWrapper").height() - 55;
	$("#subPage .sideBar").css("height", newHeight + "px");
}

function showBio(e){
	e.preventDefault();
	
	if($(this).html() == "Terms and Conditions &gt;" || $(this).html() == "Terms and Conditions >"){
		$(this).html("Close");
		$(this).parent().find(".desc").css("display", "block");
		$(this).addClass("terms");
	}else if($(this).html() == "Read bio &gt;" || $(this).html() == "Read bio >"){
		$(this).html("Close");
		$(this).parent().find(".desc").css("display", "block");
		$(this).addClass("bioLink");
	}else if($(this).html() == "For promotional details, click here."){
		$(this).html("Close");
		$(this).parent().find(".desc").css("display", "block");
		$(this).addClass("promo");
	}else if($(this).html() == "Close"){
		$(this).parent().find(".desc").css("display", "none");
		if($(this).hasClass("terms")){
			$(this).html("Terms and Conditions &gt;");
		}else if($(this).hasClass("bioLink")){
			$(this).html("Read bio &gt;");
		}else if($(this).hasClass("promo")){
			$(this).html("For promotional details, click here.");
		}
	}
	
	adjustSidebar();
}

function checkZip(zipBox){
	var zip = zipBox.value;
	if(zip.length == 5){
		if(zipcodes.indexOf(zip) > -1){
			createPopup(zipBox, "We're in your area!", "Let's talk about your needs. You can <a href=\"mediumlarge/contact/\">call us</a>, <a href=\"mailto:azcoxbiz@cox.com\">email us</a>, or <a href=\"http\:\/\/as00\.estara.com\/UI\/gui\.php\?accountid\=200106292143\&template\=362291\&var2\=PHX\&var3\=switchffe\&calltype\=webvoicepop\&linkfile\=%2FOneCC%2F200106292143%2F362291.js\&referrer\=Email&donotcache\=1354272048\&emaillink\=1\&upeid\=Hy9UuhBXEd-t2wAjfe1fzg\&guiid\=43f2de8a9637b\&timestamp\=1265155221\">click here</a> and we'll call you.");
		}else if(zipcodes.indexOf(zip) == -1){
			createPopup(zipBox, "We're sorry.", "The Cox Business Arizona network does not reach your area at this time.");
		}
	}else{
		$("#popup").css("opacity", "0");
		$("#popup").css("display", "none");
	}
}

function createPopup(item, title, copy){
	$("#popup").remove();
	$("#mainWrapper").append("<div id=\"popup\"><div class=\"content roundedtopleftbottomright\"><h3>" + title + "</h3><p>" + copy + "</p></div><div class=\"triangle\"></div></div>");
	
	var pos = $(item).offset();
	$("#popup").css("top", pos.top - $("#popup").height() + 10);
	$("#popup").css("left", pos.left);
	$("#popup").css("display", "block");
	
	$("#popup").animate({opacity:1}, 500);
	
	setTimeout(function(){$(document).bind("click", function(){
		$("#popup").animate({opacity:0}, 500, function(){
			$("#popup").css("display", "none");
			$(document).unbind("click");
		});
	});}, 10);
}

function pdfDropOut(e){
	e.preventDefault();
	if(!$(this).hasClass("open")){
		var dropout = $(this).find("a").attr("href") + "Hidden";
		$(this).parent().find("." + dropout).css("display", "block");
		$(this).addClass("open");
		
		if($(this).find("a").attr("href") == "cortelco"){
			$(this).find("a").html("<div class=\"text\">Hide Cortelco Guides ></div>");
		}else if ($(this).find("a").attr("href") == "lineup"){
			$(this).find("a").html("<div class=\"text\">Hide Channel Lineup Guides ></div>");
		}
	}else{
		var dropout = $(this).find("a").attr("href") + "Hidden";
		$(this).parent().find("." + dropout).css("display", "none");
		$(this).removeClass("open");
		
		if($(this).find("a").attr("href") == "cortelco"){
			$(this).find("a").html("<div class=\"text\">Show Cortelco Guides ></div>");
		}else if ($(this).find("a").attr("href") == "lineup"){
			$(this).find("a").html("<div class=\"text\">Show Channel Lineup Guides ></div>");
		}
	}
}

function submitSearch(e){
	if(e != null){
		e.preventDefault();
	}

	if(document.getElementById("searchBox").value != "SEARCH" && document.getElementById("searchBox").value != ""){
		window.location = $("header .headerRight .search a").attr("href") + "?q=" + document.getElementById("searchBox").value;
	}
}

function getQueryVariable(variable) { 
	var query = window.location.search.substring(1); 
	var vars = query.split("&"); 
	for (var i=0;i<vars.length;i++) { 
		var pair = vars[i].split("="); 
		if (pair[0] == variable) { 
			return pair[1]; 
		} 
	} 
} 
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}


function pageTabs() {
	//hide all but first tab info section
    $('#tabInfo>ul>li:first').siblings().hide();
	$('#tabs li').addClass('roundedtoprighttopleft');

	//check to see if the hash tag in the URL is for pressreleases and show prs
	if (window.location.hash == '#pressRelease') {
			$('#tabInfo>ul>li').eq(0).hide();
			$('#tabs li').removeClass('active');
			$('#tabInfo>ul>li').eq(1).show();
			$('#tabs li').eq(1).addClass('active');
	} 
	
    //click tab and you get the corresponding info
    $('#tabs li').live('click', function(){
		var index = $('#tabs li').index(this);

		$('#tabInfo>ul>li').hide();
		$('#tabs li').removeClass('active');
		$('#tabInfo>ul>li').eq(index).show();
		$('#tabs li').eq(index).addClass('active');

		adjustSidebar();

		return false;
    }); // end tabs bind
}

function checkURL() { 
	//check to see if the pressrelease hash tag is in the url and then display press release tab
	// if($('#newsCheck').length) {
				//console.log(window.location.hash);
		if (window.location.hash == '#pressRelease') {
			console.log(window.location.hash);
			$('#tabInfo>ul>li').eq(0).hide();
			$('#tabs li').removeClass('active');
			$('#tabInfo>ul>li').eq(1).show();
			$('#tabs li').eq(1).addClass('active');
		}
	 //}
}//checkurl

function networkMap() { 
	$('#mapLegend li').live('click', function(){
		var mapImg = $(this).attr('class');
		$('#networkMap img').stop(true, true).fadeTo('slow', 0.5).stop(true, true).fadeTo('slow', 1).attr('src', '../../../assets/images/special/map/' + mapImg +'.gif');
		return false;
		}); // end live
}
	
