var carousels = {};

			
$(document).ready(function(){
	$('#search').submit(function(){
		window.location =$(this).attr('action') +'&filter_name='+$('input[name=filter_name]',this).val()
});
    $('#menu a').each(function(){
	if($(this).attr('href') == window.location.href)
	    $(this).addClass('active');
    });
	
    $('#cart').click(function(event){
	stopPropagation(event);
	if($(this).hasClass('active')){
	    $(this).removeClass('active');
	    return true;
	}
	
	
	
	var offset = $(this).offset(),content = $(this).children('.content');
	content.css({
	'width':($(this).width()*2.7),
	'left':offset.left,
	'top':(offset.top + $(this).outerHeight())
	});
	
	$.ajax({
	    url: 'index.php?route=checkout/cart/update',
	    dataType: 'json',
	    success: function(json) {
		if (json['output']) {
		    $('#cart .content').html(json['output']);
		}
		$('#cart').toggleClass('active');
	   }
	});
	return false;
    });

    $('#cart').bind('mouseleave', function() {
	$(this).removeClass('active');
    });
    $('#cart .content, #cart .content *').live('click dblclick',function(){
	stopPropagation(event);
    });

    $('.box.category ul a').click(function(){
	if($(this).parent('li').children('ul').length){
	    $(this).parent('li').toggleClass('active');
	    return false;
	}
    });

    $('#languages a').click(function(){
	$('#languages input[name=language_code]').val($(this).attr('hreflang'));
	$('#languages').submit();
    });
    $('#currency a').click(function(){
	$('#currency input[name=currency_code]').val($(this).attr('currency'));
	$('#currency').submit();
    });
//    $('#menu li:last span').hide();
    $('.banner').each(function(){
	$('#'+$(this).attr('id')).cycle({
	    fx:'fade',
	    slideExpr:'div.item'
	})
    });
    $('.carousel').each(function(){
	var element =$(this),id = element.attr('id');
	if(carousels[id]){
	    element.children('li').css('width',carousels[id].width).css('height',carousels[id].height);
	    element.bxSlider(carousels[id]);
	}
    });
    $('.slideshow').each(function(){
	var element = $(this);
	element.nivoSlider({
	    animSpeed:500,
	    pauseTime:9000,
	    afterLoad:function(){
		element.children('.nivo-controlNav').css('margin-left','-'+(element.children('.nivo-controlNav').width()/2)+'px');
	    }
	});
    });
    $('#tabs .tab-title a').click(function(){
	$('#tabs .tab-title a').removeClass('active').removeClass('prev');
	$(this).prev('a').addClass('prev');
	$(this).addClass('active');
	$('#tabs .tab-content .tab').removeClass('active');
	$($(this).attr('href')).addClass('active');
	return false;
    });
    resize();
});
$(window).resize(function(){
    resize();
});
$(window).load(function(){
    productWH('.box-product');
    productWH('#products.grid');
    $('.product-info .gallery').width($('.product-info .gallery .image img').width());
    $('.catwall').each(function(){
	var img = $(this).children('.category:first').children('div.image'),
	width = img.children('a').children('img').width()+
	parseInt(img.css('padding-left'))+parseInt(img.css('padding-right'))+
	parseInt(img.css('border-left-width'))+parseInt(img.css('border-right-width')),
	maxHeight = 0;
	$(this).children('.category').width(width);
	$(this).children('.category').each(function(){
	    maxHeight = Math.max(maxHeight, $(this).height());
	});
	$(this).children('.category').height(maxHeight);
    });
    $('.manwall').each(function(){
	var img = $(this).children('.manufacturer:first').children('div.image'),
	width = img.children('a').children('img').width()+
	parseInt(img.css('padding-left'))+parseInt(img.css('padding-right'))+
	parseInt(img.css('border-left-width'))+parseInt(img.css('border-right-width')),
	maxHeight = 0;
	$(this).children('.manufacturer').width(width);
	$(this).children('.manufacturer').each(function(){
	    maxHeight = Math.max(maxHeight, $(this).height());
	});
	$(this).children('.manufacturer').height(maxHeight);
    });
//    $('a.fancybox img').addimagezoom({
//	zoomrange: [2, 7],
//	magnifiersize: [300,300],
//	magnifierpos: 'right',
//	cursorshade: true,
//	largeimage: function(){
//	    return $(this).parent('a').attr('href');
//	}
//    });
    $('.product-info .gallery .image-additional a').click(function(){
	try {
	    $('#product-image').width($('#product-image').width());
	    $('#product-image').attr('src',$(this).attr('href'));
	    $('#product-image').parent('a').attr('href',$(this).attr('href'));
	} catch (e) {}
	return false;

    });
});
function resize(){
    $('#footer').css('margin-top','-'+$('#footer').height()+'px');
    $('.f_m_line').css('height',$('#footer').height()+30);
}
function stopPropagation(event){
    if(event.stopPropagation)
	event.stopPropagation();
    else if (window.event) {
	window.event.cancelBubble = true;
    }
}
function productWH(element){
    $(element).each(function(){
	var img = $(this).children('.product:first').children('div.image'),
	width = img.children('a').children('img').width()+parseInt(img.css('padding-left'))+parseInt(img.css('padding-right')),
	maxHeight = 0;
	$(this).children('.product').width(width);
	$(this).children('.product').each(function(){
	    maxHeight = maxHeight>$(this).height()?maxHeight:$(this).height();
	});
	$(this).children('.product').height(maxHeight);
	$(this).children('.product')
	.children('.details')
	.css({
	    'position':'absolute',
	    'bottom':$(this).children('.product').css('padding-bottom'),
	    'left':$(this).children('.product').css('padding-left'),
	    'width':width
	});
    });
}
function display(view){
    $.cookie('display',view);
    $('#products').attr('class', view);
    switch(view){
	case 'list':
	    $('#products .product').each(function(index, element){
		var html = $('<div>').addClass('product')
		.html($('<div>').addClass('left').append($('div.image',this)))
		.append(
		    $('<div>').addClass('center')
		    .append($('div.price',this))
		    .append($('div.name',this))
		    .append($('<div>').addClass('clear'))
		    .append($('<div>').addClass('right')
			.append($('div.cart',this))
			.append($('div.compare',this))
			.append($('div.wishlist',this))
			)
		    .append($('<div>').addClass('center')
			.append($('div.description',this))
			.append($('div.rating',this))
			)
		    );
		$(this).replaceWith(html);
	    });
	    break;
	case 'grid':
	    $('#products .product').each(function(){
		var html = $('<div>').addClass('product')
		.html($('div.image',this)).append($('div.name',this))
		.append(
		    $('<div>').addClass('details')
		    .append($('div.price',this))
		    .append($('div.rating',this))
		    .append($('<div>').addClass('clear'))
		    .append($('div.compare',this))
		    .append($('div.cart',this))
		    .append($('<div>').addClass('clear'))
		    .append($('div.description',this))
		    .append($('div.wishlist',this))
		    );
		$(this).replaceWith(html);
	    });
	    productWH('#products.grid');
	    break;
    }
    resize();
}

function runIt(cart_js) {
	if(cart_js == 0){
			$("#second_cart").stop(10000000);
		}else{
			$("#second_cart").effect("pulsate", { times:100 }, 3000);
		}
}
function runCom(cart_js) {
	if(cart_js == 0){
			$("#second_compare").stop(10000000);
		}else{
			$("#second_compare").effect("pulsate", { times:100 }, 3000);
		}
}
function runWish(cart_js) {
	if(cart_js == 0){
			$("#second_wishlist").stop(10000000);
		}else{
			$("#second_wishlist").effect("pulsate", { times:100 }, 3000);
		}
}

$(document).ready(function(){
var a = $(".gadgets #second_cart span").html();	
var a = parseInt(a);
if(a > 0){runIt();}else{}
	
var a = $(".gadgets #second_compare span").html();	
var a = parseInt(a);
if(a > 0){runCom();}else{}

var a = $(".gadgets #second_wishlist span").html();	
var a = parseInt(a);
if(a > 0){runWish();}else{}
});
				
function addToCart(product_id) {
    $.ajax({
	url: 'index.php?route=checkout/cart/update',
	type: 'post',
	data: {
	    product_id:product_id
	},
	dataType: 'json',
	success: function(json) {
	    $('.success, .warning, .attention, .information, .error').remove();
	    if (json['redirect']) {
		location = json['redirect'];
	    }
	    if (json['error']) {
		if (json['error']['warning']) {
		    message(json['error']['warning']);
		}
	    }
	    if (json['success']) {
			message(json['success']);
			$('#cart-total').html(json['total']);
			$('#second_cart_total').html(json['total_count']);
			runIt(json['total_count']);
	    }
	}
    });
    return false;
}
function removeFromCart(product_id){
    $.ajax({
	url: 'index.php?route=checkout/cart/update',
	type: 'post',
	data: {
	    'remove':product_id
	},
	dataType: 'json',
	success: function(json) {
	    $('.success, .warning, .attention, .information').remove();
	    if (json['output']) {
		$('#cart-total').html(json['total']);
		$('#second_cart_total').html(json['total_count']);
		$('#cart .content').html(json['output']);
		runIt(json['total_count']);
	    }
	}
    });
    return false;
}
function addToCompare(product_id) {
    $.ajax({
	url: 'index.php?route=product/compare/update',
	type: 'post',
	data: {
	    'product_id':product_id
	},
	dataType: 'json',
	success: function(json) {
	    $('.success, .warning, .attention, .information').remove();
	    if (json['success']) {
		message(json['success']);
		$('#compare_total').html(json['total']);
		$('#second_compare_total').html(json['second_total']);
		runCom(json['second_total']);
	    }
	}
    });
    return false;
}
function removeFromCompare(product_id){
    $.ajax({
	url: 'index.php?route=product/compare/remove',
	type: 'post',
	data: {
	    'product_id':product_id
	},
	dataType: 'json',
	success: function(json) {
	    if(json.success === true){
		$('table.compare td[rel='+json.id+']').remove();
		$('table.compare thead tr td').attr('colspan', (parseInt($('table.compare thead tr td:first').attr('colspan'))-1));
		$('table.compare').css('width','100%');
		$('#second_compare_total').html(json['second_total']);
		runCom(json['second_total']);
	    }
	}
    });
    return false;
}
function addToWishList(product_id) {
    $.ajax({
	url: 'index.php?route=account/wishlist/update',
	type: 'post',
	data: {
	    'product_id':product_id
	},
	dataType: 'json',
	success: function(json) {
	    $('.success, .warning, .attention, .information').remove();
	    if (json['success']) {
		message(json['success']);
		$('.attention').fadeIn('slow');
		$('#wishlist_total').html(json['total']);
		$('#second_wishlist_total').html(json['second_total']);
		runWish(json['second_total']);
	    }
	}
    });
}
var timeoutID  = 0;
function message(text){
    clearTimeout(timeoutID);
    $('#message').click(function(){
	$('#message').hide();
    });
    $('#message .content').html(text);
    $('#message').show();
    timeoutID = setTimeout("$('#message').hide();",5000)
}

    $(document).ready(function() {
    $('.checkout-content').hide();
    $('#shipping-address .checkout-content').show();
    $(".checkout-heading").click(function(){
        $(this).next().removeAttr('style');
        $('.checkout-content').hide();
        $(this).next().slideToggle("slow");  
    });
                
    $('#shipping-address #button-account').click(function(){
		$('#shipping-address .checkout-content').hide();
        $('#shipping-method .checkout-content').show();
		});
		
	$('#content #ftoc legend input').removeAttr("checked");
	$('#content #attribute_price').hide();
	$('#content legend').next().css({'display':'none'});
	
    });
        
