var frmCart;
var frmProduct;
var frmContact;

/* Popup links */
function popupWindow(theURL, winName, winWidth, winHeight) {
	var posLeft = (Math.floor(screen.width/2)) - (Math.floor(winWidth/2));
	var posTop = (Math.floor(screen.height/2)) - (Math.floor(winHeight/2));

	newWindow = window.open(theURL,winName, "width=" + winWidth + ",height=" + winHeight + ",left=" + posLeft + ",top=" + posTop + ", location=0, menubar=0, resizable=1, scrollbars=1, status=0, toolbar=0");
	newWindow.focus();

	return false;
}

/* Allows chained onload's */
function addLoadEvent(func) {
	var oldonload = window.onload;

	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function getRPC(type) {
	if (!type)
		return false;

	$('rpc').style.display = "";	
	$('rpc-content-inner').style.display = "";
	$('rpc-content-inner').innerHTML = '<div style="padding: 10px 0"><img src="images/loading-bar-wht.gif" width="102" height="5" border="0" vspace="10" alt="Loading..." /></div>';
	
	var request = function() {
		$('frmContact') ? frmContact = new SmartForm('#frmContact', { objNme: 'frmContact', required: [['name','string','singleword'],['email','email'],'message'], processor: 'ajax.php?contact=process&sid=ma25d1kt0k7m79tcp8n1c7qm16', ignoreEmpty: true, evalScripts: true }) : null;

		if ($('frmCart')) {
			fields = new Array();

			for (var i=0; i<$('frmCart').elements.length-1; i++) {
				fields[i] = $('frmCart').elements[i].name;
			}

			frmCart = new SmartForm('#frmCart', { objNme: 'frmCart', required: fields, processor: 'ajax.php?action=cart_update&sid=ma25d1kt0k7m79tcp8n1c7qm16', ignoreEmpty: true, evalScripts: true });
		}
	}

	new ajax('ajax.php?'+type+'&sid=ma25d1kt0k7m79tcp8n1c7qm16', { update: 'rpc-content-inner', onComplete: function() { request();  }.bind(this) }).request();
}

/* Close default RPC container... */
function closeRPC() {		
	$('rpc').style.display = "none";
	$('rpc-content-inner').innerHTML = "";
}

/* RPC SearchResults... */
function getSearch(theForm) {
	if (!theForm)
		return false;

	SelectedCategory = theForm.cPath.options[theForm.cPath.selectedIndex].value;

	if (SelectedCategory > 0) {
		getWheelList(1, '&category='+SelectedCategory);
	} else {
		getWheelList(1);
	}
}

/* RPC Get List Wheels... */
function getWheelList(page, params) {
	if (!page)
		return false;

	$('sidebar').innerHTML = '<div style="padding: 10px 0"><img src="images/loading-bar-wht.gif" width="102" height="5" border="0" vspace="10" alt="Loading..." /></div>';

	if (!params) params = '';

	new ajax('ajax.php?list&page='+page+params+'&recursive=1&sid=ma25d1kt0k7m79tcp8n1c7qm16', { update: 'sidebar' }).request();
}

/* RPC Get Single Wheel Post... */
function showWheel(id) {
	if (!id)
		return false;

	$('content').innerHTML = '<div style="padding: 10px 0"><img src="images/loading-bar-wht.gif" width="102" height="5" border="0" vspace="10" alt="Loading..." /></div>';

	var request = function() {
		if ($('frmProduct')) {
			fields = new Array();

			for (var i=0; i<$('frmProduct').elements.length-1; i++) {
				fields[i] = $('frmProduct').elements[i].name;
			}

			frmProduct = new SmartForm('#frmProduct', { objNme: 'frmProduct', required: fields, processor: 'ajax.php?products='+id+'&action=cart_add&sid=ma25d1kt0k7m79tcp8n1c7qm16', ignoreEmpty: true, evalScripts: true });

			// load form
			$('tellAFriendForm') ? friend = new Friend() : null;
		}
	}

	new ajax('ajax.php?products='+id+'&sid=ma25d1kt0k7m79tcp8n1c7qm16', { update: 'content', onComplete: function() { request();  }.bind(this) }).request();
}

function listTab(obj) {
	var lis = document.getElementById('listnums').getElementsByTagName('span');	
	var name = "itemblock";

	for (var i=0; i<lis.length; i++) {
		var childName = name+(i+1);
		var child = document.getElementById(childName);

		if (child == null)	continue;		

		if(lis[i] == obj) {
			lis[i].className = "active";
			child.style.display = "";
		} else {
			lis[i].className = "";
			child.style.display = "none";
		}
	}
}

function switchImage(image) {
	$('bigWheel') ? $('bigWheel').src = image : null;
}

function loadImage(obj) {
	tmp = obj.split('/');
	image = tmp[tmp.length-1];

	$('modalUpdate') ? $('modalUpdate').innerHTML = '<p align="center"><img src="images/products/large/'+image+'" /></p>' : null;

	new Modal(null, 'modalPanel');
}

function enlargeImage(image) {
	$('modalUpdate') ? $('modalUpdate').innerHTML = '<p align="center"><img src="'+image+'" /></p>' : null;

	new Modal(null, 'modalPanel');
}

/* tell a freind form class */
var friend;
var frmFriend;

var Friend = new Class({
	initialize:function() {
		this.f = $('tellAFriendForm');
		this.f.innerHTML = '';
	},

	showHide:function(id) {
		if (!this.f.innerHTML || this.f.innerHTML == '') {
			new ajax('ajax.php?tell_a_friend&'+id+'&sid=ma25d1kt0k7m79tcp8n1c7qm16', { method: 'get', update: 'tellAFriendForm', onComplete: function() { this.prepareForm(id); }.bind(this) }).request();
		} else {
			this.f.innerHTML = '';
		}
	},

	prepareForm:function(id) {
		frmFriend= new SmartForm('#frmFriend', { objNme: 'frmFriend', required: ['from_name','from_email_address','to_name','to_email_address','message'], processor: 'ajax.php?tell_a_friend&'+id+'&action=process&sid=ma25d1kt0k7m79tcp8n1c7qm16', ignoreEmpty: true, evalScripts: true });
	}
});
