function formRequest(target, address) { formRemove(target);var headElement = document.getElementsByTagName("body").item(0);var scriptTag = document.createElement("script");scriptTag.setAttribute("id", "jscript"+target);scriptTag.setAttribute("type", "text/javascript");scriptTag.setAttribute("src", address+"&rand=" + Math.random());headElement.appendChild(scriptTag);}function formRemove(target){var headElement = document.getElementsByTagName("body").item(0);if (script = document.getElementById('jscript'+target)){headElement.removeChild(script);}}

function setCart() {
	document.getElementById('shopcart').innerHTML = document.getElementById('shopcartHolder').innerHTML;
}

function addTo(type, backorder) {
	if (backorder == "Y") {
		alert("Item on back order. Please check back soon!");
		return false;
	}
	
	var data = new Array('item_session_id', 'item_product_id', 'item_qty', 'item_product_price', 'item_model_id', 'item_model_picture_set_id');
	
	//see how many attributes on on this page
	var count = 0;
	while (true) {
		count++; 
		if (document.getElementById('attribute_'+count))
			data.push('attribute_'+count);
		else
			break;
	}

	var string = "";
	for (i in data) {
		if (document.getElementById(data[i]).value == null || document.getElementById(data[i]).value == "") {
			alert("Please select one of each option!");
			return false;
		}
		string = string + data[i] + "=" + escape(document.getElementById(data[i]).value) + "&";
	}
	formRequest('addTo', 'AJAX/'+type+'_add.php?'+string);
}

$(document).ready(function() { updateCart(); });
function updateCart() {
	//setup query
	var string = "";
	if (typeof(products) != "undefined") {
		for (i in products) {
			string = string+"item_product[]="+products[i]+"&";
		}
	}
	formRequest('cart', 'AJAX/cart_get.php?' + string);
}

function newWindow(url, width, height) {
    if((!newWin) || (newWin.closed == true)) {
        var newWin = window.open(url,"newWindow",'width=' + width + ',height=' + height + ',resizable=no,scrollbars=yes,menubar=no,toolbar=no,directories=no,location=no,status=no,screenX=550,screenY=20,top=20,left=550');
        newWin.focus();
    }

    else {
        newWin.location.href = url;
        newWin.focus();
    }

}

// canManipulateImages - check if the browser we're using can do
// clever stuff with document images.

function canManipulateImages() {
    if (document.images)
        return true;
    else
        return false;
}
// gImageCapableBrowser - is this browser hip to images? Set up
// a global variable so that we don't have to keep calling a function
// (useful if the function becomes costly to compute).

gImageCapableBrowser = canManipulateImages();

jQuery.fn.center = function() {
    this.css('position', 'absolute');
    this.css('top', ($(window).height() - this.height())/2 + $(window).scrollTop() + 'px');
    this.css('left', ($(window).width() - this.width())/2 + $(window).scrollLeft() + 'px');
    return this;
}

function lightsOff(){
    var fade_in_time = 200;
    var docHeight = $(document).height();
    
    $('body').append('<div id="trashy_dark_bg"></div>');		
    $('#trashy_dark_bg').css({
        'height': docHeight+'px',
        'width': '100%',
        'opacity' : 0.75,
        'position': 'absolute',
        'background-color': 'black',
        'z-index': 100,
        'display':'none'
    })
    .bind('click', function(){
        lightsOn();
    });
    
    $('#trashy_dark_bg').center().fadeIn(fade_in_time);
    $('#trashy_swatch_wrapper').center().fadeIn(fade_in_time);
}

function lightsOn(){
    var fade_out_time = 200;
    $('#trashy_swatch_wrapper').fadeOut(fade_out_time);
    $('#trashy_dark_bg').fadeOut(fade_out_time, function(){
        $(this).remove();
    });
}
