$(document).ready(function(){
    
    /* fix search box shadow */
    setTimeout(function(){
        $("#search-div .shadow-left,#search-div .shadow-right").height($("#search-div .search-content").height()+"px");
    }, 100);
    /* fix search box shadow */



    /* IE 7 bug fix for printer image background */
    if (jQuery.browser.msie && jQuery.browser.version < 8 && jQuery.browser.version >= 7) {
        if($("#printer-background").length != 0){
            $("#printer-logo").after("<div id='ie-7-printer-bg-fix'>&nbsp;</div> ");}
    }
    /* IE 7 bug fix for printer image background */
    
    /* IE 7 / IE 8 rounded corners */
    if (jQuery.browser.msie && jQuery.browser.version < 9) {
        $("#document-content").corner();
        $("table.products-table tr td span.discount, .product-details #product-info #product-attributes table.first-table tr td.price span.discount").corner("5px");
        $("li.faq-questions>:last-child").css({marginBottom:0,paddingBottom:0});
    }
    /* IE 7 / IE 8 rounded corners */

    /* Product table tr click*/
    $("table.products-table tr").live("click",function(e){
        if($(this).closest("#basket-content").length == 0)
        {
            var $tgt = $(e.target);
            if( ($tgt.is('td') || $tgt.is('img')) && $tgt)
            {
                if($(this).find("a:first").length) // have link
                    window.location.href = $(this).find("a:first").attr("href");
                else
                {
                    $(this).css("cursor", "auto");
                }
            }
        }
    });
    $("#basket-content table.products-table tr").css("cursor", "auto");
    /* Product table tr click*/



    /* search input functions */
    $("#search-input-span").click(function(){$("#search-input").focus()});
    $('#search-input').live("focus", function () {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
        }
    }).live("blur", function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
        }
    });
    $('#search-input').live("keypress", function(e) {
      code = e.keyCode ? e.keyCode : e.which;
      if(code.toString() == 13) {
          $('#search-button').click();
      }
    });
    $("#search-button").click(function(e){
        e.preventDefault();
        var val = $('#search-input').val();
        var link = WEB_ROOT + '/index.php?page=modules/lucene/search_product&command=start&persist=new&search=' + val;
        if(val != "" && val != $('#search-input').attr("title"))
            window.location.href = link;
        else if($("#brand").val() != "0" && $("#printer_type").val() == "0")
        {
            // redirecting to the brand page
            window.location.href = WEB_ROOT + '/index.php?page=modules/advancedcategories/advanced_search_redirect&brand_id=' + $("#brand").val();
        }
        else if($("#brand").val() != "0" && $("#printer_type").val() != "0" && $("#models").val() == "0")
        {
            // redirecting to the brand TYPE page
            window.location.href = WEB_ROOT + '/index.php?page=modules/advancedcategories/advanced_search_redirect&brand_id=' + $("#brand").val() + '&type_id=' + $("#printer_type").val();
        }

    });
    /* search input functions */



    /* search autocomplete */
    $("#search-input").autocomplete("index.php?page=modules/lucene/callbacks/search", {
		width: ($("#search-input").width() + 5),
		minChars:3,
        delay:250,
        max:50,
        scrollHeight:340,
        formatResult: function formatResult(row) {
            return row[0].replace(/(<.+?>)/gi, '');
        }
	}).result(selectedResultLog);
    function selectedResultLog(event, data, formatted) {
        if(data)
        {
            var val = $('#search-input').val();
            var link = WEB_ROOT + '/index.php?page=modules/lucene/search_product&command=start&persist=new&search=' + val;
            if(val != "" && val != $('#search-input').attr("title"))
                window.location.href = link;
        }
	}
    /* search autocomplete */


    
    /* slogan message rotate */
//    rotateSlogan();
    /* slogan message rotate */


    /* nice search selects */
    reloadNiceSelects();

    $("#brand").prev(".selectPrintle").unbind('click');
    $("#brand").prev(".selectPrintle").click(function(){
        getBrandsSelect();
    });
    /* nice search selects */

    $("#twitter-content a").each(function(){
        $(this).attr("target", "_blank");
    });

    /* Scrolled search div */
    var scroller = new StickyScroller(".half-fixed",
    {
        start: 160,
        end: $(document).height(),
        interval: 0,
        range: 0,
        margin: 20
    });
    
    scroller.onScroll(function(index)
    {
        if(index == 0 && jQuery.browser.msie)
        {
            if(jQuery.browser.version < 8)
                $(".half-fixed").css({top : "-150px"});
            else
                $(".half-fixed").css({top : "-140px"});
        }
    });
    /* Scrolled search div */

    /* IE div height fix*/
    if (jQuery.browser.msie)
    {
        $("#right-box").resize(function(){

            var dh = 0;
            var ch = 0;
            if(jQuery.browser.version >= 9 && jQuery.browser.version < 10)
            {
                //ie9
                dh+=60;
                ch+=60;
            }
            if(jQuery.browser.version >= 8 && jQuery.browser.version < 9)
            {
                //ie 8
                dh+=55;
                ch+=35;
            }
            if(jQuery.browser.version >= 7 && jQuery.browser.version < 8)
            {
                //ie 7
                dh+=45;
                ch+=25;
            }

            dh+=$("#right-box").height();
            ch+=$("#right-box").height();
            dh+=$("#footer").height();
            ch+=$("#footer").height();

            $("#document-content").height(dh+"px");
            $("#document-content").children().first().height(ch+"px");
        });
    }
    /* IE div height fix*/

    /* FAQ */
    $(".faq-title").click(function(e){
        var elem = $("#"+$(this).attr("id")+"-li");
        elem.slideToggle(100);
        $(this).toggleClass("faq-title-active");
        e.preventDefault();
    });
    
    if(window.location.href.indexOf("#") != -1 && window.location.href.indexOf("#q-") != -1)
    {
        $(window.location.href.substr(window.location.href.indexOf("#"), window.location.href.length)).click();
    }

    $(".faq-link").click(function(event){
        event.preventDefault();

        var id = $(this).attr("id");

        switch(id)
        {
            case "bestellen-link":
                if($(".main-faq-ul").length > 0)
                    $("#q-3").click();
                else
                    window.location.href = WEB_ROOT + "/index.php?page=modules/faq/faq#q-3";
                break;
            case "betalen-link":
                if($(".main-faq-ul").length > 0)
                    $("#q-4").click();
                else
                    window.location.href = WEB_ROOT + "/index.php?page=modules/faq/faq#q-4";
                break;
            case "levering-link":
                if($(".main-faq-ul").length > 0)
                    $("#q-5").click();
                else
                    window.location.href = WEB_ROOT + "/index.php?page=modules/faq/faq#q-5";
                break;
            case "terugsturen-link":
                if($(".main-faq-ul").length > 0)
                    $("#q-6").click();
                else
                    window.location.href = WEB_ROOT + "/index.php?page=modules/faq/faq#q-6";
                break;
        }
    });
    /* FAQ */

    /* IE9 jquery load table bug fix */
    if (jQuery.browser.msie && jQuery.browser.version >= 9 && jQuery.browser.version < 10) {
        $('table').each(function(){
            $(this).html($(this).html().replace(/td>\s+<td/g,'td><td'));
        });
    }
    /* IE9 jquery load table bug fix */

    $(".firstLetter").click(function(e){
        e.preventDefault();

        $('html,body').animate({scrollTop: $($(this).attr("href")).offset().top},'slow');
    });

    $(".rounded-input").each(function(){
        $(this).wrap('<span class="rounded-input-beginning '+$(this).attr("class")+'" style="width: '+$(this).width()+'px"/>');
        $(this).wrap('<span class="rounded-input-end" />');
    });


    /* Filter link click */
    $(".categories-filter-link").click(function(e){
        e.preventDefault();

        $("#" + $(this).attr("id") + "-details").toggle();
    });
    /* Filter link click */

    /* Basket link click */
    $("#basket #link a").click(function(e){
        e.preventDefault();
        if(basketLinkClickCount == 0)
        {
            basketLinkClickCount = 1;
            window.location.href = $(this).attr("href");
            $(".basket-button").attr("onclick", "");
        }
    });
    $(".basket-button").live("click",function(e){
        e.preventDefault();
        if(basketLinkClickCount == 0)
        {
            basketLinkClickCount = 1;
            var onClick = $(this).attr("onclick");
            $(this).attr("onclick", "");
            onClick();
        }
    });
    /* Basket link click */
});
var basketLinkClickCount = 0;
var currentProductsTable = null;
var messageIndex = -1;
var message = false;
function rotateSlogan()
{
    $('.message').hide();
    if(messageIndex == -1)
    {
        message = $('.message:eq(0)');
        message.fadeIn(800);
        messageIndex = $('.message').index(message);
    }
    else
    {
        var is = false;
        messageIndex++;
        $('.message').each(function(){
            if(messageIndex == $('.message').index($(this)))
            {
                is = true;
                $(this).fadeIn(800);
            }
        });

        if(!is)
        {
            messageIndex = -1;
            rotateSlogan();
            return false;
        }
    }
    setTimeout("rotateSlogan()", refresh1*1000);
    return false;
}

function reloadNiceSelects()
{
    var width = $("#page-content #search-div .middle .search-content select").width();
    if(width < 200)
        width = 176;
    else
        width = 248;

    var options = {styleClass: "selectPrintle", jScrollPane: 0}
    $(".nice-select, #customer-form-data select").styleSelect(options);
    $(".selectPrintle").width(width+"px");
}

function addToBasket(currentProductId, page)
{
    if($(".basket-button").is(":hidden"))
        $(".basket-button").fadeIn(500);

    $("#basket-amount-total").html("<img style='width: 12px;height: 12px' src='"+WEB_ROOT+"/images/ajax-loader.gif' alt='Loading'/>");
    $("#basket-price-total").html("<img style='width: 12px;height: 12px' src='"+WEB_ROOT+"/images/ajax-loader.gif' alt='Loading'/>");

    /* popup */
    var tr;
    if(currentProductsTable != null)
        tr = currentProductsTable.find("#quantity_" + currentProductId).closest("tr");
    else
        tr = $("#quantity_" + currentProductId).closest("tr");
    var table = tr.closest("table");
    var style = "";
    var newPosition;
    if(table.attr('class').indexOf("products-table") != -1)
    {
        newPosition = tr.position();
        style += "style='top:"+(newPosition.top-10)+"px;";
        newPosition = tr.find("td:nth-child(3)").position();
        style += "left:"+newPosition.left+"px;";
    }
    else if(table.attr('class').indexOf("second-table") != -1)
    {
        newPosition = $('.first-table').find("tr:nth-child(3)").position();
        style += "style='top:"+ (newPosition.top - 38) +"px;";
        newPosition = $("#product-attributes").position();
        style += "left:"+(newPosition.left-20)+"px;";
    }
    style += "'";

    var div = $("<div class='arrow-left' "+style+"></div><div class='added-to-basket-notice' "+style+"><span>"+in_basket_message+"</span></div>")
    table.after(div);
    if (jQuery.browser.msie && jQuery.browser.version < 9) {
        $(div).corner("3px");
    }
    $(div).fadeIn(300).delay(3000).fadeOut(1000, function(){$(this).remove();});
    /* popup */

    var currentAmount = $("#quantity_" + currentProductId).val();
    $.post('index.php',{ page: "ajax/cart_ajax", command: "cart", productid:currentProductId, amount:currentAmount }, function(data) {
        $("#basket-amount-total").html(data.basket_items);
        $("#basket-price-total").html(data.basket_total);
    }, "json");

    return false;
}
