//GLOBAL JavaScript

var old_image1 = "";
var old_image2 = "";
var old_color = "";
var colorId = "";
var quantity = "";
var type = "";
var skin_price = "";
var skin_price2 = "";
var special = false;
var skin = "";
var skinid = "";
var old_skin = "";
document.domain = 'ajskins.com';

/*
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
*/
function inOrOut(id, size)
{
	document.getElementById('in_stock').innerHTML = "";
	
	var url = 'include/size_ajax.php?id=' + id + '&size=' + size;
	
	var myAjax = new Ajax(url, {
		method: 'get',
		evalScripts: 'true'
	}).request();
	
	if(size > 44 && skin_price2) {
		adjustPayPal(skin_price2);
	}
	else {
		adjustPayPal(skin_price);
	}
}

function setType(itemType)
{
	type=itemType;
}

function setSpecial(itemSpecial)
{
	special=itemSpecial;
}

function setPrice(p1, p2)
{
	skin_price = p1;
	skin_price2 = p2;
	
	if(skin_price2 > 0) {
		priceHTML = "$" + skin_price + " <span style='font-weight:normal;'>(Sizes 26-44)</span><br /><span style='padding-left:40px;'>$" + skin_price2 + " <span style='font-weight:normal;'>(Sizes 46-60)</span></span>";
	}
	else {
		priceHTML = "$" + skin_price;
		adjustPayPal(skin_price);
		
	}
	
	document.getElementById('price_box').innerHTML = priceHTML;
}

function adjustPayPal(p)
{
	document.getElementById('pp_item_price').value = p;
}

function selectColor(id, color, image1, image2)
{
	//alert(color.replace(/ /g, '_'));
	colorId = id;
	
	if(type == 'belt') {
		$('product_size_select').value = 34;
		inOrOut(id, $('product_size_select').value)
	}
	document.getElementById(color.replace(/ /g, '_')).style.border = "2px solid #333";
	document.getElementById('pp_item_number').value = id;
	setQuantity(id);
	if(old_color && old_color != color){
			$(old_color.replace(/ /g, '_')).style.border = "0";
	}
	if(image1) {
		theImage = $('product_image_1');
		theImage.onclick = function (){largeImage('images/limage1/'+image1); };
		theImage.setAttribute("onClick", "javascript:largeImage('images/limage1/"+image1+"')");
		theImage.parentNode.innerHTML = theImage.parentNode.innerHTML;
	}
	if(image2) {
		theImage = $('product_image_2');
		theImage.onclick = function (){largeImage('images/limage2/'+image2); };
		theImage.setAttribute("onClick", "javascript:largeImage('images/limage2/"+image2+"')");
		theImage.parentNode.innerHTML = theImage.parentNode.innerHTML;
	}
	setPPName(color);
	setIndex('skin_id', skinid);
	setIndex('colorSelect', id);
	old_color = color;
	
}

function setPPName(color){
	name = $('pp_item_name').value
	document.getElementById('pp_item_name').value = name.substr(0, name.indexOf(",")) + ", " + skin + ", " + color;
}

function largeImage(image) {
	props=window.open('/large_image.php?image='+image, 'poppage', 'toolbars=0, scrollbars=0, location=0, statusbars=0, menubars=0, resizable=0, width=600, height=400, left = 100, top = 100');
}

function showSurvey() {
	props=window.open('survey.php', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=0, width=575, height=550, left = 100, top = 100');
}


function setQuan(num)
{
	if(special) {
		$('quantity').disabled = false;
		$('add_to_cart_button').disabled = false;
		$('add_to_cart_button').src = "images/product_page/addtocart.jpg";
		$('add_to_cart_button').style.cursor = "pointer";
	}
	
	if(type == 'belt') {
		$('in_stock').innerHTML = "";
	}
	else {
		$('in_stock2').innerHTML = "";
	}
	var list = document.getElementById('quantity');
	if(list&&list.options.length){
		for(var i=0; i<list.options.length; i++){
			list.options[i].text = list.options[i].value;
			if(list.options[i].value == num){
				list.options[i].text = list.options[i].text + " *";
			}
		}
	}

	if(num == 0){
		if(type == 'belt')
			var element = $('in_stock');
		else
			var element = $('in_stock2');
		
		if(special == 1){
			element.innerHTML = "<a title='cssbody=[tooltip_body] hideselects=[on] cssheader=[tooltip_header] header=[Out of Stock] body=[This item is discontintued and will not be restocked.]'>Out of Stock</a>";
			$('quantity').disabled = true;
			$('add_to_cart_button').disabled = true;
			$('add_to_cart_button').src = "images/product_page/addtocart_disabled.jpg";
			$('add_to_cart_button').style.cursor = "default";
		}
		else {
			element.innerHTML = "<a title='cssbody=[tooltip_body] hideselects=[on] cssheader=[tooltip_header] header=[Out of Stock] body=[Items that are out of stock have a delivery time of 1 to 2 weeks.]'>Out of Stock</a>";
		}
	}
}

function setQuantity(id)
{
	if(type != 'belt'){
		var url = 'include/get_quantity.php?id=' + id;
		var myAjax = new Ajax(url, {
			method: 'get',
			evalScripts: 'true'
		});
		myAjax.request();
	}
}

function setIndex(in_element, in_value) {
	var list = document.getElementById(in_element);
	if(list&&list.options.length){
		for(var i=0; i<list.options.length; i++){
			if(list.options[i].value == in_value){
				list.selectedIndex = i;
				return;
			}
		}
	}
	//else alert("something went wrong");
}

function swapImage(image1, image2)
{
	if(image1) {
		theImage = $('product_image_1');
		old_image1 = theImage.src;
		theImage.src = "images/image1/" + image1;
	}
	if(image2) {
		theImage = $('product_image_2');
		old_image2 = theImage.src;
		theImage.src = "images/image2/" + image2;
	}
}

function selectColorChange(id)
{
	var url = 'include/get_images.php?id=' + id;
	var myAjax = new Ajax(url, {
		method: 'get',
		evalScripts: 'true'
	});
	myAjax.request();
}

function selectSkinChange(id)
{
	old_skin = skin;
	old_color = 0;
	var url = 'include/swap_skin.php?id=' + id;
	var myAjax = new Ajax(url, {
		method: 'get',
		evalScripts: 'true'
	});
	myAjax.request();
}


function revertImages()
{
	if(old_image1) {
		theImage = $('product_image_1');
		theImage.src = old_image1;
	}
	if(old_image2) {
		theImage = $('product_image_2');
		theImage.src = old_image2;
	}
}

function hoverOn(element)
{
	var fx = new Fx.Styles(element, {duration:500, wait:false});
	fx.start({
			'background-color': '#fff',
			color: '#000'
		});
}
function hoverOff(element)
{
	var fx = new Fx.Styles(element, {duration:200, wait:false});
	fx.start({
			'background-color': '#000'
		});
	element.style.background = 'none';
}

