/* GENERAL FUNCTIONS */
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 calculateTime (timestamp) {
	currentTimeMS = new Date().getTime();
	currentTimeS = currentTimeMS/1000;
	seconds = Math.round(currentTimeS - timestamp);
	minutes = Math.round(seconds/60);
	hours = Math.round(minutes/60);
	days = Math.round(hours/24);
	weeks = Math.round(days/7);
	if (minutes < 60) {
		if (minutes < 2) {
			return 'just now';
		}
		return minutes + ' min. ago';
	} else if (hours < 24) {
		if (hours == 1) {
			return hours + ' hour ago';
		} else {
			return hours + ' hours ago';
		}
	} else if (days < 7) {
		if (days == 1) {
			return days + ' day ago';
		} else {
			return days + ' days ago';
		}
	} else if (days < 31) {
		if (weeks == 1) {
			return weeks + ' week ago';
		} else {
			return weeks + ' weeks ago';
		}
	} else {
		return false;
	}
}

function submitForm(url, formID, injectID, inject, disable) {
	if (typeof(inject) == 'undefined') { inject = true; }
	if (typeof(disable) == 'undefined') { disable = true; }
	if (disable) {
		if ($(formID).find('div.button input')) {
			$(formID).find('div.button input').attr('onclick','');
		}
	}
	showLoading(formID);
	$.post(url, $(formID).serialize(), function(data){
		if ($(injectID).hasClass('hide')) {
			$(injectID).show();
		}
		if (inject) {
			$(injectID).html(data);
		}
		hideLoading(formID);
	});
}

function showLoading(formID) {
	if ($(formID + '_button')) {
		$(formID + '_button').hide();
	}
	if ($(formID + '_loading')) {
		$(formID + '_loading').html('<div class="loading-form"><img src="/extension/universalnutrition/design/site/images/loading.gif" alt="Loading..."/></div>');
	}
}

function hideLoading(formID) {
	if ($(formID + '_loading')) {
		$(formID + '_loading').html('');
	}
	if ($(formID + '_button')) {
		$(formID + '_button').show();
	}
}

function ajaxThickboxLinks (container) {
	$('.' + container + ' a').addClass('thickbox');
	$('.' + container + ' a').each(function() {
		href = $(this).attr('href');
		href = href + '&width=' + uni.listWidth + '&height=' + uni.listHeight;
		$(this).attr('href', href);
	});
}

var redirect_time = 3;
var current_second = redirect_time + 1;
var redirect_url = '';
function redirectTimer () {
	if (current_second != 1){
		current_second -= 1;
		$('span#timer').html(current_second + '&nbsp;');
	} else {
		window.location = redirect_url;
		return;
	}
	setTimeout("redirectTimer()",1000);
}

/* FLASH GLOBAL CONFIG */
var swfConfig = { xi : "flash/expressInstall.swf", version : "9.0.0", params : { allowScriptAccess : "always", wmode : "transparent" } };

/* ADD THIS GLOBAL CONFIG */
var addthis_config = {
    data_track_clickback: true
}

function addToCart(form) {
	form_id = $(form).attr('id');
	product = $('#' + form_id + ' .product_variation')[0];
	if ($(product).val() == '') {
		alert('Please select a product from the dropdown list.');
		return false;
	}
	product = $(product).val().split('_');
	node_input = $('#' + form_id + ' .content_node_id')[0];
	$(node_input).val(product[0]);
	object_input = $('#' + form_id + ' .content_object_id')[0];
	$(object_input).val(product[1]);
	return true;
}
function ajaxCart() {
	$.ajax({
		type: "POST",
		url: '/ezshop/ajaxbasket',
		success: function(data){
			$('#cart-inject').html(data);
		}
	});
}

/* SUPPLEMENT FACTS */
function ajaxPicture(main_node_id, title, height, width) {
	var title = title || '',
		winHeight = (height + 45) || 500,
		winWidth = (width + 15) || 660;
	
	url = '/content/view/ajax/' + main_node_id + '?height=' + winHeight + '&width=' + winWidth;
	tb_show(title, url);
}


$(document).ready(function() {
	$('span.timespan').each(function() {
		ts = $(this).attr('title');
		timespan = calculateTime(ts);
		if (timespan != false) {
			$(this).text(timespan);
		}
	});
});

function formsMakeDate() {
	if (!$('#all-day').attr('checked')) {
		start_date = $('#start-date-select').val() + ' ' + $('#start-hour').val() + ':' + $('#start-minutes').val() + $('#start-am-pm').val();
		end_date = $('#end-date-select').val() + ' ' + $('#end-hour').val() + ':' + $('#end-minutes').val() + $('#end-am-pm').val();
	} else {
		start_date = $('#start-date-select').val() + ' 12:00am';
		end_date = $('#end-date-select').val() + ' 11:59pm';
	}
	$('#start-date').val(start_date);
	$('#end-date').val(end_date);
	return true;
}

function trim(str) {
	if (String.prototype.trim) {
		return str.trim();
	} else {
		var	str = str.replace(/^\s\s*/, ''),
			ws = /\s/,
			i = str.length;
		while (ws.test(str.charAt(--i)));
		return str.slice(0, i + 1);
	}
}
