jQuery(function() {

    jQuery("a[rel^='prettyPhoto']").prettyPhoto({ theme: 'light_rounded' });

    /* Clever input */
    jQuery('.clever-input')
	    .live('click', function() {
	        if (jQuery(this).val() == jQuery(this).attr('title')) {
	            jQuery(this).val('');
	        } else {
	            if (jQuery(this).val() == '') {
	                jQuery(this).val(jQuery(this).attr('title'));
	            }
	        }
	    })
	    .live('blur', function() {
	        jQuery(this).trigger('click');
	    });

    if (jQuery.browser.msie) {
        jQuery('#site-head .nav li:first, #site-head .nav li:last').append('<div class="ie-corner" />');
        jQuery('#site-main .side .panel div.top-bar').append('<span class="left" /><span class="right" />');
    }
});

/*
 * TabRotator 1.0.0 - Javascript Tab Image Rotator
 *
 * Copyright: Skylight Media Ltd.
 *
 * Author: Skylight Media Ltd.
 *
 * $Date: 24-06-2010
 *
 *	ChangeLog:
 *  
 * Unauthorized use of this script is not permitted, please contact Skylight Media Ltd. to request use of this script
 */
(function($)
{
    $.fn.TabRotator = function(options)
    {
        var settings = {
            BUTTONS_CLICK_TIMEOUT : null,
            BUTTONS_HOVER_DELAY : 400,
            BUTTONS_OPACITY_START : 0.2,
            BUTTONS_OPACITY_END : 0.8,
            BUTTONS_OPACITY_DURATION : 250,
            ROTATE_INTERVAL : 3000,
            ROTATE_INTERVAL_SPEEDY : 750,
            ROTATE_SPEED : 500,
            ITEMS_COUNT : 0,
            ROTATOR_TIMER : null,
            SCROLL_COUNT : 0,
            ALLOW_SCROLL : true,
            SPEEDY: false,
            INVERSE : false,
            IMAGE_FADE_DURATION : 600,
            TAB_SIZE : 74
        };

		/* extend/change options */
		options = options || {};
    	$.extend(settings, options);
    	
		return this.each(function()
		{
			var $cont = $(this);
			
			/* Get container */
			$cont.addClass("tab-rotator");
			
			/* Get count and set scroll count to end */
    	    settings.ITEMS_COUNT = jQuery('.text ul li', $cont).length;
    	    settings.SCROLL_COUNT = settings.ITEMS_COUNT;
    	    
    	    /* Append overlay */
    	    jQuery('.images', $cont).prepend('<div class="overlay" />');
    	    
    	    /* Append buttons */
    	    jQuery('.text', $cont).append('<div class="scroll-btn prev"><span /></div><div class="scroll-btn next"><span /></div>')
    	        .find(".scroll-btn").css({ opacity: settings.BUTTONS_OPACITY_START, height: settings.TAB_SIZE + "px" });
    	        
    	    /* Text container hover */
    	    jQuery('.text', $cont).hover(function() {
                    clearInterval(settings.ROTATOR_TIMER);
                    settings.ALLOW_SCROLL = false;
                }, function() {            
                    clearInterval(settings.ROTATOR_TIMER);
                    settings.ROTATOR_TIMER = setInterval(RotateTextPanel, settings.ROTATE_INTERVAL);
                    settings.ALLOW_SCROLL = true;
                }
            );
            
            /* Button hover + click */
            jQuery('.text .scroll-btn', $cont).hover(
                function(){
                    jQuery(this).stop().fadeTo(settings.BUTTONS_OPACITY_DURATION, settings.BUTTONS_OPACITY_END);
                    settings.SPEEDY = true;
                    clearInterval(settings.ROTATOR_TIMER);
                    settings.ALLOW_SCROLL = false;
                    settings.INVERSE = jQuery(this).hasClass("prev");
                    
                    settings.BUTTONS_CLICK_TIMEOUT = setTimeout(function(){
                        settings.ALLOW_SCROLL = true;
                        RotateTextPanel();
                        settings.ROTATOR_TIMER = setInterval(RotateTextPanel, settings.ROTATE_INTERVAL_SPEEDY);
                        clearTimeout(settings.BUTTONS_CLICK_TIMEOUT);
                    }, settings.BUTTONS_HOVER_DELAY);                    
                }, 
                function(){
                    jQuery(this).stop().fadeTo(settings.BUTTONS_OPACITY_DURATION, settings.BUTTONS_OPACITY_START);
                    settings.SPEEDY = false;
                    settings.ALLOW_SCROLL = true;
                    settings.INVERSE = false;
                    clearInterval(settings.ROTATOR_TIMER);
                    clearTimeout(settings.BUTTONS_CLICK_TIMEOUT);
                }
            ).click(function(){
                clearTimeout(settings.BUTTONS_CLICK_TIMEOUT);
                settings.ALLOW_SCROLL = false;
                jQuery('.text li a:eq('+ (settings.SCROLL_COUNT + (settings.INVERSE ? 0 : +3)) +')', $cont).trigger('click');
            });
            
            /* Text links click */
            jQuery('.text li a', $cont).click(function(e){
                e.preventDefault();
                jQuery(this).closest('li').addClass("active").siblings().removeClass("active");
                
                indx = jQuery('.text li a', $cont).index(jQuery(this));
                jQuery('.images .overlay', $cont).show().css({"z-index": 100, "opacity": 0.4}).fadeOut(settings.IMAGE_FADE_DURATION);
                jQuery('.images a:eq(' + indx + ')', $cont).css("z-index", 99).siblings().not('.overlay').css("z-index", 1);
            });
    	    
    	    /* Start rotator */
    	    RotateTextPanel();
    	    settings.ROTATOR_TIMER = setInterval(RotateTextPanel, settings.ROTATE_INTERVAL);
			
			function RotateTextPanel() {
                if (settings.ALLOW_SCROLL)
                {   
                    if (settings.SCROLL_COUNT >= (jQuery('.text ul li', $cont).length-4) && settings.SPEEDY && !settings.INVERSE) return;
                    
                    if (settings.SCROLL_COUNT >= (jQuery('.text ul li', $cont).length-4) && !settings.SPEEDY) {
                        /* At the end of the scroll, see if we need to loop through more images */
                        indx = jQuery('.text ul li', $cont).index(jQuery('.text ul li.active', $cont));
                        if (indx < (settings.ITEMS_COUNT-1) && indx > -1)
                        {
                            jQuery('.text li a:eq('+ (indx+1) +')', $cont).trigger('click');
                            return;
                        }
                        
                        jQuery('.text ul', $cont).animate({ top: "0" }, settings.ROTATE_SPEED);
                        settings.SCROLL_COUNT = 0;
                        jQuery('.text li a:eq(0)', $cont).trigger('click');
                        return;
                    }
                    
                    if (settings.SCROLL_COUNT == 0 && settings.INVERSE) return;                    

                    jQuery('.text ul', $cont).animate({ top: (settings.INVERSE ? "+=" : "-=") + settings.TAB_SIZE }, settings.ROTATE_SPEED);
                    settings.INVERSE ? (settings.SCROLL_COUNT = settings.SCROLL_COUNT-1) : (settings.SCROLL_COUNT = settings.SCROLL_COUNT+1);
                    
                    if (!settings.SPEEDY)
                    {
                        jQuery('.text li a:eq('+ settings.SCROLL_COUNT +')', $cont).trigger('click');
                    }
                }
            }		
		});		
	}
})(jQuery);
