jQuery(function($) {

    ajaxExchange('authorization?testEntry', '', 'post');

    $('#head .search form[action="shop"] div select[name="shop"]').change( function() {
        if ($(this).val() !== 'all') {
            if ($('#head .search form[action="shop"] div select[name="categorys"]').length !== 0) {
                $('#head .search form[action="shop"] div select[name="categorys"]').removeAttr('name').fadeOut('300', function() {
                    $('#head .search form[action="shop"] div select[rel="' + $('#head .search form[action="shop"] div select[name="shop"]').val() + '"]').attr('name', 'categorys').fadeIn('300');
                });
            } else {
                $('#head .search form[action="shop"] div input[name="categorys"]').removeAttr('name');
                $('#head .search form[action="shop"] div select[rel="' + $(this).val() + '"]').attr('name', 'categorys').fadeIn('300');
            }
        } else {
            $('#head .search form[action="shop"] div select[name="categorys"]').removeAttr('name').fadeOut('300');
            $('#head .search form[action="shop"] div input[rel="all"]').attr('name', 'categorys');
        }
    });

    $('.delivery-link').live('click', function() {
        if ($('.delivery').css('display') === 'none') {
            $('.delivery').fadeIn('300');
        } else {
            $('.delivery').fadeOut('300');
        }
        return false;
    });

    $('.back').live('click', function() {
        history.go(-1);
        return false;
    });

    $('a[href="#"]').live('click', function() {
        return false;
    });

    $('#content .text .block-tile .pages-total-search .display-button a, #content .text .block-line .pages-total-search .display-button a').live('click', function() {
        if ($(this).attr('rel') !== 'active') {
            $('#content .text .block-tile .pages-total-search .display-button a, #content .text .block-line .pages-total-search .display-button a').attr('rel', '');
            $(this).attr('rel', 'active');
            if ($(this).hasClass('display-line')) {
                $('#content .text .block-tile').removeClass('block-tile').addClass('block-line');
                ajaxExchange('config?save',{"config[displayItems]":"block-line"},'post');
            } else {
                $('#content .text .block-line').removeClass('block-line').addClass('block-tile');
                ajaxExchange('config?save',{"config[displayItems]":"block-tile"},'post');
            }
        }
    });

    $('#content .text .block-item .images .photobank a').live('click',function(){
        if(!$(this).hasClass('active')){
            $('#content .text .block-item .images .photobank a').removeClass('active');
            $(this).addClass('active');
            $('#content .text .block-item .images .photo-border .photo-frame img').attr('src',$('#content .text .block-item .images .photogal a').eq($(this).attr('rel')).attr('href'));
            $('#content .text .block-item .images .photo-border').attr('href',$('#content .text .block-item .images .photogal a').eq($(this).attr('rel')).attr('href'));
            $('#content .text .block-item .images .photogal a').attr('rel','prettyPhoto[pp_gal]');
            $('#content .text .block-item .images .photogal a').eq($(this).attr('rel')).attr('rel','');
            $("a[rel^='prettyPhoto']").prettyPhoto({social_tools:'',deeplinking:false});
        }
        return false;
    });

    $('a[href^="http://"]').live('click',function(){
        window.open($(this).attr('href'));
        return false;
    });
});
