// Dear Binny. I love you!
// http://binnyva.blogspot.com/2005/10/dump-function-javascript-equivalent-of.html
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];

  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
alert(dumped_text);
}

// Is MSIE?
var IE = document.all ? true : false;
var theButton = false;
var kill = false;
var scrollBy = 0;
var Scrolltimeout = null;
var isLeft = false;

function setMousemove(theButton1, isLeft2){
	isLeft = isLeft2;
	theButton = theButton1;
	document.onmousemove = detectScroll;
}

function unsetMousemove(){
	theButton = null;
	isLeft = false;
	scrollBy = 0;
	document.onmousemove = null;
}

//theScroll, theButton
function detectScroll(e){
	e = e || window.event;

	if (IE){
		tempX = event.clientX + document.body.scrollLeft;
	}
	else {
		tempX = e.pageX;
	}

	if (tempX < 0){
		tempX = 0;
	}

	var curleft = 0;

	if (theButton.offsetParent){
		curleft = theButton.offsetLeft

		obj = theButton;
		// Loop through the parents
		while (obj = obj.offsetParent){
			curleft += obj.offsetLeft
		}

	}

	var v = tempX - curleft;
	//GetByID('coord').innerHTML = (30 - v) / 10;//v;//tempX - curleft;

	lastScroll = 0;

	if (isLeft){
		v = 50 - v;
	}

	scrollBy = (v * v * 0.03) / (v);

	scrollIt();
	//var timeOut = setTimeout('scrollIt(' + (30 - v) / 10 +')', 100);

	/*while (GetByID('skinsmain').scrollLeft != lastScroll && kill == false){
		lastScroll = GetByID('skinsmain').scrollLeft;


GetByID('coord').innerHTML = i;
		GetByID('skinsmain').scrollLeft = GetByID('skinsmain').scrollLeft + 1;
	}*/

}

function scrollIt(){
	if (scrollBy != 0){
		//GetByID('coord').innerHTML = GetByID('skinsmain').scrollLeft;
		if (isLeft){
			GetByID('skinsmain').scrollLeft = GetByID('skinsmain').scrollLeft - scrollBy;
		}
		else {
		/*	if (scrollBy < 1){
				scrollBy = 1.0 + scrollBy;
			}*/
			GetByID('skinsmain').scrollLeft = GetByID('skinsmain').scrollLeft + scrollBy;
		}
		GetByID('scrollby').innerHTML = scrollBy;
		Scrolltimeout = setTimeout('scrollIt(' + ((isLeft) ? 'true' : 'false') + ')', 200);
	}
	//isLeft;//GetByID('skinsmain').scrollLeft;//scrollBy;
}

function getWidth(Elem){
	if (ns4){
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	}
	else {
		if(document.getElementById){
			var elem = document.getElementById(Elem);
		}
		else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		}
		else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}

// Because I'm lazy
function GetByID(theID){
	return document.getElementById(theID);
}

function ShowOrHide(theID){
	var theObject = GetByID(theID);

	if (theObject.style.display == 'none'){
		theObject.style.display = '';
		return true;
	}
	else {
		theObject.style.display = 'none';
		return false;
	}
}

function ShowOrHideTicket(theID){
	for (var i = 0; i < 100; i++){
		if (!GetByID(theID + '_' + i)){
			break;
		}

		var shown = ShowOrHide(theID + '_' + i);
	}

	if (shown){
		GetByID(theID + '_a').innerHTML = '[-]';
	}
	else {
		GetByID(theID + '_a').innerHTML = '[+]';
	}
}

function hide_panel(){
	document.body.className = 'full';
}

function show_panel(){
	document.body.className = '';
}

function in_array(needle, haystack){
	var found = false;

	for (var i = 0; i < haystack.length; i++){

		if (needle == haystack[i]){
			found = true;
			break;
		}

	}

	return found;
}

function security_init(data){
	GetByID('security_image').src = GetByID('security_image').src.replace(/(\?image\=).*$/, '$1' + data['crypt']);
	GetByID('security_crypt').value = data['crypt'];
	GetByID('security_answers').innerHTML = data['answers'];
}

function quantityOnChange(id, quantity, price, extraprice){

	if (parseInt(GetByID('quantity_' + id).value) > 50){
		GetByID('quantity_' + id).value = 50;
		alert('Items are limited to a maximum of 50 per item. If you wish to order over 50, please contact us for a discount.');
	}

	// If it's invalid, remove the item
	if (isNaN(parseInt(GetByID('quantity_' + id).value)) || GetByID('quantity_' + id).value < 1){
		removeItem(id, extraprice);
	}
	else if (GetByID('tr_quantity_' + id).className == 'removed'){
		addItem(id, extraprice);
	}

	// Get the original price per item
	var priceperitem = parseFloat((price > 0) ? price / quantity : '0.00');
	// Save the original price
	var originalprice = parseFloat(GetByID('jsprice_' + id).innerHTML.replace(',', ''));

	// Set the new price
	///GetByID('price_' + id).innerHTML = number_format(priceperitem * GetByID('quantity_' + id).value, 2, '.', ',');
	var newprice = number_format(priceperitem * GetByID('quantity_' + id).value, 2, '.', ',');
	GetByID('jsprice_' + id).innerHTML = newprice;

	// Set the new total
	addSubtotal(parseFloat(newprice.replace(',', '')) - parseFloat(originalprice));

	// Parse it as an integer
	GetByID('quantity_' + id).value = parseInt(GetByID('quantity_' + id).value);

	if (parseFloat(GetByID('quantity_' + id).value) != quantity){
		disableIt[id] = 'false';
	}
	else {
		disableIt[id] = 'true';
	}

	if (!GetByID('update_basket')){

		if (parseFloat(GetByID('quantity_' + id).value) != quantity){
			disableItBool = false;
		}

		return;
	}

	// Only show the update button if needed
	if (parseFloat(GetByID('quantity_' + id).value) == quantity && !in_array('false', disableIt)){
		GetByID('update_basket').disabled = true;
		//GetByID('update_basket').className = 'button_disabled';
	}
	else {
		GetByID('update_basket').disabled = false;
		GetByID('update_basket').className = 'button';
	}
}

function addRecurring(price){
	rectotal = parseFloat(rectotal.toString().replace(',', '')) + price;

	// Is the recurring total set?
	if (GetByID('rectotal')){
		GetByID('rectotal').innerHTML = number_format(rectotal, 2, '.', ',');
	}

	calculateTotal();
}

// Recalculate the total
function calculateTotal(price){
	total = subtotal + rectotal;
	//total = number_format(total + price, 2, '.');

	// Is it a percent?
	if (ispercent == true){
		// Work out the discount
		total = (total == 0) ? 0 : number_format(total - ((discount / 100) * total), 2, '.');
	}
	else {
		// Work out the discount
		total = (total == 0) ? 0 : number_format(total - discount, 2, '.');
	}

	// Is the total set?
	if (GetByID('total')){
		GetByID('total').innerHTML = number_format(total, 2, '.', ',');
	}

}

function addSubtotal(price){
	subtotal = parseFloat(subtotal.toString().replace(',', '')) + price;

	// Is the subtotal set?
	if (GetByID('subtotal')){
		GetByID('subtotal').innerHTML = number_format(subtotal, 2, '.', ',');
	}

	calculateTotal();
}

function subtractRecurring(price){
	rectotal = parseFloat(rectotal.toString().replace(',', '')) - price;

	// Is the recurring total set?
	if (GetByID('rectotal')){
		GetByID('rectotal').innerHTML = number_format(rectotal, 2, '.', ',');;
	}

	calculateTotal();
}

function subtractTotal(price){
	total = parseFloat(total.toString().replace(',', '')) - price;

	// Work out the discount
	if (typeof(discount) != 'undefined'){
	//	total = total - ((discount / 100) * total);
	}

	// Is the urring total set?
	if (GetByID('total')){
		GetByID('total').innerHTML = number_format(total, 2, '.', ',');
	}
}

function subtractSubtotal(price){
	subtotal = parseFloat(subtotal.toString().replace(',', '')) - price;

	// Is the suburring total set?
	if (GetByID('subtotal')){
		GetByID('subtotal').innerHTML = number_format(subtotal, 2, '.', ',');
	}

	calculateTotal();
}

function removeItem(item, extraprice){
	subtractRecurring(extraprice);
	GetByID('quantity_' + item).value = 0;
	GetByID('tr_quantity_' + item).className = 'removed';

	disableIt[item] = 'false';

	// Upgrades?
	if (GetByID('tr_upgrades_' + item)){
		GetByID('tr_upgrades_' + item).className = 'hide';
	}

	// Support?
	if (GetByID('tr_support_' + item)){
		GetByID('tr_support_' + item).className = 'hide';
	}

	GetByID('rem_quantity_' + item).innerHTML = 'Removed';
}

function addItem(item, extraprice){
	addRecurring(extraprice);

	// Upgrades?
	if (GetByID('tr_upgrades_' + item)){
		GetByID('tr_upgrades_' + item).className = '';
	}

	// Support?
	if (GetByID('tr_support_' + item)){
		GetByID('tr_support_' + item).className = '';
	}

	GetByID('tr_quantity_' + item).className = '';
	GetByID('rem_quantity_' + item).innerHTML = 'Remove';
}

function remove_item_init(data){
	// Remove it
	GetByID('item_' + data['id']).parentNode.removeChild(GetByID('item_' + data['id']));

	if (data['total'] == '0.00'){
		GetByID('sub_basket').style.paddingBottom = '10px';
		GetByID('sub_basket').innerHTML = '<div id="yourbasket">Your basket</div>' + "\n";
		GetByID('sub_basket').innerHTML = GetByID('sub_basket').innerHTML + '	<span class="error_text">Your basket is empty!</span>' + "\n";
		GetByID('sub_basket').innerHTML = GetByID('sub_basket').innerHTML + '</div>' + "\n";
	}
	else {
		GetByID('side_total').innerHTML = data['total'] + ' USD';
	}
}

function slideshow_init(data, prev){

	// No other skins?
	if (typeof(data['id']) == 'undefined' || stopslide == true){
		return;
	}

	// Store the previous slide
	if (typeof(prev) == 'undefined'  || prev == false){
		prev_price = GetByID('rand_price').innerHTML;
		prev_id = GetByID('rand_id').value;
		prev_name = GetByID('rand_name').innerHTML;
		prev_safename = GetByID('rand_name').href;
		prev_preview = GetByID('rand_preview').src;
	}
	else {
		// Stop the previous timeout
		clearTimeout(slidetime);
	}

	// Set the preview
	GetByID('rand_preview').src = data['preview'];

	// Set the data
	GetByID('rand_id').value = data['id'];
	GetByID('rand_name').innerHTML = data['name'];
	GetByID('rand_price').innerHTML = data['price'];

	// Set the links
	GetByID('rand_name').href = data['safename'];
	GetByID('rand_preview_link').href = data['safename'];
	GetByID('rand_price').href = data['safename'];

	// Do it all over again
	slidetime = setTimeout("get_slideshow(GetByID('rand_id').value, 'slideshow_init')", slidelength);
}

function DoStop(){
	GetByID('slidecontrol').innerHTML = 'Continue';
	GetByID('slidecontrol').onclick = function(){
		DoStart();
		return false;
	}

	clearTimeout(slidetime);
	stopslide = true;
}

function DownSpeed(){
	// Make it faster
	if (slidelength > 1000){
		slidelength = slidelength - 1000;
		GetByID('slidespeed').innerHTML = slidelength / 1000 + '/s';
	}
}

function UpSpeed(){
	// Make it slower
	if (slidelength < 10500){
		slidelength = slidelength + 1000;
		GetByID('slidespeed').innerHTML = slidelength / 1000 + '/s';//number_format(2 - (slidelength / 10000), 2, '.', ',');
	}
}

function DoStart(that){
	GetByID('slidecontrol').innerHTML = 'Stop';
	GetByID('slidecontrol').onclick = function(){
		DoStop();
		return false;
	}

	stopslide = false;
	get_slideshow(GetByID('rand_id').value, 'slideshow_init');
}

function DisableSlide(){
	// create an instance of the Date object
	var now = new Date();
	// fix the bug in Navigator 2.0, Macintosh
	fixDate(now);
	now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);

	// Set the cookie
	setCookie('disslide', 'disabled', now, '/');

	// Set the text to enable
	GetByID('disableslide').innerHTML = 'Enable';
	GetByID('disableslide').onclick = function(){
		EnableSlide();
		return false;
	}
	// Stop the slideshow
	clearTimeout(slidetime);
	stopslide = true;
}

function EnableSlide(){
	// Set the cookie
	deleteCookie('disslide', '/');

	// Set the text to enable
	GetByID('disableslide').innerHTML = 'Disable';
	GetByID('disableslide').onclick = function(){
		DisableSlide();
		return false;
	}
	// Start the slideshow
	stopslide = false;
	get_slideshow(GetByID('rand_id').value, 'slideshow_init');
}

function ShowPrevious(){

	// There isn't a previous!
	if (prev_id == false){
		return false;
	}

	data = new Array();
	data['price'] = prev_price;
	data['id'] = prev_id;
	data['name'] = prev_name;
	data['safename'] = prev_safename;
	data['preview'] = prev_preview;

	// Queue it... man I love this function
	ajax_queue('slideshow_init', false, data, true);
}

/* Made by Mathias Bynens <http://mathiasbynens.be/> */
function number_format(a, b, c, d){
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');

	if (!f[0]){
		f[0] = '0';
	}

	if (!f[1]){
		f[1] = '';
	}

	if (f[1].length < b){
		g = f[1];
		for (i=f[1].length + 1; i <= b; i++){
			g += '0';
		}
		f[1] = g;
	}

	if(d != '' && f[0].length > 3){
		h = f[0];
		f[0] = '';

		for(j = 3; j < h.length; j+=3){
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}

		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}

	c = (b <= 0) ? '' : c;
	return f[0] + c + f[1];
}

// Sets a cookie
// Found almost everywhere...
function setCookie(name, value, expires, path, domain, secure){
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

// Grabs a cookie
// Found almost everywhere...
function getCookie(name){
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1){
		begin = dc.indexOf(prefix);
		if (begin != 0){
			return null;
		}
	}
	else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1){
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

// Deletes a cookie
// Found almost everywhere...
function deleteCookie(name, path, domain){
	if (getCookie(name)){
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

// Handles dates
// Found almost everywhere...
function fixDate(date){
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0){
		date.setTime(date.getTime() - skew);
	}
}
