var currentBanner = 1;
var numberBanners;

Cufon.replace('.nav-cufon, .title h2, .title p, #banners h2', {fontFamily: 'Baar Zeitgeist', hover: true});
Cufon.replace('h1, .featured-large h2, h3, .product-row ul li h2, #left-nav h2, #content h2, .search label:first-child, #content form label, #header-contact h2, #header-contact p', {fontFamily: 'Sylfaen'});

$(document).ready(function() { 

	if($('body.site').length > 0){
		setupBanner();
		getBasket();
	}

	$('a.thickbox').colorbox({transition:'elastic', speed:500});
	$('a.gps').colorbox({transition:'elastic', speed:500, height: "100%", width: "100%", iframe: true});
	
	if($('#left-nav').height() < $("#content").height()){
		$('#left-nav').css({'height' : $("#content").height()});
	}
	
	$("a[rel=external]").click(function(){
		window.open(this.href);
		return false;
	});
	
    $('.scroll-left').click(function(){
    	bannerChange('left');
    	return false;
    });
    
    $('.scroll-right').click(function(){
    	bannerChange('right');
    	return false;
    });
    
    $('.scroll-left').hide();
    
    $('#module_search input').keydown(function(e) {
		if (e.keyCode == 13) {
			moduleSearch();
		}
	});
}); 

function getBasket(){
	$.ajax({
		type: 'get',
		url: '/store/v2/index.php?route=module/cart/callback',
		dataType: 'html',
		success: function (html) {
			noItems = $(html).find('dd.cart-items').html();
			if(noItems > 0){
				// Show cart and load
				$('#module_cart .middle').html(html);
				$('#module_cart').fadeIn(1000);
				
			} else{
				// Show phone number
				$("#header-contact").fadeIn(100);
			}
		}
	});
}

function showModal(type){

	if(type == "added-to-cart"){
		/*jQuery.facebox(function($) { 
				jQuery.get('/resources/javascript/facebox/added-to-cart.html', function(data) { jQuery.facebox(data) })
			})	*/
			
		$('#module_cart').parent().prepend("<div class='added-to-cart' id='added-to-cart'><h2>Item added to cart</h2></div>");
		$('#added-to-cart').animate({"left": "-=159px"}, "slow", '', function(){
			$(this).animate({opacity: 1.0}, 3000).animate({"left": "+=159px"}, "slow");
		
		});
		
		

	}
}

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

function bannerChange(direction){
	
	numberBanners = $(".banner").length;
	
	if(direction == 'left'){
		$(".banner-container").animate({"left": "+=942px"}, "1000");
		currentBanner--;
	}
	
	if(direction == 'right'){
		$(".banner-container").animate({"left": "-=942px"}, "1000");
		currentBanner++;
	}
	
	if(currentBanner == numberBanners){
		$('.scroll-right').hide();
	}else{
		$('.scroll-right').show();
	}
	
	if(currentBanner == '1'){
		$('.scroll-left').hide();
	} else{
		$('.scroll-left').show();
	}

}

function review(productID) {
	$.ajax({
		type: 'post',
		url: 'index.php?route=product/product/write&product_id='+productID,
		dataType: 'json',
		data: 'name=' + encodeURIComponent($('input[name=\'name\']').val()) + '&text=' + encodeURIComponent($('textarea[name=\'text\']').val()) + '&rating=' + encodeURIComponent($('input[name=\'rating\']:checked').val() ? $('input[name=\'rating\']:checked').val() : '') + '&verification=' + encodeURIComponent($('input[name=\'verification\']').val()),
		beforeSend: function() {
			$('.success, .warning').remove();
			$('#review_button').attr('disabled', 'disabled');
			$('#review_title').after('<div class="wait"><img src="catalog/view/theme/default/image/loading.gif" alt="" /></div>');
		},
		complete: function() {
			$('#review_button').attr('disabled', '');
			$('.wait').remove();
		},
		success: function(data) {
			if (data.error) {
				$('#review_title').after('<div class="warning"><p>' + data.error + '</p></div>');
			}
			
			if (data.success) {
				$('#review_title').after('<div class="success"><p>' + data.success + '</p></div>');
								
				$('input[name=\'name\']').val('');
				$('textarea[name=\'text\']').val('');
				$('input[name=\'rating\']:checked').attr('checked', '');
				$('input[name=\'verification\']').val('');
			}
		}
	});
}

function setupBanner(){

	$('#banners ul').innerfade({ 
		speed: 2000, 
		timeout: 6000, 
		type: 'sequence', 
		containerheight: '230px'
	}); 

}