
// Product object
var objProduct = function() {
	
	var ProductSize = 0,
		ProductPrice = 0,
		ProductImage = 1,
		ProductImagePath = '',
		ProductImages = [];
	
	var prototype = function() {
	}
	
	return {
		
		init:function() {
		},
		
		getSize:function() {
			return ProductSize;
		},
		
		setSize:function(n_id) {
			var elOn = document.getElementById('size'+ProductSize),
				elOff = document.getElementById('size'+n_id);
			
			if (elOn!=undefined) elOn.className = '';
			elOff.className = 'sizes-on';
			ProductSize = n_id;
		},
		
		setProductImages:function(str_images) {
			ProductImages = str_images.split(',');
		},
		
		setProductImagePath:function(str_path) {
			ProductImagePath = str_path;
		},
		
		setProductImage:function(n_id) {
			var el = document.getElementById('prodimage'),
				elOn = document.getElementById('images-'+ProductImage),
				elOff = document.getElementById('images-'+n_id);
			
			el.src = ProductImagePath + ProductImages[n_id-1];
			elOn.className = '';
			elOff.className = 'image-numbers-on';
			ProductImage = n_id;
		}
		
	}
	
}();

// Cart object
var objCart = function() {
	
	var getSubmitUrl = function() {
		var strPath = window.location.pathname,
			strSize = objProduct.getSize(),
			arrPairs = window.location.search.substring(1).split('&'),
			strQuery = '';
		
		for (var i=0; i<arrPairs.length; i++) {
			var arrPair = arrPairs[i].split('=');
			if (arrPair[0]!='cart' && arrPair[0]!='sid') strQuery += (arrPair.join('=') + '&');
		}
		
		return strPath + '?' + strQuery + 'cart=add&sid=' + strSize;
	}
	
	return {
		
		init:function() {
		},
		
		addToCart:function() {
			if (objProduct.getSize().length) {			
				window.location = getSubmitUrl();
			} else {
				alert('Please select a size before adding to cart.');
			}
		}
		
	}
	
}();

// init objects.
objProduct.init();
objCart.init();
