﻿function OpenSubmenu(object) {
    var submenu = $(object).siblings("ul:eq(0)");

    if (submenu.is(":visible")) {
        submenu.slideUp(600);
        $(object).attr("src", "/Files/Templates/Designs/Fromm/images/dropdown_button.png");
    }
    else {
        submenu.slideDown(600);
        $(object).attr("src", "/Files/Templates/Designs/Fromm/images/dropdown_active.png");
    }
}

function replaceUrlInTweet() {
    var twitterMessage = $.trim($("p.social").text());
    var urlRegex = new RegExp("(https?://.*?)(?=[.,:;)]*($|\s))");

    var arrMatches = twitterMessage.match(urlRegex)[0].split(' ');
    var count = arrMatches.length;

    for (i = 0; i < count; i++) {
        var toReplace = arrMatches[i];
        twitterMessage = twitterMessage.replace(toReplace, "<a href='" + toReplace + "' target='_blank'>" + toReplace + "</a>")
    }

    $("p.social").html(twitterMessage)
}

function showSearchBar() {
    //Check if search bar is shown, if it is hide it, otherwise show it.
    var isVisible = $("#search").is(":visible");

    if (isVisible) {
        hideSearchBar();
    }
    else {
        //Show the overlay
        $("#overlay").css("opacity","0.7").fadeIn(600);
        //$("#overlay").fadeTo(600,0.7);

        //Show the hidden div
        $("#search").fadeIn(600);
        //$("#search").fadeTo(600,0.7);

        //Set focus on search field
        $("#searchInput").focus();
    }
}

function hideSearchBar() {
    //Hide the div
    $("#search").fadeOut(600);

    //hide the overlay
    $("#overlay").fadeOut(600);
}

function rollOver(btn) {
    $(btn).css('background-color', '#fa1b38');
    //Cufon.refresh();
}

function rollOut(btn) {
    $(btn).css('background-color', '#FFF');
    //Cufon.refresh();
}

function textOver(text) {
    $(text).css('color', '#FFF');
    Cufon.refresh();
}

function textOut(text) {
    $(text).css('color', '#fa1b38');
    Cufon.refresh();
}

function rollop(btn) {
    $(btn).css('border-color', '#FF0000');
}

function rolluit(btn) {

    $(btn).css('border-color', '#FFFFFF');
}

function showArrow(arrow) {
    $(arrow).fadeTo(10, 100);
}

function hideArrow(arrow) {
    $(arrow).fadeTo(10, 0);
}

function hideAllArrows() {
    hideArrow('#arrow_1');
    hideArrow('#arrow_2');
    hideArrow('#arrow_3');
}
function changeImg(img) {
    $('body').css('backgroundImage', 'url(Files/Templates/Designs/Fromm/' + img + ')');
}

$(document).ready(function () {
    if ($(".productDetailText").length > 0) {
        shortenText();
    }
    /* hideLinks();
    $("#mainMenu >li a").click(function () {
    dropDown(this);
    });
    $("#mainMenu").focusout(function () {        
    setTimeout(closeMenu, 1000);
    });*/
    checkMenu();
    $(".lees_verder").hover(
        function () {
            $(this).css('background-color', '#fff');
            $(this).find('a').css('color', '#fa1b38');

            $(this).parent().find('p').css('color', '#fff');

            $(this).parent().find("h1 > a").css('color', '#fff');

            $(this).parent().css('background-color', '#fa1b38');

            Cufon.replace('#productBlock h1', {});
        },
        function () {
            $(this).css('background-color', '#fa1b38');
            $(this).find('a').css('color', '#fff');

            $(this).parent().find('p').css('color', '#464A55');

            $(this).parent().find("h1 > a").css('color', '#fa1b38');
            $(this).parent().css('background-color', '#fff');

            Cufon.replace('#productBlock h1', {});
        }
    );

    $('#name').focus(function () {
        var nameValue = $('#name').val();
        if (nameValue == 'Uw naam') {
            $('#name').val('');
        }
    });

    $('#email').focus(function () {
        var emailValue = $('#email').val();
        if (emailValue == 'Uw e-mail') {
            $('#email').val('');
        }
    });

    $('#name').blur(function () {
        var nameValue = $('#name').val();

        if (nameValue == '') {
            $('#name').val('Uw naam');
        }
    });

    $('#email').blur(function () {
        var emailValue = $('#email').val();

        if (emailValue == '') {
            $('#email').val('Uw e-mail');
        }
    });

    hideAllArrows();

    $('#image_btn1').mouseover(function () {
        rollop('#image_btn1');
        showArrow('#arrow_1');

    });

    $('#image_btn2').mouseover(function () {
        rollop('#image_btn2');
        showArrow('#arrow_2');
    });

    $('#image_btn3').mouseover(function () {
        rollop('#image_btn3');
        showArrow('#arrow_3');
    });

    $('#image_btn1').mouseout(function () {
        rolluit('#image_btn1');
        hideArrow('#arrow_1');
    });

    $('#image_btn2').mouseout(function () {
        rolluit('#image_btn2');
        hideArrow('#arrow_2');
    });

    $('#image_btn3').mouseout(function () {
        rolluit('#image_btn3');
        hideArrow('#arrow_3');
    });

    $('#image_btn1').click(function () {
        changeImg("images/img_background.png");

    });
    $('#image_btn2').click(function () {
        changeImg("images/img_background2.png");

    });
    $('#image_btn3').click(function () {
        changeImg("images/img_background3.png");

    });

    /*FUNCTIONALITY TO CREATE OVERLAY ON MENU HOVER*/
    //    $("#mainMenu > li").mouseover(function () {

    //        if ($(this).children().size() > 1) {
    //            //console.log($("#overlay").is(":visible"))
    //            $("#overlay").fadeIn(600);
    //        }
    //    })

    //    $("#mainMenu > li").mouseleave(function () {

    //        if (/*($("#overlay").is(":visible")) &&*/($("#mainMenu > li > ul").not(":visible"))) {
    //            $("#overlay").fadeOut(600);
    //            return false;
    //        }
    //    })

    //Add an additional class to the second and third level menu
    $("#mainMenu > li > ul > li > ul").attr("class", "secondLevel");
    $("#mainMenu > li > ul > li > ul > li > ul").attr("class", "thirdLevel");

    $(function () {
        $('ul.secondLevel li').each(function (index, item) {
            $(this).find('a').toggleClass('aBold', ($(this).find('ul.thirdLevel').length > 0));
        });
    });

    $(function () {
        //Remove the last border-image on the right side of the homepage products
        var length = $("#Products div").children("img.shadow").length;
        var itemToRemove = $("#Products div img.shadow")[length - 1];
        $(itemToRemove).remove();
    });

    //Watermarks contact forms.
    $("#ctl01_tbName").watermark("Naam*");
    $("#ctl01_tbComments").watermark("Opmerkingen*");
    $("#ctl01_tbCompany").watermark("Bedrijfsnaam*");
    $("#ctl01_tbEmail").watermark("E-mail*");
    $("#ctl01_tbPhoneNumber").watermark("Telefoonnummer");
    $("#ctl01_tbCity").watermark("Plaats*");

    //Fix the left sidebar, first occuring navigation item
    $("#nav_left > .nav_left").first().css("margin-top", "50px");
});

$(document).keyup(function (e) {
    if (e.keyCode == 27) { hideSearchBar(); }   // esc
});
//DROPDOWN PRODUCTEN
/*var active = false;
function dropDown(link) {
    var menu = $(link).parent().find('ul:first');    
    if (active == false) {        
        menu.show();
        active = true;
    }
   else {
       
       closeMenu();
       $(link).attr('onClick', '');
       window.location = $(link).attr('href');
       active = false;
    }

    return false;
}
function hideLinks() {
    $('#mainMenu > li').children("ul").each(function () {
        $(this).parent().find("a:first").attr('onClick', 'return false;')
    })
}
function closeMenu() {
    $("#mainMenu > li > ul:visible").hide();
    active = false;
}*/
function shortenText() {
    $('.productDetailText').each(function () {
        var textElement = $(this); //Element(s) which you want to limit the size off 
        var limit = 150; //The number of characters to show
        var textContent = textElement.html(); //Getting the text from the elements
        jQuery.trim(textContent);
        var textContentTemp = textContent.substr(0, limit); //Get the part of the text you want to show on your page
        textElement.html(textContentTemp + "...");
    });
}
function checkMenu() {
    $('#mainMenu > li > ul > li > ul > li').children('ul').each(function () {
        $(this).parent().addClass('threeLevels');
    })
}
