/**
 * Global BAC scripts for integration w/ WordPress Theme.
 */
$(function() {
	// set login menu item to "My Account" if logged in
	// also set "Product Selection" menu item to display login panel if user is not logged in
	if (BAC.isUserLoggedIn) {
		$('#menu_item_11 a', '#tertNav').text('My Account').attr("href", BAC.url + "/account");
		
		// set product selection menu to open in new window
		//$('#menu_item_8 a', '#auxNav').attr('target', '_blank');
		
	} else {
		// bind login menu
		var lnk = $('#menu_item_11 a', '#tertNav').attr("href");
		var redirectUrl = window.location.href;
		redirectUrl = redirectUrl.replace(BAC.url, '');
		$('#menu_item_11 a', '#tertNav').attr("href", lnk + '?redirect_to=/english' + redirectUrl);
		// set product selection menu to display login panel
		//$('#menu_item_8 a', '#auxNav').attr("href", "/login?pss=1&redirect_to=/wp-content/themes/aircoil/api/requests/pss/prepare_for_user.php");
	}
	
	
	
	// country selection
	$('#jumpCountry').change(function() {
		var w = document.getElementById("jumpCountry").selectedIndex;
		var name = document.getElementById("jumpCountry").options[w].text;
		var val = document.getElementById("jumpCountry").options[w].value;
		$(this).parents('form:first').append('<input type="hidden" name="region" value="' + name + '"/>');
		$(this).parents('form:first').append('<input type="hidden" name="regionalSite" value="' + val + '"/>');
		$(this).parents('form:first').submit();
	});
	
	// rep search
	$('#zipSearch', '#loginFind').click(function() {
		var zip = $('#zipCode', '#loginFind').val();
		var permalink = $('#repSearchPermalink').val();
		window.location = permalink + '?zip=' + zip;
		return false;
	});
	
	// login bar script
	$('#loginButton', '#loginFind').click(function() {
		$(this).parent('form').submit();
		return false;
		/*var username = $('#loginName').val();
		var password = $('#loginPass').val();
		var submitButton = $(this);
		
		//$('#loginFind .loginBubble').animate({opacity:1}, 250, 'swing');
		$('#loginFind .loginBubble .loginText').html('Logging in...');
		$('#loginFind .loginBubble').slideDown(100);	
			
		$.post(BAC.secureURL + BAC.ajaxGateway, {action : 'login', 'username' : username, 'password' : password}, function(resp) {
			var data = eval('(' + resp + ')');	
			
			if (data.success) {
				window.location = BAC.accountUrl;
			} else {
				var error = 'Username/password incorrect.';
				if (data.error != '') error = data.error
				$('#loginFind .loginBubble .loginText').html(error);
				
			}
		});
		
		return false;*/
		
	});
	
	
	$('table#noCountryTable').css('display', 'none');
	$('table#noCountryTable').css('opacity', '0');
	$('table.locationInfo').css('display', 'none');
	$('table.locationInfo').css('opacity', '0');
	
	
	var sVal = '';
	
	$('select.locationMenu').change(changeLocation);

	
	$('p#backToTopBtn').click(function(){
		$.scrollTo('#header', 300);
		return false;
	}).stop();
	
	
	$('#country').change(function() {
		stateSwap('#state', '#country');
	});

		
	$("a[href*=.pdf]").click(function(){
		$(this).attr({"target":"_blank"});
		return true;
	});
});

function addBackToTop(xVal){		
	$('a#' + xVal).parents('p').css('display', 'block').fadeTo('slow', 1);
}
	

function changeLocation() {
	sVal = $('select.locationMenu').val();
	 	
	 	if (sVal == "US/Canada") {
			$('#countryInfo').css('display', 'none');
			$('#repZipSearch').css('display', 'block');
			$('#repZipSearch').fadeTo('300', 1);	 
			$('#noCountryTable').css('display', 'none');
			$('#noCountryTable').fadeTo('300', 0);
			$('.locationInfo').css('display', 'none');
			$('.locationInfo').fadeTo('300', 0);
	 		return;
	 	} else {
			$('#repZipSearch').css('display', 'none');
			$('#repZipSearch').fadeTo('300', 0);
	 	}
	 	
	 	$('table.locationInfo').fadeTo('300', 0);	 
		$('table.locationInfo').css('display', 'none');
		
		$('#countryInfo').css('display', 'none');
	 
		if(sVal == 'selectCountry'){
			return false;
		}else if (sVal == '') {
			$('#countryInfo').css('display', 'block');
			$('#noCountryTable').css('display', 'block');
			$('#noCountryTable').fadeTo('300', 1);	 
		}else{
			$('#countryInfo').css('display', 'block');
			$('#noCountryTable').css('display', 'none');
			$('#noCountryTable').fadeTo('300', 0);
			if (sVal) {
				$('a#' + sVal).parent().parent().parent().parent().css('display', 'block');
				$('a#' + sVal).parent().parent().parent().parent().fadeTo('300', 1);	 
			}
		}
	
		if (sVal) $.scrollTo('#' + sVal, 800,{offset: -125, onAfter:function(){addBackToTop('#' + sVal)}});
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}


//swap state dropdown with input text field for non-US/canada
function stateSwap(stateList, countryList) {
	var country = $(countryList).val();
	var realID = stateList.replace('#', '');
	if (country == "United States" || country == "Canada")
	{
	   if ($(stateList)[0].tagName !== "SELECT")
	   {
	       $(stateList + '-list').show();
		  $(stateList).remove();
		  $(stateList + '-list').attr("name", realID);
		  $(stateList + '-list').attr("id", realID);
	   }
	}
	else
	{
	    $(stateList).hide();
    	$(stateList).attr("name", realID + "-list");
   	    $(stateList).parent().append('<input type="text" name="' + realID + '" id="' + realID + '" />');	
   	    $(stateList).attr("id", realID + "-list");
	}
	
	

}


//get querystring var
function get(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

var popupAvailabilityCheck             = "";    // instance of 'Look Up' window
function popUpAvailabilityCheck()
{
	popupAvailabilityCheck = window.open('http://www.baltimoreaircoil.com/pss/web/getUserZipCode.php', 'popUpAvailabilityCheck', 'toolbar=no, scrollbar=yes, resizable=no, width=600, height=500');    
    
    // if window is already open, set focus
	if (window.focus) 
	{
		popupAvailabilityCheck.focus();
	}
}

/******************************************************************************
 * RESOURCE LIBRARY
 ******************************************************************************/
$(function() {
	$('#sp-searchtext', '#kcSearchContainer').keydown(function(event) {
		if (event.keyCode == 13) return false;	// disable enter key
	});
});

BAC.ResourceLibrary = new Object();

BAC.ResourceLibrary.updatePager = function () {
	var totalPages = $('#totalPages').val();
	var totalResults = $('#totalResults').val();
	var html = 'Page: ';
	var curPage = $('#page').val();
	for(i=1; i<=totalPages; i++) {
		html += '<em class="page"><a ';
		if (i == curPage) html += 'class="current" ';
		html += 'href="#">' + i + '</a></em>';
	}
	$('#pager').html(html);
	$('#pager em').click(function() {
		var pg = $(this).text();
		$('#page').val(pg);
		updateResources();
		return false;
	});
}
