var xajaxRequestUri=baseURL;
var xajaxDebug=false;
var xajaxStatusMessages=false;
var xajaxWaitCursor=false;
var xajaxDefinedGet=0;
var xajaxDefinedPost=1;

function x_call(){return xajax.call("call", arguments, 1);}
function x_checkSession(){return xajax.call("checkSession", arguments, 1);}

interfaceClass = Class.create();
interfaceClass.prototype = {
	initialize: function() {
		this.events = {
			windowLoad			:	this.windowLoad.bindAsEventListener(this),
			keypress			:	this.keypress.bindAsEventListener(this)
		};

		Event.observe(window, 'load', 		this.events.windowLoad);
		Event.observe(window, 'keypress', 	this.events.keypress);
	},
	windowLoad: function() {
		this.hoverFade();
		new loginClass();
		new contactClass();
		new quickSearchClass();

		$$('.imageLink').each(function(el) {
			el.observe('click',function(ev) {
				ev.stop();
				$$('.imageLink').each(function(el) {
					el.className = "imageLink";
				});
				el.className = "imageLink on";

				$('previewContainer').childElements().each(function(el) {
					el.hide();
				});

				$('previewLink-' + el.id).childElements().each(function(el) {
					el.remove();
				});

				$('previewLink-' + el.id).show();
				$('previewLink-' + el.id).appendChild(new Element('img', { src : "/pic/454/0/1/90/img/templates/" + el.id + ".jpg", id : 'preview-' + el.id }));

/*				Event.observe('preview-' + el.id, 'load', function(ev) {
						$('indicator').hide();
					}
				);
				$('indicator').show();*/

			});
      	});

		if ($('toggleColours')) {
			Event.observe('toggleColours', 'click', function(e) {
				e.stop();
				if ($('toggleColours').innerHTML == "Select additional colours") {

					new Effect.BlindDown('additionalColours', {
						afterFinish: function() {
							$('toggleColours').innerHTML = "Hide additional colours";
							$('search').action = "/search/?showColours=true";
						},
						queue: 'end',
						duration: 0.5,
						fps: 30
					});

				} else {
					new Effect.BlindUp('additionalColours', {
						afterFinish: function() {
							$('toggleColours').innerHTML = "Select additional colours";
							$('search').action = "/search/";
						},
						queue: 'end',
						duration: 0.5,
						fps: 30
					});
				}
			});
		}
	},
	keypress: function(e) {
		key = e.which || e.keyCode;
	},
	hoverFade: function() {
		$$('.hoverFade').each(function(e) {
			e.style.cursor = "pointer";
			Element.setOpacity(e, 0.75);

			Event.stopObserving(e, 'mouseover', this.hoverFadeMouseOver);
			Event.observe(e, 'mouseover', 		this.hoverFadeMouseOver);

			Event.stopObserving(e, 'mouseout', 	this.hoverFadeMouseOut);
			Event.observe(e, 'mouseout', 		this.hoverFadeMouseOut);

			if (e.id == "") {
				e.id = Math.round(100*Math.random());
			}

		}.bind(this));
	},
	hoverFadeMouseOver: function(event) {
		new Effect.Opacity(this, { from: 0.75, to: 1.0, duration: 0.3, fps: 30, queue: {position: 'end', scope: this.id, limit: 2} });
		event.stop();
	},
	hoverFadeMouseOut: function(event) {
		new Effect.Opacity(this, { from: 1.0, to: 0.75, duration: 0.3, fps: 30, queue: {position: 'end', scope: this.id, limit: 2} });
		event.stop();
	},
	generateCC: function() {
		var cc_number = new Array(16);
		var cc_len = 16;
		var start = 0;
		var rand_number = Math.random();

		switch($('checkoutForm').creditCardType.value)
        {
			case "Visa":
				cc_number[start++] = 4;
			  break;
			case "Discover":
				cc_number[start++] = 6;
				cc_number[start++] = 0;
				cc_number[start++] = 1;
				cc_number[start++] = 1;
			  break;
			case "MasterCard":
				cc_number[start++] = 5;
				cc_number[start++] = Math.floor(Math.random() * 5) + 1;
			  break;
			case "Amex":
				cc_number[start++] = 3;
				cc_number[start++] = Math.round(Math.random()) ? 7 : 4 ;
				cc_len = 15;
			  break;
        }

        for (var i = start; i < (cc_len - 1); i++) {
			cc_number[i] = Math.floor(Math.random() * 10);
        }

		var sum = 0;
		for (var j = 0; j < (cc_len - 1); j++) {
			var digit = cc_number[j];
			if ((j & 1) == (cc_len & 1)) digit *= 2;
			if (digit > 9) digit -= 9;
			sum += digit;
		}

		var check_digit = new Array(0, 9, 8, 7, 6, 5, 4, 3, 2, 1);
		cc_number[cc_len - 1] = check_digit[sum % 10];

		$('checkoutForm').creditCardNumber.value = "";
		for (var k = 0; k < cc_len; k++) {
			$('checkoutForm').creditCardNumber.value += cc_number[k];
		}
		$('checkoutForm').cvv2Number.value = "1234";
	}
}

interface = new interfaceClass();

loginClass = Class.create();
loginClass.prototype = {
	initialize: function() {
		if ($('login.submit')) {
			$('login.submit').observe('click',
			function(e) {
				e.stop();
				x_call("login", "input", "login.error",
				{
					username: $F('username'),
					password: $F('password'),
					redirect: $F('redirect')
				});
			});
		}

		if ($('login.logoutLink')) {
			$('login.logoutLink').observe('click',
			function(e) {
				e.stop();
				eraseCookie('key');
				window.location=baseURL;
			});
		}
	}
}

contactClass = Class.create();
contactClass.prototype = {
	initialize: function() {
		if ($('contact.submit')) {
			$('contact.submit').observe('click',
			function(e) {
				e.stop();
				x_call("contact", "input", "content",
				{
					name: $F('name'),
					email: $F('email'),
					subject: $F('subject'),
					message: $F('message')
				});
			});
		}
	}
}

quickSearchClass = Class.create();
quickSearchClass.prototype = {
	initialize: function() {
		if ($('quickSearch.input')) {
			$('quickSearch.input').value = "Search";
			$('quickSearch.input').observe('blur',
			function(e) {
				if(this.value=='') this.value='Search';
			});
			$('quickSearch.input').observe('focus',
			function(e) {
				if(this.value=='Search') this.value='';
			});
		}
		if ($('search.keywords')) {
			$('search.keywords').value = "- Keywords -";
			$('search.keywords').observe('blur',
			function(e) {
				if(this.value=='') this.value='- Keywords -';
			});
			$('search.keywords').observe('focus',
			function(e) {
				if(this.value=='- Keywords -') this.value='';
			});
		}
	}
}

function kpHandler(event,func) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
 	if (keyCode == 13) {
		func();
	}
  return true;
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*50*50*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function loginOk(key) {
	createCookie("key", key, 0);
}