$(document).ready(function() {

    $("img[title]").tooltip();
    
    $("a[rel=example1]").fancybox({
        'titleShow': false
    });
    
    $("a[rel=web2print]").fancybox({
        'width': 1040,
        'height': 800,
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'iframe',
        'showNavArrows': false
    });
    


    $(".preloader").fancybox({
        'titleShow': false
    });

    $(".fb_popup").fancybox({
        'titleShow': false,
        'transitionIn': 'none',
        'transitionOut': 'none'
    });

    
    $("a#example2").fancybox({
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic'
    });

    $("a#example3").fancybox({
        'titleShow': false,
        'transitionIn': 'none',
        'transitionOut': 'none'
    });

    $("a#example4").fancybox();

    $("a#example5").fancybox({
        'titlePosition': 'inside'
    });

    $("a#example6").fancybox({
        'titlePosition': 'over'
    });

    $("a[rel=example_group]").fancybox({
        enableEscapeButton: true,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'titlePosition': 'over',
        'showNavArrows': true,
        'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
            return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
        }
    });

    $("a[rel=fileapproval]").fancybox({
        enableEscapeButton: true,
        'titleShow': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'showNavArrows': false
    });

    //Examples - various

    $("#various1").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'none',
        'transitionOut': 'none'
    });

    $("#various2").fancybox();

    $("#various3").fancybox({
        'width': '75%',
        'height': '75%',
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'iframe'
    });


    $("#various4").fancybox({
        'padding': 0,
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none'
    });
    
    //Production
    $(".loginFrame").fancybox({
        enableEscapeButton: true,
        'titleShow': true,
        'width': 510,
        'height': 300,
        'overlayOpacity': .6,
        'overlayColor': '#383838',
        'type': 'iframe'
    });

    $(".sf-with-ul").hover(function() {
        $(".level2").css("visibility", "visible");
    });


    //Product Selection
    $(".barbtn1").click(function() {
        $(".barcontent2").slideUp("slow");
        $(".barcontent3").slideUp("slow");
        $(".barcontent4").slideUp("slow");
        $(".barcontent5").slideUp("slow");
        $(".barcontent1").slideDown("slow");
    });
    $(".barbtn2").click(function() {
        $(".barcontent1").slideUp("slow");
        $(".barcontent3").slideUp("slow");
        $(".barcontent4").slideUp("slow");
        $(".barcontent5").slideUp("slow");
        $(".barcontent2").slideDown("slow");
    });
    $(".barbtn3").click(function() {
        $(".barcontent1").slideUp("slow");
        $(".barcontent2").slideUp("slow");
        $(".barcontent4").slideUp("slow");
        $(".barcontent5").slideUp("slow");
        $(".barcontent3").slideDown("slow");
    });
    $(".barbtn4").click(function() {
        $(".barcontent1").slideUp("slow");
        $(".barcontent2").slideUp("slow");
        $(".barcontent3").slideUp("slow");
        $(".barcontent5").slideUp("slow");
        $(".barcontent4").slideDown("slow");
    });
    $(".barbtn5").click(function() {
        $(".barcontent1").slideUp("slow");
        $(".barcontent2").slideUp("slow");
        $(".barcontent3").slideUp("slow");
        $(".barcontent4").slideUp("slow");
        $(".barcontent5").slideDown("slow");
    });
    $(".prijs1").click(function() {
        $(".meerprijs2").fadeOut("slow");
        $(".meerprijs1").fadeIn("slow");
    });
    $(".prijs2").click(function() {
        $(".meerprijs1").fadeOut("slow");
        $(".meerprijs2").fadeIn(".meerprijs2 active");
    });		    
    
    $('.barbtn2').click(function() {
    $('.barcontent1').slideUp('normal', function() {});
    $('.barcontent3').slideUp('normal', function() {});
    $('.barcontent4').slideUp('normal', function() {});
    $('.barcontent5').slideUp('normal', function() {});
    $('.barcontent2').slideDown('slow', function() { });
    });			
    $('.barbtn3').click(function() {
    $('.barcontent1').slideUp('normal', function() {});
    $('.barcontent2').slideUp('normal', function() {});
    $('.barcontent4').slideUp('normal', function() {});
    $('.barcontent5').slideUp('normal', function() {});
    $('.barcontent3').slideDown('slow', function() { });
    });
    $('.barbtn4').click(function() {
    $('.barcontent1').slideUp('normal', function() { });
    $('.barcontent2').slideUp('normal', function() { });
    $('.barcontent3').slideUp('normal', function() { });
    $('.barcontent5').slideUp('normal', function() {});
    $('.barcontent4').slideDown('slow', function() { });
    });
    $('.barbtn5').click(function() {
    $('.barcontent1').slideUp('normal', function() { });
    $('.barcontent2').slideUp('normal', function() { });
    $('.barcontent3').slideUp('normal', function() { });
    $('.barcontent4').slideUp('normal', function() {});
    $('.barcontent5').slideDown('slow', function() { });
    });
    
    $('.ShowIssuers').click(function() {
        $('.banken').css("visibility", "visible");
        $('.banken').show("normal");
        $('.incasso').hide("normal");
    });

    $('.ShowBankData').click(function() {
        $('.banken').hide("normal");
        $('.incasso').css("visibility", "visible");
        $('.incasso').show("normal");
    });

    $('.HideIssuers').click(function() {
        $('.banken').hide("normal");
        $('.incasso').hide("normal");
    });    	
    PositionSmartLabels();
});

function showhide(whichLayer, arg3) {
    var elem, vis;
    if (document.getElementById) // this is the way the standards work    
        elem = document.getElementById(whichLayer);
    else if (document.all) // this is the way old msie versions work      
        elem = document.all[whichLayer];
    else if (document.layers) // this is the way nn4 works    
        elem = document.layers[whichLayer];
    vis = elem.style;  // if the style.display value is blank we try to figure it out here
    if ((vis.visibility == 'hidden' || arg3 == 'show') && arg3 != 'hide') {
        vis.visibility = 'visible';
        vis.height = '';
    } else {
        vis.visibility = 'hidden';
        vis.height = '0px';
    }
}

function slideHelpdesk(id, down) {
    var timeToSlide = 50; // in milliseconds
    var obj = document.getElementById(id);
    if (down == 1) { // if it's allready hidden we slide it down
        obj.style.visibility = "hidden";
        obj.style.display = "block";
        var height = obj.offsetHeight;
        obj.style.height = "0px";
        obj.style.visibility = "visible";
        slideDownHelpdesk(obj, 0, height, Math.ceil(height / timeToSlide));
    } else {
        slideUpHelpdesk(obj, Math.ceil(obj.offsetHeight / timeToSlide), obj.offsetHeight);
    }
}

function slideDownHelpdesk(obj, offset, full, px) {
    if (offset < full) {
        obj.style.height = offset + "px";
        obj.style.visibility = "hidden";
        offset = offset + px;
        setTimeout((function() { slideDownHelpdesk(obj, offset, full, px); }), 1);
    } else {
        obj.style.height = full + "px"; //If the data inside is updated on runtime you can use auto instead...
        obj.style.visiblilty = "visible";
    }
}

function slideUpHelpdesk(obj, px, full) {
    if ((obj.offsetHeight - px) > 0) {
        obj.style.height = obj.offsetHeight - px + "px";
        obj.style.visibility = "hidden";
        setTimeout((function() { slideUpHelpdesk(obj, px, full); }), 1);
    } else {
        obj.style.height = full + "px"; // we reset the height if we were to slide it back down
        obj.style.display = 'none';
    }
}

function Row1ClickCategoryVar(sender, eventArgs) 
{
    Button5ClickImitator();
}
function Row1ClickProductVar(sender, eventArgs) 
{
    Button3ClickImitator(); 
}
function Row2Click(sender, eventArgs) 
{
    Button3ClickImitator();
}
function Row3ClickCategoryVar(sender, eventArgs) 
{
    Button3ClickImitator();
}
function Row3ClickProductVar(sender, eventArgs) 
{
    Button1ClickImitator(); 
}
function Button1ClickImitator()
{
    $('.barcontent2').slideUp('normal', function() {});
    $('.barcontent3').slideUp('normal', function() {});
    $('.barcontent4').slideUp('normal', function() {});
    $('.barcontent5').slideUp('normal', function() {});
    $('.barcontent1').slideDown('slow', function() { });
    $('.bar1').focus();
}
function Button2ClickImitator()
{
    $('.barcontent1').slideUp('normal', function() {});
    $('.barcontent3').slideUp('normal', function() {});
    $('.barcontent4').slideUp('normal', function() {});
    $('.barcontent5').slideUp('normal', function() {});
    $('.barcontent2').slideDown('slow', function() { });
    $('.bar2').focus();
}
function Button3ClickImitator()
{
    $('.barcontent1').slideUp('normal', function() {});
    $('.barcontent2').slideUp('normal', function() {});
    $('.barcontent4').slideUp('normal', function() {});
    $('.barcontent5').slideUp('normal', function() {});
    $('.barcontent3').slideDown('slow', function() { });
    $('.bar3').focus();
}
function Button4ClickImitator()
{
    $('.barcontent1').slideUp('normal', function() { });
    $('.barcontent2').slideUp('normal', function() { });
    $('.barcontent3').slideUp('normal', function() { });
    $('.barcontent5').slideUp('normal', function() {});
    $('.barcontent4').slideDown('slow', function() { });
    $('.bar4').focus();
}
function Button5ClickImitator()
{
    $('.barcontent1').slideUp('normal', function() { });
    $('.barcontent2').slideUp('normal', function() { });
    $('.barcontent3').slideUp('normal', function() { });
    $('.barcontent4').slideUp('normal', function() {});
    $('.barcontent5').slideDown('slow', function() { });
    $('.bar5').focus();
}
function ShowBar4()
{
    $('.bar4').slideDown('slow', function(){});
}

function EmptySearchString(id, value) {
    var searchInput = document.getElementById(id)
    if (searchInput.value == value) {
        searchInput.value = '';
    }
}

 var target_Date_Set;
 function countdownToCSE(pnlToCSE_Id, target_Date)
 {
     target_Date_Set = target_Date;
     doCountdownToCSE(pnlToCSE_Id);
 }
function doCountdownToCSE(pnlToCSE_Id)
     {
     var pnlToCSE = document.getElementById(pnlToCSE_Id);
     Today = new Date();
     Todays_Year = Today.getFullYear() - 2000;
     Todays_Month = Today.getMonth();                  
     
     //Convert both today's date and the target date into miliseconds.                           
     Todays_Date = (new Date()).getTime();                                 

     //Find their difference, and convert that into seconds.                  
     Time_Left = Math.round((target_Date_Set - Todays_Date) / 1000);
     
     if(Time_Left < 0)
        Time_Left = 0;
     
     var innerHTML = '';
     
    hours = Math.floor(Time_Left / (60 * 60));
    Time_Left %= (60 * 60);
    minutes = Math.floor(Time_Left / 60);
    Time_Left %= 60;
    seconds = Time_Left;
    var images = pnlToCSE.getElementsByTagName('img');
    var hoursStr = format_number(hours, 3);
    if (hoursStr.substr(0,1) != '0')
        getDesignedDigits(images, 0, hoursStr);
    else {
        images[0].setAttribute('className', 'digit_') || images[0].setAttribute('class', 'digit_');
        getDesignedDigits(images, 1, hoursStr.substr(1));
    }
    getDesignedDigits(images, 4, format_number(minutes, 2));
    getDesignedDigits(images, 7, format_number(seconds, 2));
        
     //Recursive call, keeps the clock ticking.
     if (pnlToCSE_Id)
        setTimeout('doCountdownToCSE("' + pnlToCSE_Id + '");', 1000);
}
function getDesignedDigits(pnl, startCtrl, arg){
    for (ind = 0; ind < arg.length; ind++){
        var digit = arg.substr(ind, 1);
        var img = pnl[startCtrl + ind];
        img.setAttribute('className', 'digit' + digit) || img.setAttribute('class', 'digit' + digit);
    }
}
function format_number(pnumber,decimals){
    var dec = '' + new String(pnumber);
    while(dec.length < decimals) { dec = '0' + dec; }
    return dec;
}
function PositionSmartLabels(){
    for (ind = 0; ind < 5; ind++)
    {
        var smartLbl = $('.smartlabel' + ind);
        if(smartLbl)
            smartLbl.css('padding-top',(smartLbl.height() > 20) ? 6 : 16);
    }
}
function SimulatePostback(gridId, commandName) {
    setTimeout(function () {
        __doPostBack(gridId, commandName);
    }, 100);
}
function SubCategoryRowCreated(sender, eventArgs) {
    var dataItem = eventArgs.get_gridDataItem();
    for (var i = 0; i < dataItem.get_element().cells.length; i++) {
        dataItem.get_element().cells[i].onmouseover = function () {
            var defaultColor = '#ffffff';
            var selectColor = '#f2f2f2';
            if (this.cellIndex > 1 && this.parentNode.cells[3].childNodes[0].childNodes[2].text == "")
                selectColor = '#ffffff';
            this.style.backgroundColor = selectColor;
            switch (this.cellIndex) {
                case 0:
                    this.parentNode.cells[1].style.backgroundColor = selectColor;
                    this.parentNode.cells[2].style.backgroundColor = defaultColor;
                    this.parentNode.cells[3].style.backgroundColor = defaultColor;
                    break;
                case 1:
                    this.parentNode.cells[0].style.backgroundColor = selectColor;
                    this.parentNode.cells[2].style.backgroundColor = defaultColor;
                    this.parentNode.cells[3].style.backgroundColor = defaultColor;
                    break;
                case 2:
                    this.parentNode.cells[0].style.backgroundColor = defaultColor;
                    this.parentNode.cells[1].style.backgroundColor = defaultColor;
                    this.parentNode.cells[3].style.backgroundColor = selectColor;
                    break;
                case 3:
                    this.parentNode.cells[0].style.backgroundColor = defaultColor;
                    this.parentNode.cells[1].style.backgroundColor = defaultColor;
                    this.parentNode.cells[2].style.backgroundColor = selectColor;
                    break;
            }
        };
        dataItem.get_element().cells[i].onmouseout = function () {
            for (ind = 0; ind < this.parentNode.cells.length; ind++)
                this.parentNode.cells[ind].style.backgroundColor = '#ffffff';
        };
    }
}
function AddonRowCreated(sender, eventArgs) {
    var dataItem = eventArgs.get_gridDataItem();
    for (var i = 0; i < dataItem.get_element().cells.length; i++) {
        dataItem.get_element().cells[i].onmouseover = function (e) {
            var checkBox = this.parentNode.cells[0].childNodes[1];
            if (e)
                switch (checkBox.className) {
                case "addon-checkbox":
                    checkBox.className = "addon-checkbox-hover";
                    break;
                case "addon-checkbox-checked":
                    checkBox.className = "addon-checkbox-checked-hover";
                    break;
            }
        };
        dataItem.get_element().cells[i].onmouseout = function (e) {
            var checkBox = this.parentNode.cells[0].childNodes[1];
            if (e)
                switch (checkBox.className) {
                case "addon-checkbox-hover":
                    checkBox.className = "addon-checkbox";
                    break;
                case "addon-checkbox-checked-hover":
                    checkBox.className = "addon-checkbox-checked";
                    break;
            }
        };
    }
}
function ParticallyPricedRowCreated(sender, eventArgs) {
    var dataItem = eventArgs.get_gridDataItem();
    for (var i = 0; i < dataItem.get_element().cells.length; i++) {
        dataItem.get_element().cells[i].onclick = function (e) {
            if (this.className.length != 16) {
                if (this.className != "rightarrowcell")
                    radalert('Klik op de gewenste prijs om door te gaan', 430, 100, '');
            }
            else {
                var lblPriceId = this.childNodes[0].childNodes[1].value;
                switch (this.className) {
                    case "priceselectcell2":
                    case "pricetippedcell2":
                        Button2ClickImitator();
                        break;
                    case "priceselectcell3":
                    case "pricetippedcell3":
                        Button3ClickImitator();
                        break;
                    case "priceselectcell4":
                    case "pricetippedcell4":
                        Button4ClickImitator();
                        break;
                    case "priceselectcell0":
                    case "pricetippedcell0":
                        ShowBar4();
                        Button4ClickImitator();
                        break;
                    default:
                        Button1ClickImitator();
                        break;
                }
                SimulatePostback(lblPriceId, '');
            }
            return false;
        }
    }
}

/* LoginPanel */
function toggleTab(cur_tab) {
    SetActive(cur_tab);

    if (cur_tab == 1) {
        document.getElementById("tab1").style.display = 'block';
        document.getElementById("tab2").style.display = 'none';
        document.getElementById("tab3").style.display = 'none';
        document.getElementById("tab1li").style.color = 'black';
        document.getElementById("tab1li").style.backgroundImage = 'url(images/popbtnbgr.png)';
        document.getElementById("tab2li").style.backgroundImage = 'url(images/popbtnbg.png)';
        document.getElementById("tab2li").style.backgroundPosition = '0px 1px';
        document.getElementById("tab2li").style.color = 'white';
        document.getElementById("tab3li").style.backgroundImage = 'url(images/popbtnbg.png)';
        document.getElementById("tab3li").style.backgroundPosition = '0px 1px';
        document.getElementById("tab3li").style.color = 'white';
    }
    else if (cur_tab == 2) {
        document.getElementById("tab2").style.display = 'block';
        document.getElementById("tab1").style.display = 'none';
        document.getElementById("tab3").style.display = 'none';
        document.getElementById("tab2li").style.color = 'black';
        document.getElementById("tab2li").style.backgroundImage = 'url(images/popbtnbgr.png)';
        document.getElementById("tab1li").style.backgroundImage = 'url(images/popbtnbg.png)';
        document.getElementById("tab1li").style.backgroundPosition = '0px 1px';
        document.getElementById("tab1li").style.color = 'white';
        document.getElementById("tab3li").style.backgroundImage = 'url(images/popbtnbg.png)';
        document.getElementById("tab3li").style.backgroundPosition = '0px 1px';
        document.getElementById("tab3li").style.color = 'white';

    }
    else if (cur_tab == 3) {
        document.getElementById("tab3").style.display = 'block';
        document.getElementById("tab2").style.display = 'none';
        document.getElementById("tab1").style.display = 'none';
        document.getElementById("tab2li").style.color = 'black';
        document.getElementById("tab2li").style.backgroundImage = 'url(images/popbtnbgr.png)';
        document.getElementById("tab1li").style.backgroundImage = 'url(images/popbtnbg.png)';
        document.getElementById("tab1li").style.backgroundPosition = '0px 1px';
        document.getElementById("tab1li").style.color = 'white';
        document.getElementById("tab3li").style.backgroundImage = 'url(images/popbtnbg.png)';
        document.getElementById("tab3li").style.backgroundPosition = '0px 1px';
        document.getElementById("tab3li").style.color = 'white';

    }

}

function SetActive(curTab) {
    var x = document.getElementsByName('imageTab').length;
    for (a = 0; a < x; a++) {
        var imgSrc = document.getElementsByName('imageTab')[a].src;
        imgSrc = imgSrc.replace("-active", "-inactive");
        if (a + 1 == curTab) {
            imgSrc = imgSrc.replace("-inactive", "-active");
        }
        if (document.getElementsByName('imageTab') != null) {
            document.getElementsByName('imageTab')[a].src = imgSrc;
        }
    }
}

function ChangeRowBackgroundColor(ctrl, rgbColor) {
    var cells = ctrl.getElementsByTagName("td");
    if (cells)
        for (ind = 0; ind < cells.length; ind++)
            cells[ind].style.backgroundColor = rgbColor;
}
