var slideshow;
$(document).ready(function(){
	$("#shopping-bag-trigger").click(function(){
		cartCallback();
		
		$("#cart-dropdown").slideToggle('slow', function(){});
		return false;
	});
	
	$('#add_to_cart').removeAttr('onclick');

	$('#add_to_cart').click(function () {
		cartCallback($('#product :input'));
		$("#cart-dropdown").slideToggle('slow', function(){});
		return false;		
	});	
	
	// fill drop down cart in header
	//cartCallback();
	
	if($("#slideshow").length > 0){
		slideshow=new TINY.slideshow("slideshow");
		slideshow.auto=true;
		slideshow.speed=5;
		slideshow.link="linkhover";
		slideshow.info="information";
		slideshow.thumbs="slider";
		slideshow.left="slideleft";
		slideshow.right="slideright";
		slideshow.scrollSpeed=4;
		slideshow.spacing=5;
		slideshow.active="#F36D89";
		slideshow.init('slideshow','image','imgprev','imgnext','imglink');
	}

});

function cartCallback(data){
	var method = "post";
	if(typeof(data) == "undefined" || data == null || data == "")
		method = "get";
		
	$.ajax({
			type: method,
			url: 'index.php?route=module/cart/callback',
			dataType: 'html',
			data: data,
			success: function (html) {
				var arr = html.split("||");
				var items = parseInt(arr[0]);
				if(items == 1)
					$('#bag-items').html(items + " item");
				else
					$('#bag-items').html(items + " items");
				$('.block-content').html(arr[1]);
			}
	});
}

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;
} 

function moduleSearch() {	
	pathArray = location.pathname.split( '/' );
	
	url = location.protocol + "//" + location.host + "/" + pathArray[1] + '/';
		
	url += 'index.php?route=product/search';
		
	var filter_keyword = $('#filter_keyword').attr('value')
	
	if (filter_keyword) {
		url += '&keyword=' + encodeURIComponent(filter_keyword);
	}
	
	var filter_category_id = $('#filter_category_id').attr('value');
	
	if (filter_category_id) {
		url += '&category_id=' + filter_category_id;
	}
	
	location = url;
}
