
var current_tab = '';
function show_tab(tab_name){
    if (tab_name != current_tab){
        if (!$('#' + tab_name + '_tab').get(0)){
            tab_name = current_tab;
        }
        
        $("ul.tabs a.selected").removeClass('selected');
        $(".tab_set div.tab_pane").hide();
        $('#' + tab_name + '_tab').show();
        $('#' + tab_name + '_tab_link').addClass('selected');

        current_tab = tab_name;
    }
}


$(document).ready(function(){
    var tab = document.location.hash;
    if (tab && tab.substr(0,1) == '#'){
        tab = tab.substr(1);
        show_tab(tab);
    }
    else {
        show_tab('description')
    }
});



function sort_products(sel){
    var sort_replaced = false;
    var sort_query = 'sort=' + $(sel).val();
    var query_arr = [];

    if (document.location.search){
        var query = document.location.search.substr(1);
        query_arr = query.split('&');

        for (var q=0;q<query_arr.length;q++){
            if (query_arr[q].indexOf('sort=') == 0){
                query_arr[q] = sort_query;
                sort_replaced = true;
            }
        }

        if (!sort_replaced){
            query_arr.push(sort_query);
        }
    }
    else {
        query_arr.push(sort_query);
    }

    document.location.search = '?' + query_arr.join('&');
}

function set_page_size(sel){
    var page_size_replaced = false;
    var query_arr = [];
    var page_size_query = 'page_size=' + $(sel).val();
    
    if (document.location.search){
        var query = document.location.search.substr(1);
        query_arr = query.split('&');

        for (var q=0;q<query_arr.length;q++){
            if (query_arr[q].indexOf('page_size=') == 0){
                query_arr[q] = page_size_query;
                page_size_replaced = true;
            }
        }

        if (!page_size_replaced){
            query_arr.push(page_size_query);
        }
    }
    else {
        query_arr.push(page_size_query);
    }

    document.location.search = '?' + query_arr.join('&');
}


$(document).ready(function (){

    $('.photo_thumb').click(function(){
        $('#product_image').attr('src', $(this).attr('data-mainurl'));
        $('#product_image').attr('data-rawurl', $(this).attr('data-rawurl'));
        $('#product_image_link').attr('href', $(this).attr('data-rawurl'));
        $('#product_image_link').attr('title', $(this).attr('title'));
    });
    
    try {
        $('#product_image_link').fancybox({
                                    'titlePosition'	: 'over',
                                    'transitionIn'	: 'elastic',
                                    'transitionOut'	: 'elastic'
                            });

            /*$('.video_link').each(function(){
                var embed_html = $(this).data('embedhtml');
                if (embed_html){
                    $(this).fancybox({
                        'content' : embed_html,
                        'transitionIn' : 'elastic',
                        'transitionOut' : 'elastic'
                    });
                }
            });*/
        $('.video_link').each(function(){
            $(this).unbind('click');
            $(this).click(function(){
                var win = window.open('', 'product_video_window', 'toolbar=0,height=400,width=650');
                win.document.open();
                win.document.write('<html><body style="padding:0;margin:0;">' + $(this).data('embedhtml') + '</body></html>');
                win.document.close();
                return false;
            });
        });
    }
    catch (e){
        // fancy box threw error
        $('#product_image_link').unbind('click');
        $('#product_image_link').click(function(){
            window.open($(this).attr('href'), 'product_image_window', 'toolbar=0,height='+$('#product_image').data('raw-height')+',width='+$('#product_image').data('raw-width'));
            return false;
        });
        $('.video_link').each(function(){
            $(this).unbind('click');
            $(this).click(function(){
                var win = window.open('', 'product_video_window', 'toolbar=0,height=400,width=650');
                win.document.open();
                win.document.write('<html><body style="padding:0;margin:0;">' + $(this).data('embedhtml') + '</body></html>');
                win.document.close();
                return false;
            });
        });
    }

    $("ul.tabs a").click(function() {
        show_tab($(this).attr("href").replace('#', ''));
        /* return false because changing the hash adds to the page history */
        return false;
    });
    

});
