/*Mini Slider	!Niet in Document.ready! */ 
function slideSwitch() {
	var $active = $('#mini-slider img.active');

	if ( $active.length == 0 ) $active = $('#mini-slider img:last');

	var $next =  $active.next().length ? $active.next()
		: $('#mini-slider img:first');


		/*start randomize*/
	var $sibs  = $active.siblings();
	var rndNum = Math.floor(Math.random() * $sibs.length );
	var $next  = $( $sibs[ rndNum ] );
		/*end randomize*/

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
		});
}

$(function() {
	setInterval( "slideSwitch()", 7000 );
});



$(document).ready(function() {
	
   
	// TICKER TWITTER, Thanks to Antoni Lupetti @ Woork.blogspot.com
	var first = 0;
	var speed = 2500;
	var pause = 2500;
	
		function removeFirst(){
			first = $('ul#twitter_update_list li:first').html();
			$('ul#twitter_update_list li:first')
			.animate({opacity: 0}, speed)
			.fadeOut('slow', function() {$(this).remove();});
			addLast(first);
		}
		
		function addLast(first){
			last = '<li style="display:none">'+first+'</li>';
			$('ul#twitter_update_list').append(last)
			$('ul#twitter_update_list li:last')
			.animate({opacity: 1}, speed)
			.fadeIn('slow')
		}
	
	interval = setInterval(removeFirst, pause);
	// TICKER TWITTER END
    
    // SEARCH TOGGLE
    $('#searchToggle').click(function () {
        $('#searchBox').toggle();
    });
    
   $('#searchtext[type="text"]').focus(function() {
        if (this.value == this.defaultValue){
        	this.value = '';
    	}
    });
    
/*    
     $('#searchtext[type="text"]').blur(function() {
        if ($.trim(this.value == '')){
        	this.value = (this.defaultValue ? this.defaultValue : '');
    	}
    }); 
*/

    
    // SEARCH TOGGLE END
    
    // FAQ TOGGLES
    $("ul .question").click(function() {
        var index = $(this).parent().index();

        $("#faq").find(".show").removeClass("show");
        $("#faq").find(".block:eq(" + index + ")").addClass("show");
    });

    // FAQ TOGGLES END

    
    // MAIN SUBMENU 
        var sFadeSpeed = "normal";
    
        //On Hover Over
        function megaHoverOver(){
            $(this).find(".submenu").stop().fadeTo(sFadeSpeed, 1).show(); //Find sub and fade it in
            (function($) {
                //Function to calculate total width of all ul's
                jQuery.fn.calcSubWidth = function() {
                    rowWidth = 0;
                    //Calculate row
                    $(this).find(".border-menu ul").each(function() { //for each ul...
                        rowWidth += $(this).width(); //Add each ul's width together
                    });
                };
            })(jQuery); 

            if ( $(this).find(".row").length > 0 ) { //If row exists...

                var biggestRow = 0;	

                $(this).find(".row").each(function() {	//for each row...
                    $(this).calcSubWidth(); //Call function to calculate width of all ul's
                    //Find biggest row
                    if(rowWidth > biggestRow) {
                        biggestRow = rowWidth;
                    }
                });

                $(this).find(".submenu").css({'width' :biggestRow}); //Set width
                $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

            } else { //If row does not exist...

                $(this).calcSubWidth();  //Call function to calculate width of all ul's

               // $(this).find(".submenu").css({'width' : rowWidth}); //Set Width


            }
        }
        //On Hover Out
        function megaHoverOut(){
          $(this).find(".submenu").stop().fadeTo(sFadeSpeed, 0, function() { //Fade to 0 opactiy
              $(this).hide();  //after fading, hide it
          });
        }
    
    
        //Set custom configurations
        var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
        interval: 0, // number = milliseconds for onMouseOver polling interval
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
        timeout: 0, // number = milliseconds delay before onMouseOut
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)
        };

        $("ul#topnav li .submenu").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
        $("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations 
    
    // END MAIN SUBMENU



    //SWITCH BUTTON ANIMATION
        $("#switch-button").hover(function() {
             
             $(this).stop().animate({right: '-5px'}, 'normal');
   
           },function(){
               
             $(this).stop().animate({right: '-150px'}, 'normal');
             
           });

    //END SWITCH BUTTON ANIMATION
    
    // Clickable slider
	$('.wrap #slides img').click(function(event) {
		window.location = 'http://www.zeo.nl/contact';
		return false;
	});

    //  Homepage slider
    if ( $( "#slides" ).length )
    {     
        if ( typeof( $( "#slides" ).slides ) == "function" )
        {
            $( "#slides" ).slides
            ({
                preload: true,
                preloadImage: "/images/base/slider/loading.gif",
                play: 11000,
                pause: 6000,
                hoverPause: false,
                effect: "slide"
            })
        }
    }
    
    if( $( "#optin-label" ).length )
    {        
        $( "#optin-label" ).tooltip({

        // place tooltip on the right edge
        //position: "center right",

        // a little tweaking of the position
        offset: [-5, -15],

        // use the built-in fadeIn/fadeOut effect
        effect: "fade",

        // custom opacity setting
        //opacity: 0.7

});
    }
    
});

