$(function(){
    init();
});

var search_text = 'Search';
function init(){
    // Blur on focus
    $('a').focus(function() { this.blur(); });
    
    // Active Category menu
    $('a.active').parents('ul').css('display', 'block');
    $('a.active').siblings('ul').css('display', 'block');
    
    // Search
    $('#search-box-go').click(function(){ moduleSearch(); });
    $('#filter_keyword').val(search_text);
    $('#filter_keyword').focus(function() {
        if( $(this).val() == search_text )
            $(this).val('').addClass('active');
    }).blur(function() {
        if( $(this).val() == '' )
            $(this).val(search_text).removeClass('active');
    });
    $('#search-box input').keydown(function(e) {
        if (e.keyCode == 13) {
            moduleSearch();
        }
    });
    
    // Slideshow
    $('.fadein').cycle();
    
    // Option, price update
    togglePriceDisplay('pc');
    $('#option-select').change(function(){
        var selected = $("#option-select option:selected").text();
        if( selected == "Folded Card, Blank Inside" ){
            togglePriceDisplay('fcb');
        } else if ( selected == "Folded Card, B&W Inside" ){
            togglePriceDisplay('fcbw');
        } else if ( selected == "Folded Card, Colour Inside" ){
            togglePriceDisplay('fcc');
        } else {
            togglePriceDisplay('pc');
        }
    });
}

function togglePriceDisplay(selected) {
    $('#discount-price div.priceval').hide();
    $('#discount-price div.price-' + selected).show();
}

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;
}

// Switcher
$('.switcher').bind('click', function() {
    $(this).find('.option').slideToggle('fast');
});
$('.switcher').bind('mouseleave', function() {
    $(this).find('.option').slideUp('fast');
}); 
