// 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;
	}
}

var popunder = "/satisfaction/";
var winfeatures = "width=630,height=510,scrollbars=1,resizable=1,toolbar=0,location=1,menubar=0,status=0,directories=0";

function loadSurvey() {
	var exp = new Date();
	exp.setTime(exp.getTime() + 62208000000);
	if (getCookie("popunder") == null) {  
		loadpopunder();
		setCookie("popunder", "yes", exp, "/", false, false);
	}
}

function loadpopunder(){
	win2 = window.open(popunder, "", winfeatures);
	if (win2)
		win2.blur();
	window.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');

//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() {
			
	$('#button, #headerMenuGetStarted').click(function() {
		if (getCookie('Activation')) {
			window.location = 'http://store.stamps.com/Store/directmail/activation.jsp';
		} else {
			window.location = '/registration/';
		}
		return false;
	});

	if (getCookie('Activation')) {
		$('#regLink').attr('href', 'http://store.stamps.com/Store/directmail/activation.jsp');
	}
	
	$('a.popupBasic').fancybox({
		'hideOnContentClick': false,
		'titleShow': false,
		'overlayColor': '#83a1d1'
	});	

	//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
	});	

});
