var carousels = {};
$(document).ready(function(){
    $('#search').submit(function(){
        try{
            if($(this).children('input[name=filter_name]').val() !=''){
                window.location = $(this).attr('action')+'&filter_name='+$(this).children('input[name=filter_name]').val();
            }
        }catch(e){}
        return false;
    });
    $('#menu a').each(function(){
        if($(this).attr('href') == window.location.href)
            $(this).addClass('active');
    });
    $('#cart').click(function(){
        stopPropagation(event);
        if($(this).hasClass('active')){
            $(this).toggleClass('active');
            return false;
        }
        var offset = $(this).offset(),content = $(this).children('.content');
        content.css('width',($(this).width()*1.7));
        content.css('top',(offset.top + $(this).height())).css('left',offset.left);
        $.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').click(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(){
    return false;
    $('#wrapper').css('min-height',0);
    $('#wrapper').css('min-height',$(document).height());
    $('#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 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']);
            }
        }
    });
    return false;
}
function removeFromCart(product_id){
    stopPropagation(event);
    $.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']);
                $('#cart .content').html(json['output']);
            }
        }
    });
    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']);
            }
        }
    });
    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%');
            }
        }
    });
    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']);
            }
        }
    });
}
var timeoutID  = 0;
function message(text){
    clearTimeout(timeoutID);
    $('#message').click(function(){
        $('#message').hide();
    });
    $('#message .content').html(text);
    $('#message').show();
    timeoutID = setTimeout("$('#message').hide();",5000)
}
