var prefix = parent.location.protocol + '//';
var ajaxUrl = prefix + 'www.weeklyplus.com/themes/';
var baseUrl = '';
function redirect(url){
	parent.location = url;
}

function fieldFocus(field, text){
	if(field.value == text){
		field.value = '';
	}
}

function fieldBlur(field, text){
	if(field.value == ''){
		field.value = text;
	}
}

function fieldBGBlur(field, url){
	if(field.value == ''){
		field.style.backgroundImage = url;
		field.style.backgroundRepeat = 'no-repeat';
	} else {
		field.style.backgroundImage = '';
	}
}

function loadLoginFields(){
	var email = document.getElementById('subscribeEmailLogin');
	var password = document.getElementById('subscribePasswordLogin');
	fieldBGBlur(email, 'url(/beta/themes/images/subscribe-input-email.gif)');
	fieldBGBlur(password, 'url(/beta/themes/images/subscribe-input-password.gif)');
}

function toggleDiv(action, div){
	if(document.getElementById(div)){
		if(action == 'hide'){
			document.getElementById(div).style.display = 'none';
		} else if(action == 'show') {
			document.getElementById(div).style.display = 'block';
		} else {
			if(document.getElementById(div).style.display == 'block'){
				document.getElementById(div).style.display = 'none';
			} else {
				document.getElementById(div).style.display = 'block';
			}
		}
	}
}

function toggleSubscribe(){
	if(document.getElementById('memberLogin').style.display == 'block'){
		document.getElementById('memberLogin').style.display = 'none';
		document.getElementById('memberSubscribe').style.display = 'block';
	} else {
		document.getElementById('memberSubscribe').style.display = 'none';
		document.getElementById('memberLogin').style.display = 'block';
	}
}

function confirmDelete(msg, url){
	if(confirm(msg)) document.location = url;
}

function updateTotal(idCartItem,fee,dealsLeft,buyLimit){
    var amount = document.getElementById('quantity-' + idCartItem).value;
	var total = 0;

	amount = amount * 1;
	dealsLeft = dealsLeft * 1;

	if( amount > buyLimit ) {
		alert("We're sorry this deal is limited to "+buyLimit+" purchases per person.");
	} else if( amount > dealsLeft){
		alert("We're sorry there are only " + dealsLeft + " deals left.");
	} else {
		total = amount * fee;
		document.getElementById('total-' + idCartItem).innerHTML = "$" + total;
	}
    
    updateCartTotal();
}

function updateCartTotal(){
    var cartTotal = 0;
    $$(".cartTextTotal").each(function(s){
        amount = s.innerHTML;
        cartTotal = cartTotal + parseInt(amount.substring(1, amount.length));
    });
    
    $("orderTotal").update("$" + cartTotal);
}

function calcTime(offset) {

    // create Date object for current location
    d = new Date();

    // convert to msec
    // add local time zone offset
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    nd = new Date(utc + (3600000*offset));

    // return time as a string
    return nd;

}

function clockAnimation(days, size, idAdvertiser, idGroup){
	myDay = days+'';
	div = myDay.replace(".", "-");
	document.getElementById(size + 'Hand-' + idGroup + '-' + idAdvertiser).style.background = "transparent url('"+baseUrl+"/themes/images/clock-default-"+size+"-hand-"+div+".png') no-repeat scroll left top";
}

var divGraphic = 0;

function clockTimeLeft(idAdvertiser, idGroup, myYear, myMonth, myDate, myHour, tz, size) {
	dateFuture = new Date(myYear, myMonth-1, myDate, myHour, 59, 59);

	dateNow = calcTime(tz);
	//dateNow = new Date();									//grab current date
	amount = dateFuture.getTime() - dateNow.getTime();		//calc milliseconds between dates
	dateNow = '';

	// time is already past
	if(amount < 0){

		document.getElementById(size+'TimeLeftDays-'+idGroup+'-'+idAdvertiser).innerHTML = 0;
		document.getElementById(size+'TimeLeftHrs-'+idGroup+'-'+idAdvertiser).innerHTML = 0;
		document.getElementById(size+'TimeLeftMin-'+idGroup+'-'+idAdvertiser).innerHTML = 0;
		document.getElementById(size+'TimeLeftSec-'+idGroup+'-'+idAdvertiser).innerHTML = 0;

	} else{ // date is still good

		days=0;hours=0;mins=0;out="";

		amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs

		days = Math.floor(amount/86400);//days
		amount = amount%86400;

		hours = Math.floor(amount/3600);//hours
		amount = amount%3600;

		mins = Math.floor(amount/60);//minutes
		amount = amount%60;

		secs = Math.floor(amount);//seconds

		if(days < 1){
			graphic = '0';
		} else if(days >= 7){
			graphic = '7';
		} else {
			graphic = days;
		}
        
		if(divGraphic != graphic){
			divGraphic = graphic;
			clockAnimation(graphic, size, idAdvertiser, idGroup);
		}

		document.getElementById(size+'TimeLeftDays-'+idGroup+'-'+idAdvertiser).innerHTML = days;
		document.getElementById(size+'TimeLeftHrs-'+idGroup+'-'+idAdvertiser).innerHTML = hours;
		document.getElementById(size+'TimeLeftMin-'+idGroup+'-'+idAdvertiser).innerHTML = mins;
		document.getElementById(size+'TimeLeftSec-'+idGroup+'-'+idAdvertiser).innerHTML = secs;

		setTimeout("clockTimeLeft('"+idAdvertiser+"','"+idGroup+"','"+myYear+"','"+myMonth+"','"+myDate+"','"+myHour+"','"+tz+"','"+size+"')", 1000);
	}
}
