﻿        jQuery(document).ready(function() {
	        //Code goes here
	        //On Hover Over
        function megaHoverOver(){
            jQuery(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
            (function(jQuery) {
                //Function to calculate total width of all ul's
                jQuery.fn.calcSubWidth = function() {
                    rowWidth = 0;
                    //Calculate row
                    jQuery(this).find("ul").each(function() { //for each ul...
                        rowWidth += jQuery(this).width(); //Add each ul's width together
                    });
                };
            })(jQuery); 
 
            if ( jQuery(this).find(".row").length > 0 ) { //If row exists...
 
                var biggestRow = 0;	
 
                jQuery(this).find(".row").each(function() {	//for each row...
                    jQuery(this).calcSubWidth(); //Call function to calculate width of all ul's
                    //Find biggest row
                    if(rowWidth > biggestRow) {
                        biggestRow = rowWidth;
                    }
                });
 
                jQuery(this).find(".sub").css({'width' :biggestRow}); //Set width
                jQuery(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
 
            } else { //If row does not exist...
 
                jQuery(this).calcSubWidth();  //Call function to calculate width of all ul's
                jQuery(this).find(".sub").css({'width' : rowWidth}); //Set Width
 
            }
        }
        //On Hover Out
        function megaHoverOut(){
          jQuery(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
              jQuery(this).hide();  //after fading, hide it
          });
        }
        //Set custom configurations
        var config = {
             sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
             interval: 100, // number = milliseconds for onMouseOver polling interval
             over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
             timeout: 500, // number = milliseconds delay before onMouseOut
             out: megaHoverOut // function = onMouseOut callback (REQUIRED)
        };
 
        jQuery("#topnav td .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
        jQuery("#topnav td").hoverIntent(config); //Trigger Hover intent with custom configurations
        
        jQuery("input[id*='searchtext']").css({'border' : '2px solid #ffffff', 
                                                'float' : 'left', 
                                                'margin-left' : '15px', 
                                                'margin-top' : '2px',
                                                'font-size' : '12px',
                                                'color' : '#000000'});

        	//Speed of the slideshow
	    var speed = 5000;
    	
	    //You have to specify width and height in #slider CSS properties
	    //After that, the following script will set the width and height accordingly
	    jQuery('#mask-gallery, #gallery li').width(jQuery('#slider').width());	
	    jQuery('#gallery').width(jQuery('#slider').width() * jQuery('#gallery li').length);
	    jQuery('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height(jQuery('#slider').height());
    	
	    //Assign a timer, so it will run periodically
	    var run = setInterval('newsscoller(0)', speed);	
    	
	    jQuery('#gallery li:first, #excerpt li:first').addClass('selected');
     
	    //Pause the slidershow with clearInterval
	    jQuery('#btn-pause').click(function () {
		    clearInterval(run);
		    return false;
	    });
     
	    //Continue the slideshow with setInterval
	    jQuery('#btn-play').click(function () {
		    run = setInterval('newsscoller(0)', speed);	
		    return false;
	    });
    	
	    //Next Slide by calling the function
	    jQuery('#btn-next').click(function () {
		    newsscoller(0);	
		    return false;
	    });	
     
	    //Previous slide by passing prev=1
	    jQuery('#btn-prev').click(function () {
		    newsscoller(1);	
		    return false;
	    });	
    	
	    //Mouse over, pause it, on mouse out, resume the slider show
	    jQuery('#slider').hover(
    	
		    function() {
			    clearInterval(run);
		    }, 
		    function() {
			    run = setInterval('newsscoller(0)', speed);	
		    }
	    ); 	
	
        });
        
        function newsscoller(prev) {
 
	        //Get the current selected item (with selected class), if none was found, get the first item
	        var current_image = jQuery('#gallery li.selected').length ? jQuery('#gallery li.selected') : jQuery('#gallery li:first');
	        var current_excerpt = jQuery('#excerpt li.selected').length ? jQuery('#excerpt li.selected') : jQuery('#excerpt li:first');
         
	        //if prev is set to 1 (previous item)
	        if (prev) {
        		
		        //Get previous sibling
		        var next_image = (current_image.prev().length) ? current_image.prev() : jQuery('#gallery li:last');
		        var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : jQuery('#excerpt li:last');
        	
	        //if prev is set to 0 (next item)
	        } else {
        		
		        //Get next sibling
		        var next_image = (current_image.next().length) ? current_image.next() : jQuery('#gallery li:first');
		        var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : jQuery('#excerpt li:first');
	        }
         
	        //clear the selected class
	        jQuery('#excerpt li, #gallery li').removeClass('selected');
        	
	        //reassign the selected class to current items
	        next_image.addClass('selected');
	        next_excerpt.addClass('selected');
         
	        //Scroll the items
	        jQuery('#mask-gallery').scrollTo(next_image, 800);		
	        jQuery('#mask-excerpt').scrollTo(next_excerpt, 800);					
        	
        }
 
