﻿/*disable copy/paste... well, not really "disable", but rather "make it as hard as possible"...*/

$(document).bind("contextmenu", function(e) {
    return false;
});

(function($) { if ($.browser.mozilla) { $.fn.disableTextSelect = function() { return this.each(function() { $(this).css({ "MozUserSelect": "none" }) }) }; $.fn.enableTextSelect = function() { return this.each(function() { $(this).css({ "MozUserSelect": "" }) }) } } else { if ($.browser.msie) { $.fn.disableTextSelect = function() { return this.each(function() { $(this).bind("selectstart.disableTextSelect", function() { return false }) }) }; $.fn.enableTextSelect = function() { return this.each(function() { $(this).unbind("selectstart.disableTextSelect") }) } } else { $.fn.disableTextSelect = function() { return this.each(function() { $(this).bind("mousedown.disableTextSelect", function() { return false }) }) }; $.fn.enableTextSelect = function() { return this.each(function() { $(this).unbind("mousedown.disableTextSelect") }) } } } })(jQuery)

var SiteUrl = "http://www.deknudtmirrors.com/";

$(document).ready(function() {
    $('#tekstenlink').attr("style", "display: none;"); //google link

    if ($(".categoryTip").length > 0) {
        $('.categoryTip').tooltip({
            track: true,
            delay: 0,
            showURL: false,
            fade: 250
        });
    }

    $('#wrapper').disableTextSelect(); //disable selecting
    getSiteUrl(); //alle functies die siteurl gebruiken in de functie WaitForSiteUrl() zetten!

    $("ul[@rel$='subcollection']").hide();

    $("a[@rel$='external']").click(function() {
        this.target = "_blank";
    });

    if ($.browser.msie) {
        if (navigator.appVersion.indexOf("MSIE 7.0") == -1) {
            $('img[@src$=.png]').ifixpng();
            $("#products ul li img").hover(function() {
                $(this).addClass("productOver");
            }, function() {
                $(this).removeClass();
            });
        }
    }

    $(".pressTextBox").focus(function() {
        if ($(this).attr("value") == $(this).attr("title")) {
            $(this).attr("value", "");
        }
    });

    $(".pressTextBox").blur(function() {
        if ($(this).attr("value").length < 1) {
            $(this).attr("value", $(this).attr("title"));
        }
    });

    $("#previous img").click(function() {
        if (!checkIfStopNeededPreviousOnly()) { //next uitschakelen als "einde" bereikt is
            $('#products').trigger('goto', currentPosition - 3);
            $('#products').trigger('start'); //moet na goto komen, anders soms geen start wanneer previous geklikt wordt op "einde" van een scroll
        }
    });

    $("#next img").click(function() {
        if (!checkIfStopNeeded()) { //next uitschakelen als "einde" bereikt is
            $('#products').trigger('start');
            $('#products').trigger('goto', currentPosition + 3);
        }
    });

    $("#ctl00_ctlCollecties_txtCategorie").keyup(function() {
        if ($("#ctl00_ctlCollecties_txtCategorie").val().length >= 4) {
            $("#ctl00_ctlCollecties_txtProduct").focus();
        }
    });

    animateSearch();
});

function animateSearch() {

    $(".inner-list").hide();
    $('#search-close').hide();

    $("#search-open").click(function() {

        $(".inner-list").show();
        $("#searchList").animate({ height: "230px" }, "slow", "swing", function() {
            $("#search-close").show();
            $("#search-open").hide();
//            $("#search-close").show("slow");
//            $("#search-open").hide("slow");        
        });

    });

    $("#search-close").click(function() {

        $("#searchList").animate({ height: "0px" }, "slow", "swing", function() {
            $(".inner-list").hide();
            $("#search-close").hide();
            $("#search-open").show();
//            $("#search-close").hide("slow");
//            $("#search-open").show("slow");           
        });
    });
}

function setNavigationPngTransparent(obj) {
    if ($.browser.msie) {
        if (navigator.appVersion.indexOf("MSIE 6.0") != -1) {
            $(obj).ifixpng();
        }
    }
}

function getPathfotosOnLoad() {
    /* lege selectedcollection betekent ophalen uit sessie (gebeurt in de functie) */
    realtime.GetPathfotos('', $("#ctl00_hdnPart").attr("value"), $("#ctl00_hdnLanguageCode").attr("value"), OnSucceededGetPathfotos, OnFailedGetPathfotos);
}

function getPathfotosOnClick() {
    $('a[@rel=collectionselect]').click(function() {
        //leegmaken searchfields
        if ($("#ctl00_ctlCollecties_txtCategorie").length > 0) {
            $("#ctl00_ctlCollecties_txtCategorie").val("");
        }
        if ($("#ctl00_ctlCollecties_txtProduct").length > 0) {
            $("#ctl00_ctlCollecties_txtProduct").val("");
        }

        // Eerst checken of er subcollecties in zitten.
        if ($(this).next("ul").length > 0) {
            $(this).next("ul[@rel$='subcollection']").slideToggle("slow");
        } else {
            if ($(this).parent().parent().attr("rel") != "subcollection") { //enkel toeschuiven indien niet op een subcollectie geklikt werd
                $("ul[@rel$='subcollection']").slideUp("slow");
            }            
            $('a[@rel=collectionselect]').removeClass();
            $(this).addClass("selected");
            realtime.GetPathfotos($(this).attr("id"), $("#ctl00_hdnPart").attr("value"), $("#ctl00_hdnLanguageCode").attr("value"), OnSucceededGetPathfotos, OnFailedGetPathfotos);
        }
    });
}

function OnSucceededGetPathfotos(result, userContext, methodName) {
    $("#pathfotoslist").html(result);
    enableProductView();
    startSlideshow();
    currentPosition = 0;
    $('#products').trigger('start'); //we forceren nog eens het starten, want een gestopte scroller zal na 1 item gescrolled te hebben terug stoppen wanneer we de scroller terug enablen na het inladen van een nieuwe lijst
    $('#products').trigger('goto', [0]); //fix voor zoekresultaten

    checkIfStopNeeded();
}

function OnFailedGetPathfotos(error, userContext, methodName) {
    $("#pathfotoslist").html("Er is een fout opgetreden, probeer later opnieuw aub...");
}

var currentPosition = 0;

function startSlideshow() {
    if ($("#products").length > 0) {
//        //        alert($("#products li").length);
//        var number = ((($("#products").width() / 110).toString()).split('.'))[0] - 1;
//        alert(number);
//        var aantal = $("#products li").length - 1;

//        if (aantal < number) {
//            number = aantal;
//        }

        $('#products').serialScroll({
            items: 'li',
            axis: 'x',
            step: 1,
            offset: 0,
            start: 0,
            duration: 800,
            force: true,
            lazy: true,
            lock: false,
            interval: 2500,
            constant: false,
            stop: true,
            //exclude: number, //niet bruikbaar, previous/next flippen als dit aanligt
            onBefore: function(e, elem, $pane, $items, pos) {
                currentPosition = pos;
                checkIfStopNeeded();
            },
            cycle: false //don't pull back once you reach the end
        });
    }
}

function checkIfStopNeeded() {
    var number = ((($("#products").width() / 110).toString()).split('.'))[0] - 1;
    if ((number >= $("#products li").length - currentPosition - 1) || (number >= $("#products li").length)) {
        $('#products').trigger('stop');
        return true;
    }
    else {
        return false;
    }
}

function checkIfStopNeededPreviousOnly() {
    var number = ((($("#products").width() / 110).toString()).split('.'))[0] - 1;
    if (number >= $("#products li").length) {
        $('#products').trigger('stop');
        return true;
    }
    else {
        return false;
    }
}

////zoekfct oud
//function searchArtikelen() {
//    if ($("#ctl00_ctlCollecties_txtCategorie").attr("value").length > 0 || $("#ctl00_ctlCollecties_txtProduct").attr("value").length > 0) {
//        realtime.GetPathfotosSearch($("#ctl00_ctlCollecties_txtCategorie").attr("value"), $("#ctl00_ctlCollecties_txtProduct").attr("value"), $("#ctl00_hdnPart").attr("value"), $("#ctl00_hdnLanguageCode").attr("value"), OnSucceededGetPathfotos, OnFailedGetPathfotos);
//        realtime.GetSearchedCollection($("#ctl00_ctlCollecties_txtCategorie").attr("value"), OnSucceededGetSearchedCollection, OnFailedGetSearchedCollection);
//    }
//    else {
//        realtime.GetPathfotos($('a[@class=selected]').attr("id"), $("#ctl00_hdnPart").attr("value"), $("#ctl00_hdnLanguageCode").attr("value"), OnSucceededGetPathfotos, OnFailedGetPathfotos);
//    }
//}

function enableProductView() {
    $(".collectieFoto").click(function() {
        $("#homepage").html("");
        $("#moviepage").html("");    
        $(this).removeAttr("href");
        realtime.GetArtikelDetail($(this).attr("id"), $("#ctl00_hdnLanguageCode").val(), OnSucceededGetProductDetail, OnFailedGetProductDetail);
        $("#content").html("<div id=\"loading\"><img src=\"" + SiteUrl + "images/ajax-loader.gif\" alt=\"\" /></div>");

        $("#products").trigger('stop');
    });
}

function OnSucceededGetProductDetail(result, userContext, methodName) {
    if ($("#content").length > 0) {
        $("#content").html(result);
        resetImageToResolution();
        loadLightBox();
        loadTooltip();
    }
}

function OnFailedGetProductDetail(result, userContext, methodName) {
    if ($("#content").length > 0) {
        $("#content").html(result);
    }
}

function loadLightBox() {
    if ($("#productContent").length > 0) {
        $("#productContent a.lightbox").lightBox();
    }
}

function OnSucceededGetSearchedCollection(result, userContext, methodName) {
    $('a[@rel=collectionselect]').removeClass();
    $("#" + result).addClass("selected");
    if ($("#" + result).parent().parent().attr("rel") == "subcollection") {
        $("#" + $("#" + result).parent().parent().attr("id")).slideDown("slow");
    }
    else {
        $("ul[@rel$='subcollection']").hide();
    }
}

function OnFailedGetSearchedCollection(result, userContext, methodName) {
    $('a[@rel=collectionselect]').removeClass();
    $("ul[@rel$='subcollection']").hide();
    //alert('error');
}

//voor didier
function getRandomProduct() {
    $("#homepage").html("");
    $("#moviepage").html("");
    realtime.GetArtikelDetail(0, $("#ctl00_hdnLanguageCode").val(), OnSucceededGetProductDetail, OnFailedGetProductDetail);
}

//dealer loading ding + validation
function checkDealers() {
    if (checkTextBoxes('dealers')) {
        $("#dealers").html("<div id='loadDealers'><img src='" + SiteUrl + "images/ajax-loader.gif' alt='loading' /><br /><br />" + $("#ctl00_cphBody_notificationMessage2").val() + "</div>");
        return true;
    }

    return false;
}

function resetImageToResolution() {
    var height = window.screen.height;
    var imageHeight = 350; //height - 154 - 370;
    
    if ($("#productFoto").length > 0) {
        if ($("#productFoto").length > 0) {
            $("#productFoto").load(function() {
                var img = new Image();
                img = $("#productFoto");
                
                var width, height;
                width = img.width();
                height = img.height();
               
                if (height > width) {
                    // Portrait foto.
                    if (height > imageHeight) {
                        $("#productFoto").removeAttr("style");
                        $("#productFoto").attr("style", "height: " + imageHeight + "px;");                        
                    }
                    else {
                        $("#productFoto").removeAttr("style");
                    }
                }    
                else {
                    if (width > height) {
                        // Landscape foto.
                        if (width > 445) {
                            $("#productFoto").removeAttr("style");
                            $("#productFoto").attr("style", "width: 445px;");                        
                        }
                        else {
                            // Foto's die net geen vierkant zijn springen anders uit de boot.
                            if (height > imageHeight) {
                                $("#productFoto").removeAttr("style");
                                $("#productFoto").attr("style", "height: " + imageHeight + "px;");                        
                            }
                            else {
                                $("#productFoto").removeAttr("style");  
                            }
                        }
                    }
                    else {
                        // Vierkante foto.
                        if (width == height) {
                            if (height > imageHeight) {
                                $("#productFoto").removeAttr("style");
                                $("#productFoto").attr("style", "height: " + imageHeight + "px;");                        
                            }
                            else {
                                $("#productFoto").removeAttr("style");
                            }
                        }                    
                    }
                }  
                
                $("#loadingProductImage").hide();    
            });
        }
    }
}

function loadTooltip() {
    if ($(".foto").length > 0) {
        $('.foto').tooltip({ 
            track: true, 
            delay: 0, 
            showURL: false, 
            fade: 250 
        });
    }

    if ($(".downloadpdfTip").length > 0) {
        $('.downloadpdfTip').tooltip({
            track: true,
            delay: 0,
            showURL: false,
            fade: 250
        });
    }  
}

/* Opvragen SiteUrl */
function getSiteUrl() {
    realtime.GetSiteUrl(onSucceededSiteUrl, OnFailedSiteUrl);
}

function onSucceededSiteUrl(result, userContext, methodName) {
    // SiteUrl haalt asynchroon de SiteUrl uit de web.config.
    // Omdat de slideshows pas mag gestart worden als SiteUrl goed
    // opgehaald is, starten we hier de slideshows.
    SiteUrl = result;

    WaitForSiteUrl();
}

function OnFailedSiteUrl(error, userContext, methodName) {
    //alert("Er was een probleem bij het ophalen van de siteurl. Probeer opnieuw aub");
    WaitForSiteUrl();
}

function WaitForSiteUrl() {
    getPathfotosOnLoad();
    getPathfotosOnClick();
    loadLightBox();

    $("#previous img").hover(function() {
        $(this).attr("src", SiteUrl + "images/leftCol-over.png");
        setNavigationPngTransparent(this);
    }, function() {
        $(this).attr("src", SiteUrl + "images/leftCol.png");
        setNavigationPngTransparent(this);
    });

    $("#next img").hover(function() {
        $(this).attr("src", SiteUrl + "images/rightCol-over.png");
        setNavigationPngTransparent(this);
    }, function() {
        $(this).attr("src", SiteUrl + "images/rightCol.png");
        setNavigationPngTransparent(this);
    });

    if ($("#noFlash").length > 0) {
        //vertalingen
        var strIntroOverslaan = "intro overslaan";
        var strSpeelduur = "(speelduur: 30 sec)";
        
        if ($("#ctl00_cphBody_hdnIntroOverslaan").length > 0) {
            strIntroOverslaan = $("#ctl00_cphBody_hdnIntroOverslaan").attr("value");
        }
        if($("#ctl00_cphBody_hdnSpeelduur").length > 0) {
            strSpeelduur = $("#ctl00_cphBody_hdnSpeelduur").attr("value");
        }
    
        //flash toevoegen
        var flashvars = {};
        flashvars.url = SiteUrl;
        var params = {};
        params.allowfullscreen = "true";
        var attributes = {};
        swfobject.embedSWF("" + SiteUrl + "intro.swf", "noFlash", "640", "400", "9.0.0", false, flashvars, params, attributes);

        $("#noFlash").parent().append("<p><a style=\"color: #565656;\" href=\"javascript:void(0);\" onclick=\"getRandomProduct();\">" + strIntroOverslaan + "</a> " + strSpeelduur + "</p>");
    }

    if ($("#noFlashKlassiek").length > 0) {
        var flashvars = {};
        flashvars.url = SiteUrl;
        var params = {};
        params.allowfullscreen = "true";
        var attributes = {};
        swfobject.embedSWF("" + SiteUrl + "klassiek.swf", "noFlashKlassiek", "880", "535", "9.0.0", false, flashvars, params, attributes);
    }

    if ($("#noFlashMakingOf").length > 0) {
        var flashvars = {};
        flashvars.url = SiteUrl;
        var params = {};
        params.allowfullscreen = "true";
        var attributes = {};
        swfobject.embedSWF("" + SiteUrl + "makingof.swf", "noFlashMakingOf", "753", "608", "9.0.0", false, flashvars, params, attributes);
    }
}

/* nieuwe zoekfunctie */
//zoekfct met opties
function searchArtikelenOptions() {
    var boolStijl_Modern = false
    var boolStijl_Style = false
    var boolVorm_Vierkant = false
    var boolVorm_Rechthoek = false
    var boolVorm_Rond = false
    var boolVorm_Willekeurig = false
    var boolVorm_Meubel = false
    var intAfmetingen = 0
    var strArtnummer_Categorie = ''
    var strArtnummer_Product = ''

    if ($("#ctl00_ctlSearch_chkStijl_Modern").length > 0) {
        boolStijl_Modern = $("#ctl00_ctlSearch_chkStijl_Modern").attr("checked");
    }

    if ($("#ctl00_ctlSearch_chkStijl_Style").length > 0) {
        boolStijl_Style = $("#ctl00_ctlSearch_chkStijl_Style").attr("checked");
    }

    if ($("#ctl00_ctlSearch_chkVorm_Vierkant").length > 0) {
        boolVorm_Vierkant = $("#ctl00_ctlSearch_chkVorm_Vierkant").attr("checked");
    }

    if ($("#ctl00_ctlSearch_chkVorm_Rechthoek").length > 0) {
        boolVorm_Rechthoek = $("#ctl00_ctlSearch_chkVorm_Rechthoek").attr("checked");
    }

    if ($("#ctl00_ctlSearch_chkVorm_Rond").length > 0) {
        boolVorm_Rond = $("#ctl00_ctlSearch_chkVorm_Rond").attr("checked");
    }

    if ($("#ctl00_ctlSearch_chkVorm_Willekeurig").length > 0) {
        boolVorm_Willekeurig = $("#ctl00_ctlSearch_chkVorm_Willekeurig").attr("checked");
    }

    if ($("#ctl00_ctlSearch_chkVorm_Meubel").length > 0) {
        boolVorm_Meubel = $("#ctl00_ctlSearch_chkVorm_Meubel").attr("checked");
    }

    if ($("#ctl00_ctlSearch_rblAfmetingen").length > 0) {
        if ($("#ctl00_ctlSearch_rblAfmetingen_0").attr("checked")) {
            intAfmetingen = 0
        }
        if ($("#ctl00_ctlSearch_rblAfmetingen_1").attr("checked")) {
            intAfmetingen = 1
        }
        if ($("#ctl00_ctlSearch_rblAfmetingen_2").attr("checked")) {
            intAfmetingen = 2
        }
        if ($("#ctl00_ctlSearch_rblAfmetingen_3").attr("checked")) {
            intAfmetingen = 3
        }
        if ($("#ctl00_ctlSearch_rblAfmetingen_4").attr("checked")) {
            intAfmetingen = 4
        }
    }

    realtime.GetPathfotosSearchOptions(boolStijl_Modern, boolStijl_Style, boolVorm_Vierkant, boolVorm_Rechthoek, boolVorm_Rond, boolVorm_Willekeurig, boolVorm_Meubel, intAfmetingen, $("#ctl00_hdnPart").attr("value"), $("#ctl00_hdnLanguageCode").attr("value"), OnSucceededGetPathfotosOptions, OnFailedGetPathfotosOptions);
    realtime.GetSearchedCollection($("#ctl00_ctlCollecties_txtCategorie").attr("value"), OnSucceededGetSearchedCollection, OnFailedGetSearchedCollection);
}

function OnSucceededGetPathfotosOptions(result, userContext, methodName) {
    $("#pathfotoslist").html(result);
    enableProductView();
    startSlideshow();
    currentPosition = 0;
    $('#products').trigger('start'); //we forceren nog eens het starten, want een gestopte scroller zal na 1 item gescrolled te hebben terug stoppen wanneer we de scroller terug enablen na het inladen van een nieuwe lijst
    $('#products').trigger('goto', [0]); //fix voor zoekresultaten

    checkIfStopNeeded();
}

function OnFailedGetPathfotosOptions(error, userContext, methodName) {
    $("#pathfotoslist").html("Er is een fout opgetreden, probeer later opnieuw aub...");
}

//zoekfct met zoektermen
function searchArtikelenTerms() {
    var strArtnummer_Categorie = ''
    var strArtnummer_Product = ''

    if ($("#ctl00_ctlSearch_txtCategorie").length > 0) {
        strArtnummer_Categorie = $("#ctl00_ctlSearch_txtCategorie").val();
    }

    if ($("#ctl00_ctlSearch_txtProduct").length > 0) {
        strArtnummer_Product = $("#ctl00_ctlSearch_txtProduct").val();
    }

    realtime.GetPathfotosSearch(strArtnummer_Categorie, strArtnummer_Product, $("#ctl00_hdnPart").attr("value"), $("#ctl00_hdnLanguageCode").attr("value"), OnSucceededGetPathfotosTerms, OnFailedGetPathfotosTerms);
    realtime.GetSearchedCollection($("#ctl00_ctlCollecties_txtCategorie").attr("value"), OnSucceededGetSearchedCollection, OnFailedGetSearchedCollection);
}

function OnSucceededGetPathfotosTerms(result, userContext, methodName) {
    $("#pathfotoslist").html(result);
    enableProductView();
    startSlideshow();
    currentPosition = 0;
    $('#products').trigger('start'); //we forceren nog eens het starten, want een gestopte scroller zal na 1 item gescrolled te hebben terug stoppen wanneer we de scroller terug enablen na het inladen van een nieuwe lijst
    $('#products').trigger('goto', [0]); //fix voor zoekresultaten

    checkIfStopNeeded();
}

function OnFailedGetPathfotosTerms(error, userContext, methodName) {
    $("#pathfotoslist").html("Er is een fout opgetreden, probeer later opnieuw aub...");
}