function prodStyleAddOption(option){
	this.options.push(option);
}

function ProductOption(optionid,optionLabel){
	this.optionid = optionid;
	this.label = optionLabel;
}

function ProductStyle(styleid,styleLabel,styleImage){
	this.styleid = styleid;
	this.label = styleLabel;
	this.image = styleImage;
	this.options = new Array();
	this.addOption = prodStyleAddOption;
}

function prodAddStyle(style){
	this.styles.push(style);
}

function prodFindStyle(styleid){
	for (var i = 0; i < styles.length; i++){
		if (this.styles[i].styleid == styleid)
			return this.styles[i];
	}
	alert("Error - style not found");
	return null;
}

/****/
function prodStartLoading(productid){
	$('prodLoading'+this.productid).style.visibility="visible";
}

function prodStopLoading(productid){
	$('prodLoading'+this.productid).style.visibility = "hidden";
}

function prodShow(){
	this.prodObj.style.top = (Position.cumulativeOffset($('quickAddLink' + this.productid))[1] - 195) + "px";
	this.prodObj.style.left = (Position.cumulativeOffset($('quickAddLink' + this.productid))[0] + 58) + "px";
	//new Effect.Appear(this.prodObj);
	this.prodObj.style.display='block';
}

function prodHide(productid){
	//new Effect.Puff(this.prodObj);
	this.prodObj.style.display = 'none';
}

function prodQuickAdd(){
	this.show();
	this.startLoading();
	var _this = this;

	new Ajax.Request(this.siteRoot + '/store/products/index.cfm?action=ajaxprodoptions&root=' + this.siteRoot + '&productid=' + this.productid,
		{
			'onSuccess' : function (response){
				_this.update(response);
				_this.fixPNG();
				_this.stopLoading();
			},
			'onFailure' : function(response){
				_this.stopLoading();
				alert(response.responseText);
			}
		});
}

function prodUpdate(response){
	this.prodObj.innerHTML = response.responseText;	
}

function prodUpdateOps(styleid){
	this.startLoading();
	$('prodSubmit' + this.productid).style.visibility = 'hidden';
	var _this = this;
	var a = new Ajax.Request(this.siteRoot + '/store/products/index.cfm?action=ajaxstyleoptions&productid=' + this.productid + '&styleid=' + styleid,
		{
		'onSuccess' : function (response){
			_this.stopLoading();
			$('optionContainer' + _this.productid).innerHTML = response.responseText;
			$('prodSubmit' + _this.productid).style.visibility = 'visible';
		},
		'onFailure' : function (response){
			_this.stopLoading();
		}
	});
}

var animArray = new Array();
var animLock = false;

function prodAnimateToCart(img){
	var d = document.createElement("IMG");
	d.src = img.src;
	document.body.appendChild(d);
	d.style.position = 'absolute';
	Position.clone(img,d);

	var starty = parseInt(d.style.top);
	var startx = parseInt(d.style.left);
	var iendx = Position.cumulativeOffset($('cartimage'))[0]+26;
	var iendy = Position.cumulativeOffset($('cartimage'))[1]+23;

	var sw = img.width;
	var sh = img.height;

	var a = new animation(startx,starty,iendx,iendy,sw,sh,d,1);
	new Effect.Opacity(d,{duration:1.0,from:0.4,to: 0.4});
	d.style.zIndex = 4;

while (animLock){	
	}
	var l = animArray.length;
	animArray.push(a);
	var intid = setInterval("toCartHelper(" + l + ")",5);
	a.intid = intid;
	animLock = false;
}


function toCartHelper(index){
	var a = animArray[index];
	a.d.style.top = (a.starty + Math.round((a.iendy-a.starty) * a.pos / 100)) + "px";

	a.d.style.left = (a.startx + Math.round((a.iendx-a.startx) * a.pos / 100)) + "px";

	a.d.style.width = (a.sw - Math.round(a.sw * a.pos/100)) + "px";

	a.d.style.height = (a.sh - Math.round(a.sh * a.pos/100)) + "px";

	if (a.pos == 100){
		clearInterval(a.intid);
		document.body.removeChild(a.d);
	}
	else
		a.pos++;
}



function prodAddToCart(img){
	var s = $('styleID_' + this.productid);
	var o = $('optionID_' + this.productid);
	if (s.options){
		s = s.options[s.selectedIndex].value;
	}
	else
		s = s.value;
	if (o.options){
		o = o.options[o.selectedIndex].value;
	}
	else
		o = o.value;
	this.startLoading();
	var _this = this;
	var a = new Ajax.Request(this.siteRoot + '/store/products/index.cfm?action=ajaxaddCart&root=' + this.siteRoot + '&productid=' + this.productid + '&styleid=' + s + '&optionid=' + o,
		{
			'onSuccess' : function (response){
				_this.stopLoading();
				_this.animateToCart(img);
				scart.updateSummary();
				scart.updateSize();
				scart.updateTotal();				
				},
			'onFailure' : function(response){
				_this.stopLoading();
				alert("Item failed to add: " + response.responseText);
			}
		});
}

function prodFixPNG(){
	if (document.all){
		$('prodTop' + this.productid).src = this.siteRoot + '/images/store/pngfix.png';
		$('prodBottom' + this.productid).src = this.siteRoot + '/images/store/pngfix.png';
		$('prodCall' + this.productid).src = this.siteRoot + '/images/store/pngfix.png';		
		$('prodHeader' + this.productid).style.backgroundImage = 'url(' + this.siteRoot + '/images/store/pngfix.png)';
		$('prodMiddle' + this.productid).style.backgroundImage = 'url(' + this.siteRoot + '/images/store/pngfix.png)';
	}
}


function Product(productid,productname,root,registrar){
	this.productid = productid;
	this.productName = productname;
	this.siteRoot = root;
	this.styles = new Array();
	this.prodObj = $('qp' + this.productid);
	this.registrar = registrar;

	//Commented out because it caused problems with the option/style dropdowns in IE.
	/*new Draggable(this.prodObj);*/
	this.addStyle = prodAddStyle;
	this.findStyle = prodFindStyle;
	this.startLoading = prodStartLoading;
	this.stopLoading = prodStopLoading;
	this.update = prodUpdate;
	this.show = prodShow;
	this.hide = prodHide;
	this.quickAdd = prodQuickAdd;
	this.updateOps = prodUpdateOps;
	this.addToCart = prodAddToCart;
	this.animateToCart = prodAnimateToCart;
	this.fixPNG = prodFixPNG;
	this.positionedElement = new PositionedElement(this.prodObj,$('quickAddLink' + this.productid),58,-195);
	this.registrar.register(this.positionedElement);
}

Product._loadImages = function(){
	Product._siteRoot = Product.siteRoot;
	Product._close_out = new Image();
	Product._close_out.src = Product._siteRoot + '/images/store/close_blue.jpg';

	Product._close_over = new Image();
	Product._close_over.src= Product._siteRoot + '/images/store/close_bl_hl.jpg';

	Product._close_click = new Image();
	Product._close_click.src=Product._siteRoot + '/images/store/close_bl_cl.jpg';
	
	Product._top = new Image();
	Product._top.src = Product._siteRoot + '/images/store/callTopBlue.png';

	Product._header = new Image();
	Product._header.src = Product._siteRoot + '/images/store/blueMiddle2.png';
	
	Product._callout_up_left = new Image();
	Product._callout_up_left = Product._siteRoot + '/images/store/callupleft.png';
	
	Product._callout_up_right = new Image();
	Product._callout_up_right = Product._siteRoot + '/images/store/callupright.png';

	Product._callout_low_left = new Image();
	Product._callout_low_left = Product._siteRoot + '/images/store/calllowleft.png';

	Product._callout_low_right = new Image();
	Product._callout_low_right = Product._siteRoot + '/images/store/calllowright.png';

	Product._bottom = new Image();
	Product._bottom.src = Product._siteRoot + '/images/store/callBottomWhite.png';
	
	Product._middle = new Image();
	Product._middle.src = Product._siteRoot + '/images/store/whitemiddle.png';

	Product._IEPNGFix = new Image();
	Product._IEPNGFix.src = Product._siteRoot + '/images/store/pngfix.png';

}