if (jQuery) {
	jQuery.noConflict();
	
	jQuery('#banner_ad > .banners').cycle({ 
		fx			:  	'fade', 
		timeout		: 	5000
	});
}

var loading_template = new Template(
	'<p style="width: 100%; text-align: center;"><img src="images/throbber.gif" alt="loading..." /><br />Loading...<br />Please wait</p>'
);

var updateCart = function() {
	new Ajax.Request('cart.php', {
		method: 'post',
		parameters: { action: 'get_status' },
		onSuccess: function(r) {
			var resp = r.responseJSON;
			
			if (resp.success) {
				$('product_num').update(resp.products);
			}
		}
	});
};

var init = function() {
	if ($('product_num')) updateCart();
	
	$$('img[id^="switch"]').each(function (ele) {
		ele.observe('click', function (e) {
			$(ele.id.substr(7) + '_list').toggle();			
			ele.src = (ele.src.indexOf('minus.gif') != -1) ? 'images/layout/plus.gif' : 'images/layout/minus.gif';
		});
	});
	
	$$('img[id^="more"]').each(function (ele) {
		ele.observe('click', function (e) {
			$(ele.id.substr(5) + '_more').toggle();			
			ele.src = (ele.src.indexOf('less.gif') != -1) ? 'images/layout/more.gif' : 'images/layout/less.gif';
		});
	});
	
	$$('a.remove_link').each(function(ele) {
		ele.observe('click', function(e) {
			e.stop();
			
			removeProduct(Event.element(e));
		});
	});
};

var addProduct = function(type, ele) {
	switch(type) {
		case 'carpet':
			var params = {
				type: type,
				color_code: ele.getAttribute('color_code'),
				style_code: ele.getAttribute('style_code')
			};
			break;
		
		case 'sheet':
			var params = {
				type: type,
				product_code: ele.getAttribute('product_code'),
				color_code: ele.getAttribute('color_code'),
				pattern: ele.getAttribute('pattern')
			};
			break;
		
		case 'vinyl':
			var params = {
				type: type,
				color_code: ele.getAttribute('color_code'),
				collection: ele.getAttribute('collection')
			};
			break;
		
		case 'tile':
			var params = {
				type: type,
				item_id: ele.getAttribute('item_id')
			};
			break;
		
		case 'wood':
		case 'laminate':
			var params = {
				type: type,
				product_code: ele.getAttribute('product_code'),
				series: ele.getAttribute('series')
			};
			break;
			
		case 'trim':
			var params = {
				type: type,
				product_code: ele.getAttribute('product_code'),
				trim_code: ele.getAttribute('trim_code')
			};			
			break;
			
		case 'wholesale':
			var params = {
				type: type,
				prod_id: ele.getAttribute('prod_id'),
				color_code: ele.getAttribute('color_code')
			};			
			break;
			
		case 'temp_wholesale':
			var params = {
				type: type,
				style_code: ele.getAttribute('style_code'),
				color_code: ele.getAttribute('color_code')
			}
			break;	
	}
	
	if (type == 'trim') {
		var qty_box = ele.getAttribute('trim_code')+'_qty'; 
		params.qty =  $(qty_box).value;		
	}	else {
		params.qty = $F('basket_amt');
	}
		
	if (params.qty == null) return false;
	if (params.qty <= 0) return false;
		
	params.action = 'add_product';
	
	//console.log($H(params).inspect());
	
	new Ajax.Request('cart.php', {
		method: 'post',
		parameters: params,
		onSuccess: function(r) {
			var resp = r.responseJSON;
			
			if (resp.success) {
				alert('Your product has been added to your quote basket.');
				
				updateCart();
			} else {
				alert(resp.error);
			}
		}
	});
};

var removeProduct = function(ele) {
	var type = ele.getAttribute('type');
	
	switch(type) {
		case 'carpet':
			var params = {
				type: type,
				color_code: ele.getAttribute('color_code'),
				style_code: ele.getAttribute('style_code')
			};
			break;
		
		case 'sheet':
			var params = {
				type: type,
				product_code: ele.getAttribute('product_code'),
				color_code: ele.getAttribute('color_code'),
				pattern: ele.getAttribute('pattern')
			};
			break;
		
		case 'vinyl':
			var params = {
				type: type,
				color_code: ele.getAttribute('color_code'),
				collection: ele.getAttribute('collection')
			};
			break;
		
		case 'tile':
			var params = {
				type: type,
				item_id: ele.getAttribute('item_id')
			};
			break;
		
		case 'wood':
		case 'laminate':
			var params = {
				type: type,
				product_code: ele.getAttribute('product_code'),
				series: ele.getAttribute('series')
			};
			break;
			
		case 'trim':
			var params = {
				type: type,
				product_code: ele.getAttribute('product_code'),
				trim_code: ele.getAttribute('trim_code')
			};			
			break;
			
		case 'wholesale':
			var params = {
				type: type,
				prod_id: ele.getAttribute('prod_id'),
				color_code: ele.getAttribute('color_code')
			};			
			break;
			
		case 'temp_wholesale':
			var params = {
				type: type,
				style_code: ele.getAttribute('style_code'),
				color_code: ele.getAttribute('color_code')
			};			
			break;	
	}
	
	params.action = 'remove_product';
	
	new Ajax.Request('cart.php', {
		method: 'post',
		parameters: params,
		onSuccess: function(r) {
			var resp = r.responseText.evalJSON();
			
			if (resp.success) {
				updateCart();
				
				if (resp.product_removed) {
					ele.up(1).fade({
						afterFinish: function() {
							ele.up(1).remove();
						}
					});
				}
					
				$('total_count').update(resp.new_count);
			} else {
				alert(resp.error);
			}
		}
	});
	setTimeout("window.location = 'quote.php';",2000);
};

function spamFreeEmailAddress(linkText,userName,self_titled){
	var domain='unitedflooringmills.com';	
	var emailE = userName + '@' + domain;
	if (self_titled) {
		document.write('<a href="mailto:' + emailE + '">' + emailE + '</a>');
	} else {
		document.write('<a href="mailto:' + emailE + '">' + linkText + '</a>');
	}
}

// NEW STUFF ADDED BY STEPHEN 3/25/11

var open_spec;
var search_parameters = [];

var params_to_query = function(params) {
	return params.collect(function(p) {
		return Object.toJSON(p);
		//return Object.toQueryString(p);
	});
};

window.h4_listener =  function(e) {
	$$('#product_sidebar div[id$=_more]').invoke('hide');
	
	var h4 = Event.element(e);
	
	if (open_spec != null && h4.getAttribute('spec') != open_spec.getAttribute('spec')) {
		open_spec.setStyle({ backgroundImage: 'url(images/bullet_closed.png)' });
		open_spec.up().morph('height: 28px;');
	}
	
	open_spec = h4;
	
	var height = h4.getStyle('height');
	
	if (h4.up().getStyle('height') != '28px') {
		h4.setStyle({ backgroundImage: 'url(images/bullet_closed.png)' });
		h4.up().morph('height: 28px;');
	} else {
		h4.setStyle({ backgroundImage: 'url(images/bullet_open.png)' });
		
		var newheight = h4.next().getStyle('height');
		h4.up().morph('height: ' +  (40 + parseInt(newheight.substr(0, newheight.length - 2))) + 'px;');
	}
};

var jump_listener = function(e) {
	e.stop();
	
	open_spec = null;
	
	var ele = Event.element(e);
	var item = ele.getAttribute('item');
	var value = ele.getAttribute('value');
	
	search_parameters.push({ 'SpecNumber': item, 'SpecDetail': value});
	
	new Ajax.Updater('product_sidebar', 'includes/sidebar_carpet.php', {
		method: 'post',
		evalScripts: true,
		parameters: {
			'search_by_params': true,
			'params[]': params_to_query(search_parameters)
		}
	});
};

var remove_listener = function(e) {
	e.stop();
	
	var newlink = 'carpet.php';
	var ele = Event.element(e);
	var item = ele.getAttribute('item');
	var value = ele.getAttribute('value');
	
	var new_parameters = search_parameters.reject(function(p) {
		return (p.SpecNumber == item && p.SpecDetail == value);
	});
	
	if (new_parameters.length > 0) {
		newlink += '?search_by_params=true&';
		
		new_parameters.each(function(p) {
			newlink += 'params[]=' + p.SpecNumber + '::' + p.SpecDetail + '&';
		});
	}
	
	window.location = newlink;
};

window.more_listener = function(e) {
	e.stop();
	
	$(Event.element(e).getAttribute('value') + '_more').toggle();	
};


Event.observe(window, 'load', init);

