$(document).ready(function() {

	// modal windows

	$("a.fancybox").colorbox({'opacity': 0.4, 'title': ' '});
	$("a.modal_inline").colorbox({'opacity': 0.4});
	
	$('a.modal, a.shop_locator').each(function(){
		if($(this).attr('href').indexOf("?") == -1){
			$(this).attr('href', $(this).attr('href') + '?modal=1' );
		}else{
			$(this).attr('href', $(this).attr('href') + '&modal=1' );
		}
	});
		
	$("a.modal").colorbox({
				'opacity': 0.4, 'width':'700px', 'height':'75%', 'iframe': true, 'title':'<div id="modal_back"></div><a style="float:left;margin:0 0 0 5px" href="javascript:void(0)" onclick="window.frames[0].print();">Print This</a><a style="float:right;margin: 0 30px 0 0;" href="javascript:void(0)" onclick="$.colorbox.close();">Close</a>'});
	
	$("a.shop_locator").colorbox({'opacity': 0.4, 'width':'700px', 'height':'530px', 'iframe': true, 'title':'<div id="modal_back"></div><a style="float:left;margin:0 0 0 5px" href="javascript:void(0)" onclick="window.frames[0].print();">Print This</a><a style="float:right;margin: 0 30px 0 0;" href="javascript:void(0)" onclick="$.colorbox.close();">Close</a>'});
	
	if($.browser.msie && $.browser.version=="6.0"){
	
		var no_ajax = true;
	
	}else{
	
		// add to cart ajax (links)
	
		$(".button_buy").bind("click", function(){
		
			add_to_cart($(this).attr('href'));
		
			return false;
		
		});
		
		// add to cart ajax (forms)
		
		$("#product").bind("submit", function(){
		
			add_to_cart_post();
		
			return false;
		
		});
	
	}

		
});


// add to cart for buy links

function add_to_cart(add_url){

	if(document.location.protocol == "https:") add_url = add_url.replace('http://','https://');
	
	if(document.location.protocol == "http:") add_url = add_url.replace('https://','http://');

	// show modal window
	
	$.colorbox({inline:true, href:'#added_to_cart', 'opacity':0.4}).trigger('click');

	$.get(add_url+"&ajax=1", function(data){
	
		// update basket summary
	
		$("#header_col_one").load(location.href+" #header_col_one>*",function(){
		
			// update modal window
			
			data = data.replace("{total}",$("#basket_total").html());
		
			$("#added_to_cart").html(data);
		
		});
	
	});

}

function add_to_cart_post(){

	// show modal window
	
	$.colorbox({inline:true, href:'#added_to_cart', 'opacity':0.4}).trigger('click');

	$.post("/index.php?route=checkout/cart&ajax=1", $("#product").serialize(), function(data){
	
		// update basket summary
	
		$("#header_col_one").load(location.href+" #header_col_one>*",function(){
		
			// update modal window
			
			data = data.replace("{total}",$("#basket_total").html());
		
			$("#added_to_cart").html(data);
		
		});
	
	});

}


function getURLVar(urlVarName) {
	var urlHalves = String(document.location).toLowerCase().split('?');
	var urlVarValue = '';
	
	if (urlHalves[1]) {
		var urlVars = urlHalves[1].split('&');

		for (var i = 0; i <= (urlVars.length); i++) {
			if (urlVars[i]) {
				var urlVarPair = urlVars[i].split('=');
				
				if (urlVarPair[0] && urlVarPair[0] == urlVarName.toLowerCase()) {
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	
	return urlVarValue;
}

$('#search input').keydown(function(e) {
	if (e.keyCode == 13) {
		moduleSearch();
	}
});

function moduleSearch() {
	url = 'index.php?route=product/search';
	
	var filter_keyword = $('#filter_keyword').attr('value')
	
	if (filter_keyword) {
		url += '&keyword=' + encodeURIComponent(filter_keyword);
	}
	
	url += '&description=0';
	
	location = url;
}
