// set default monthly rate, which may be re-set through Test&Target
var rate = '$15.99';

if (getCookie('Visitor') && getCookie('Visitor') != '' && getCookie('Visitor') != null) {
	var visitorID = getCookie('Visitor');
} else {
	var rndmNum = Math.floor(Math.random()*100);
	if (rndmNum<10) {
		var rndmID = '0'+rndmNum+'';
	} else {
		var rndmID = rndmNum+'';
	}
	var visitorID = Math.round(new Date().getTime()) + rndmID;
	var expdate = new Date();
	expdate.setTime (expdate.getTime() + 7776000000); // 90 days
	setCookie('Visitor', visitorID, expdate, '/', '.stamps.com', false);
}

function getDemoDimensions() {
	if (screen.width > 1366 && screen.height > 800) {
		videoHeight = 351; //702;
		videoWidth = 624; //1248;
	} else if (screen.width > 1366) {
		videoHeight = 351; //526.5;
		videoWidth = 624; //936;
	} else if (screen.width > 800 && screen.width <= 1366 && screen.height > 768) {
		videoHeight = 351; //526.5;
		videoWidth = 624; //936;
	} else {
		videoHeight = 351;
		videoWidth = 624;
	}
	
	var notChrome = ( $.browser.safari && /chrome/.test(navigator.userAgent.toLowerCase()) ) ? false : true;
	if (notChrome == false) {
		videoHeight = videoHeight + 6;
	}
	
	if ($.browser.safari == true && notChrome == true) {
		videoHeight = videoHeight + 4;
	}
}

function startEagleEyeDownload() {
	window.location = '/download/us/cab/stamps/EagleEye.exe';
}

if (typeof(identifier) != 'undefined') {
	if (identifier == 'Shipping') {
		setCookie('TabCookie', 'Shipping', null, '/');
	}
	if (identifier == 'Activation') {
		setCookie('Activation', true, null, '/');
	}
	if (identifier == 'PostageMeters') {
		setCookie('TabCookie', 'PostageMeters', null, '/');
	}
	if (identifier == 'PostOffice') {
		setCookie('TabCookie', 'PostOffice', null, '/');
	}
}

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function nextPageProShipPlan() {
	document.location = '/registration/?PromoCode=FREE1500';
}

function nextPageProShipPlan500() {
	document.location = '/registration/?PromoCode=FREE500';
}

try {
	window.external.IsSDCClient();
	if (identifier == 'download') {
		document.write('<link rel="stylesheet" type="text/css" href="/site/css/global_clientDownload.css" media="all">');
	} else {
		document.write('<link rel="stylesheet" type="text/css" href="/site/css/global_client.css" media="all">');
	}
} catch (ex) {
}

function getChatLandingURL() {
	var URL = "http://stamps.custhelp.com/app/chat/chat_launch";
	return URL;
}

var chatWindow = [];

function launchChat() {
	if (navigator.userAgent.indexOf('Chrome/') > 0) {
		if (window.chatWindow[0]) {
			window.chatWindow[0].close();
			window.chatWindow = [];
		}
	}
	if (typeof chatWindow[0] != 'object' || chatWindow[0].closed) {
		chatWindow[0] = window.open(getChatLandingURL(), 'chatWindow', 'width=670,height=640');
	} else {
		chatWindow[0].focus();
	}			
}

function updateCampaigns(campaignName, recipeName) {
	var xmlStr;
	var nodeCampaign = '';
	var subSource = '';
	var expdate = new Date();
	expdate.setTime (expdate.getTime() + 7776000000); // 90 days
	if (getCookie('subSource') && getCookie('subSource') != '' && getCookie('subSource') != null)
		subSource = getCookie('subSource');
	if (getCookie('campaigns') && getCookie('campaigns') != '' && getCookie('campaigns') != null) {
		xmlStr = getCookie('campaigns');
		var closeIndex = xmlStr.indexOf('</campaigns>')
		nodeCampaign = xmlStr.substring(62, closeIndex);
	}
	if (campaignName == 'init') {
		var xmlStart = '<?xml version="1.0" encoding="utf-8"?><offermatica><campaigns>';
		var xmlEnd = '</campaigns><subsource>' + subSource + '</subsource><visitor>' + visitorID + '</visitor></offermatica>';
		xmlStr = xmlStart + nodeCampaign + xmlEnd;
		setCookie('campaigns', xmlStr, expdate, '/', '.stamps.com', false);
	} else {
		if (nodeCampaign.indexOf('<name>' + campaignName + '</name>') == -1) {
			var xmlStart = '<?xml version="1.0" encoding="utf-8"?><offermatica><campaigns>';
			nodeCampaign += '<campaign><name>' + campaignName + '</name><recipe>' + recipeName + '</recipe></campaign>';
			var xmlEnd = '</campaigns><subsource>' + subSource + '</subsource><visitor>' + visitorID + '</visitor></offermatica>';
			xmlStr = xmlStart + nodeCampaign + xmlEnd;
			setCookie('campaigns', xmlStr, expdate, '/', '.stamps.com', false);
		}
	}
}

updateCampaigns('init');

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//Logic for determining if the user has a OS that is compatible with Web Postage
var version = parseFloat($.browser.version.split('.').slice(0,2).join('.'));
var webPostageSupported = true;
var ua = navigator.userAgent;
var arrUa = ua.split(/\s*[;)(]\s*/);
var indx = ($.browser.safari || ($.browser.firefox && version < 4)) ? 3 : 2;
var osVersion = arrUa[indx].split(' ').pop();
var arrOsVerNum = osVersion.split('_');
var osVerNum = parseFloat(arrOsVerNum[0] + '.' + arrOsVerNum[1]);

if (ua.match(/Mac|PPC/)) {
	if (ua.match(/PPC/)) {
		webPostageSupported = false;
	} else {
		if (osVerNum < 10.5)
			webPostageSupported = false;
	}
}

$(document).ready(function() {
						   
	$('#btnCalcPO').click(function() {
		window.open('/tools/calculators/postoffice/', 'pocomparison', 'height=302,width=492,scrollbars=no,resizable=yes,status=no');
	});

	$('#calculator, #btnCalcPM, #bluecalculator').click(function() {
		window.open('/tools/calculators/postagemeter/', 'pmcomparison', 'height=249,width=492,scrollbars=no,resizable=yes,status=no');
	});

	$('#calc').click(function() {
		window.open('/tools/calculators/postoffice/', 'pmcomparison', 'height=249,width=492,scrollbars=no,resizable=yes,status=no');
	});

	$('#tabs div').mouseout(function () {
		$('#tabs div a').css('color', '#EEE');
	});
	
	$('#tabs div').mouseover(function () {
		if ($(this).css('cursor') != 'auto') {
			$('#tabs div#' + this.id + ' a').css('color', '#FFF');
		}
	});
	
	$('#tabs>div').click(function () {
		if ($(this).css('cursor') != 'auto') {
			if (this.id == 'tabViewDemo') {
			} else {
				document.location = $('#' + this.id + ' a').attr('href');
			}
		}
		return false;
	});
	
	$('#btnGetPostage, #headerMenuGetStarted, #goLink, #tabgetStarted, #customTallOfferbox, #regLinkPlansComparisonPro').click(function() {
		if (identifier == 'AdvancedShipping') {
			window.location = '/registration/?PromoCode=PJKR-9QX2';
		} else if (getCookie('Activation')) {
			window.location = 'http://store.stamps.com/Store/directmail/activation.jsp';
		} else {
			window.location = '/registration/';
		}
		return false;
	});

	$('#offerBox').click(function() {
		if (identifier == 'AdvancedShipping-No') {
			window.location = '/registration/?PromoCode=PJKR-9QX2';
		} else if (getCookie('Activation')) {
			window.location = 'http://store.stamps.com/Store/directmail/activation.jsp';
		} else if ('.popupBasic') {
		} else {
			window.location = '/registration/';
		}
		return false;
	});

	equalHeight($('#middleContent>div'));

	$('a.popupBasic').fancybox({
		'hideOnContentClick': false,
		'titleShow': false,
		'overlayColor': '#83a1d1'
	});	
	
	$('#regLinkProShipPopUp').click(function(){
		// expire the cookies first for the benefit of IE
		var exp1 = new Date();
		exp1.setTime(exp1.getTime() - 86400000);
		setCookie("Source", false, exp1, "/", ".stamps.com");
		setCookie("PromoCode", false, exp1, ",", ".stamps.com");
		setCookie("OfferID", false, exp1, "/", ".stamps.com");
		// then delete them
		deleteCookie("Source", "/", ".stamps.com");
		deleteCookie("PromoCode", "/", ".stamps.com");
		deleteCookie("OfferID", "/", ".stamps.com");
		// set new cookies
		var exp2 = new Date();
		exp2.setTime(exp2.getTime() + 229990000000);
		setCookie("Source", "si10584354", exp2, "/", ".stamps.com");	
		setCookie("PromoCode", "FREE1500", exp2, "/", ".stamps.com");
		setCookie("OfferID", "ProfShip_3499_5pa_1030wc_1060wc_1090wc_10120wc_scfree_wk35", exp2, "/", ".stamps.com");
		setTimeout(nextPageProShipPlan, 500);
	});
	
	$('#regLinkProShip500PopUp').click(function(){
		// expire the cookies first for the benefit of IE
		var exp1 = new Date();
		exp1.setTime(exp1.getTime() - 86400000);
		setCookie("Source", false, exp1, "/", ".stamps.com");
		setCookie("PromoCode", false, exp1, ",", ".stamps.com");
		setCookie("OfferID", false, exp1, "/", ".stamps.com");
		// then delete them
		deleteCookie("Source", "/", ".stamps.com");
		deleteCookie("PromoCode", "/", ".stamps.com");
		deleteCookie("OfferID", "/", ".stamps.com");
		// set new cookies
		var exp2 = new Date();
		exp2.setTime(exp2.getTime() + 229990000000);
		setCookie("Source", "si10506128", exp2, "/", ".stamps.com");	
		setCookie("PromoCode", "FREE500", exp2, "/", ".stamps.com");
		setCookie("OfferID", "ProfShip_3499_5pa_1030wc_1060wc_1090wc_10120wc_scfree_wk35", exp2, "/", ".stamps.com");
		setTimeout(nextPageProShipPlan500, 500);
	});

	//Set cookie with rate to make it available to ProActive Chat
	setCookie('PlanRate', rate, null, '/', '.stamps.com', false);

	getDemoDimensions();
	$('a.SDCdemoLink').fancybox({
		'type': 'iframe',
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'speedIn': '300',
		'speedOut': '300',
		'hideOnContentClick': false,
		'titleShow': false,
		'overlayColor': '#83a1d1', 
		'width': videoWidth + 5,
		'height': videoHeight + 5
	});	
	$('#eagleEyeDemoLink').fancybox({
		'type': 'iframe',
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'speedIn': '300',
		'speedOut': '300',
		'hideOnContentClick': false,
		'titleShow': false,
		'overlayColor': '#83a1d1', 
		'width': videoWidth + 5,
		'height': videoHeight + 5
	});	
	$('#shippingDemoLink').fancybox({
		'type': 'iframe',
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'speedIn': '300',
		'speedOut': '300',
		'hideOnContentClick': false,
		'titleShow': false,
		'overlayColor': '#83a1d1', 
		'width': videoWidth + 5,
		'height': videoHeight + 5
	});
	$('#postageMeterDemoLink').fancybox({
		'type': 'iframe',
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'speedIn': '300',
		'speedOut': '300',
		'hideOnContentClick': false,
		'titleShow': false,
		'overlayColor': '#83a1d1', 
		'width': videoWidth + 5,
		'height': videoHeight + 5
	});	

});
