﻿/*****************************************************

	Javascript code for vnu websitegenerator

	by		: leonard eshuis
	for		: Evident interactive
	version	: beta
	date	: 2 feb 2009

*****************************************************/

var $jq = jQuery.noConflict();

$jq(document).ready(function() {

    /**************** generic functions *****************/

    function fullStop(e) {
        stopBubbling(e);
        stopDef(e);
    }


    function stopBubbling(e) {
        if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
    }

    function stopDef(e) {
        if (e && e.preventDefault) e.preventDefault();
        else if (window.event && window.event.returnValue)
            window.eventReturnValue = false;
    }

    function isNumeric(text) {
        if (text.length == 0) {
            return false;
        }
        for (i = 0; i < text.length; i++) {
            if ("0123456789.,".indexOf(text[i]) == -1) {
                return false;
            }
        }
        return true;
    }

    /**************** equal column heights **************/

    function equalizeHeight() {

        var maxHeight = 0;
        var columns = $jq("#main>div");

        // clear
        /* $jq("#main>div").each(function(index, item){
        item.css("height", null);
        }); */
        // set
        for (var i = 0; i < columns.length; i++) {
            var colHeight = $jq(columns[i]).height();
            if (colHeight > maxHeight) {
                maxHeight = colHeight;
            }
        }
        $jq("#main>div").css("height", maxHeight);
    }
    //equalizeHeight();

    /**************** subnavigation *********************/

    $jq("#subnav ul ul ul ").hide();
    $jq("#subnav ul ul li.activeparent ul ").show();

    $subSubMenu = $jq("#subnav ul ul ul");

    $jq("#subnav .active:first").next().show();

    $jq("#subnav>ul>li>ul>li").bind("mouseover", function(e) {
        stopBubbling(e);
        var $thisSubSubMenu = $jq(this).find(">ul");
        $subSubMenu.not($thisSubSubMenu).hide();
        $thisSubSubMenu.show();
    })

    /**************** main navigation *******************/

    var navTimer = 0;
    var lastSubMenu = null;

    $jq("#navigation ul ul").hide();

    $jq("#navigation>div>ul>li").bind("mouseover", function() {
        var thisSubmenu = $jq(this).find("ul");
        $jq("#navigation ul ul").not(thisSubmenu).hide();
        $jq(this).find("ul").show();
        $jq(this).find("ul").addClass("open");
    })

    $jq("#navigation ul ul").bind("mouseout", function() {
        navTimer = setTimeout('$jq("#navigation .open").hide()', 250)
    })

    $jq("#navigation ul ul").bind("mouseover", function() {
        clearTimeout(navTimer)
    })


    /* */


    $jq(".lastestnews .body").slideUp();

    var trigger = null;

    $jq(".lastestnews li").hover(
			function() {
			    $jq(this).find(".body").slideDown();
			    $jq(".lastestnews li").not($jq(this)).find(".body").slideUp();
			},
			function() {
			}
		);

    /**************** photo galleries *******************/

    pgc = {
        add: function(ident, givenTimer, transitionTime) {
            pgc[ident] = $jq("#" + ident);
            pgc[ident].waitTime = givenTimer;
            pgc[ident].transTime = transitionTime;
            pgc[ident].timer = null;
        }
    }


    $jq(".pictures li").hide();

    $jq(".photogallery").each(function(n) {
        $jq(this).attr("id", "pg_" + n);

        var givenTimer = $jq(".inputInterval", $jq(this)).val();

        pgc.add("pg_" + n, givenTimer, givenTimer / 4);

        $jq(".pictures .previousPhoto", $jq(this)).bind("click", function() {
            slideShow('Prev', null, "pg_" + n)
        });

        $jq(".pictures .nextPhoto", $jq(this)).bind("click", function() {
            slideShow('Next', null, "pg_" + n)
        });

        $jq(".pictures li:first", $jq(this))
            .addClass("activePicture")
            .fadeIn();

        $jq(".thumbnails li:first", $jq(this))
            .addClass("activeThumb")
            .addClass("active");

        $jq(".thumbnails .actions", $jq(this)).toggle(
		function() {
		    $jq(this).find("img:first").slideUp().next().slideDown();
		    clearInterval(pgc["pg_" + n].timer);
		},
		function() {
		    $jq(this).find("img:first").slideDown().next().slideUp();
		    slideShow('Next', null, "pg_" + n);
		    pgc["pg_" + n].timer = setInterval(function() { slideShow('Next', null, "pg_" + n) }, pgc["pg_" + n].waitTime);
		}
    	);

        pgc["pg_" + n].timer = setInterval(function() { slideShow('Next', null, "pg_" + n) }, pgc["pg_" + n].waitTime);

        $jq(".thumbnails .cutter a", $jq(this))
		    .bind("click", function(e) {
		        fullStop(e);
		        slideShowRequest($jq(".cutter a", "#pg_" + n).index(this), "pg_" + n)
		        return false;
		    });

        var totalWidth = $jq(".thumbnails", $jq(this)).width();
        var cutterWidth = $jq(".cutter ol", $jq(this)).width();
        var restWidth = totalWidth - cutterWidth - 70;

        if (restWidth >= 0) {
            $jq(".thumbnails .previousPhoto", $jq(this)).hide();
            $jq(".thumbnails .nextPhoto", $jq(this)).hide();
            $jq(".cutter", $jq(this)).css("margin-left", "75px");
        }

        $jq(".thumbnails .nextPhoto", $jq(this)).bind("click", function(e) {
            fullStop(e)
            var temp = $jq(".thumbnails .cutter ol:first", "#pg_" + n).position().left;
            var limit = $jq(".thumbnails .cutter li", "#pg_" + n).length + 2;

            temp = temp - 64;
            limit = ((limit * 64) - 280) * -1

            if (temp > limit) {
                $jq(".thumbnails .cutter ol:first", "#pg_" + n).stop().animate({ "left": temp });
            }
        });

        $jq(".thumbnails .previousPhoto", $jq(this)).bind("click", function(e) {
            fullStop(e)

            var temp = $jq(".thumbnails .cutter ol:first", "#pg_" + n).position().left;
            temp = temp + 64;
            if (temp < 0) {
                $jq(".thumbnails .cutter ol:first", "#pg_" + n).stop().animate({ "left": temp });
            } else {
                $jq(".thumbnails .cutter ol:first", "#pg_" + n).stop().animate({ "left": 0 });
            }

        });
    })


    function slideShow(direction, requested, target) {

        var $target = $jq("#" + target);

        var $current = $jq(".activePicture", $target)
        var $nextInLine;

        var $currentThumb = $jq(".activeThumb", $target)
        var $nextInLineThumb;

        data = pgc[target];

        if (direction == "Next") {
            if ($current.next().length > 0) {
                $nextInLine = $current.next();
                $nextInLineThumb = $currentThumb.next()
            } else {
                $nextInLine = $current.siblings(":first");
                $nextInLineThumb = $currentThumb.siblings(":first");
            }
        }

        if (direction == "Prev") {
            if ($current.prev().length > 0) {
                $nextInLine = $current.prev();
                $nextInLineThumb = $currentThumb.prev()
            } else {
                $nextInLine = $current.siblings(":last");
                $nextInLineThumb = $currentThumb.siblings(":last");
            }
        }

        if (direction == "Request") {
            $nextInLine = $jq(".pictures li:nth-child(" + requested + ")", $target);
            $nextInLineThumb = $jq(".thumbnails li:nth-child(" + requested + ")", $target)
        }

        $current
            .fadeOut(data.transTime)
            .removeClass("activePicture");

        $nextInLine
            .fadeIn(data.transTime)
            .addClass("activePicture");

        $currentThumb
            .removeClass("activeThumb")
            .removeClass("active");

        $nextInLineThumb
            .addClass("activeThumb")
            .addClass("active");
    }

    function slideShowRequest(request, ident) {
        slideShow("Request", request + 1, ident);
    }


    /*    $jq(".photogallery .pictures img").bind("click", function(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
    targ = targ.parentNode;

        console.log($jq(targ).parent())

        if (targ.tagName == "IMG") {

            var i = new Image();

            if ($jq(targ).parent().tagName == "A") {
    return true;
    } else {
    i.src = $jq(targ).attr("src");
    }

            $jq.blockUI({
    message: i,
    css: { top: '10px', left: '10px' }
    });
    $jq('.blockOverlay').attr('title', 'Click to unblock').click($jq.unblockUI);
    $jq('.blockMsg').attr('title', 'Click to unblock').click($jq.unblockUI);
    return false;
    }
    });
    */
    function fitToSize() {

    }

    function slideNews() {
        var slideTime = 4000; /* time the slide pauzes */
        var TransTime = 1000; /* time the slide transitions to the next */
        var graceTime = 1000;  /* time to need to be of the menu to re-engage the automatic slider */

        var slideHeight = 150; /* the height of news article */
        var buttonHeight = 24; /* the height of the button controling the slide */


        var slideTimer = null; /* timer for slide function */
        var graceTimer = null; /* timer for "grace periode" */

        var nArticles = $jq(".article").length;
        var counter = 0;
        var correction = 0;

        var timerOn = false;    /* is the slide timer engaged */

        /* start the pointer - indicator before buttons - */
        $jq("#pointer").animate({ marginTop: ((counter + 1) * buttonHeight) }, slideTime)

        function next() {
            counter += 1;

            $jq("#slider").animate({ marginTop: "-=" + slideHeight }, TransTime, function (){
				$jq(".article:first").clone().appendTo("#slider");
				$jq(".article:first").remove();
				$jq("#slider").css({ marginTop: 0});
				correction += 1;
            });

            if (counter === nArticles) {

                /* resets the slider */
                counter = 0;
                $jq("#pointer").animate({ marginTop: ((counter + 1) * buttonHeight) }, slideTime)
                
            } else {
            
                /* move pointer to next */
                if (counter < (nArticles - 1)) {
                    $jq("#pointer").animate({ marginTop: ((counter + 1) * buttonHeight) }, slideTime)
                } else {
                    $jq("#pointer").stop().animate({ marginTop: "0px" }, slideTime)
                }
            }
            $jq(".listitemactive").removeClass("listitemactive");
            $jq("#news li:eq(" + counter + ")").addClass("listitemactive");
        }

        /* engage slide timer */
        slideTimer = setInterval(function() { next(); timerOn = true }, slideTime);

        /* button control for flipping through the news*/
        $jq("#news li").mouseover(function(e) {
			var correctedIndex = 0;

            /* disengage slide timer */
            if (timerOn) {
                clearInterval(slideTimer);
                timerOn = false;
            }

            /* disengage grace periode timer */
            clearTimeout(graceTimer);

            /* stop event from moving on to other tags*/
            if (!e) var e = window.event;
            e.cancelBubble = true;
            if (e.stopPropagation) e.stopPropagation();

            /* what button was used */
            var index = $jq("#news li").index(this);

            $jq("#news .listitemactive").removeClass("listitemactive")
            $jq("#news li:eq("+index+")").addClass("listitemactive")

            /* the action , you wanted */
			correctedIndex = index - (correction%5);


			if (correctedIndex<0){
				correctedIndex = nArticles + correctedIndex 
			} 
          
            for(var i = 0;i<correctedIndex;i++){
			    $jq(".article:first").clone().appendTo("#slider");
			    $jq(".article:first").remove();
			    $jq("#slider").css({ marginTop: 0});
			    correction += 1;
            }
            counter = index;
      
//            $jq("#slider").stop().animate({ marginTop: -(correctedIndex * slideHeight) }, 10);
            $jq("#pointer").stop().animate({ marginTop: index * buttonHeight }, 10)
        });

        /* trigger grace periode */
        $jq("#news li").mouseout(function(e) {
            graceTimer = setTimeout(function() {

                clearInterval(slideTimer);

                /* slidetimer trigger */
                slideTimer = setInterval(function() {
                    next();
                    timerOn = true;
                }, slideTime);
            }, graceTime)
        });
    }

    /* trigger slidingNews */
    slideNews();

    /**************** preset & Initialisation ***********/

    jQuery("body").append("<div id=\"fog\"></div>");

    var temp = jQuery(".popupdetail:first").clone()
    jQuery("body").append(temp)
    jQuery(".popupdetail:first").remove();

    jQuery(".popupclose").bind("click",function(e){
        jQuery("#popupProductInfo").fadeOut(200);
        jQuery("#fog").hide();
        jQuery("#fog").css({ "height": 0, "width": 0 });
    });

    var temp = setTimeout(function(){
        if(jQuery(".popupdetail:visible").length>0){
         jQuery("#fog").css({ "height": jQuery(document).height(), "width": jQuery(document).width() });
         jQuery("#fog").show();
     }
        
    },250)

    jQuery("#fog").click(function(){
        jQuery(this).hide();
        jQuery(".popupdetail").hide();
    });

    /* leonard eshuis - 8 jan 2010 - */
    
    $jq(".row").each(function(){
        var tallest = 0;
    
        $jq(">div",this).each(function(){
            if($jq(this).height()>tallest){tallest=$jq(this).height()}
        });
        
        $jq(">div>div",this).css({"height":tallest})
    });


});



/* */


