$(document).ready(function() {
    var $videoTabBtn, $tabs;


	// Slider
	$slider = $(".slider")

	if($slider.length > 0){
		slider();
	}

	
	$('.internal #content-right ul li:last-child').addClass('last');


	// Product tabs	
	$tabs = $( "#tabs" )
	$tabs.tabs();

	
	// Accordion
	$('.feature-box div').hide();
	$('.mis-box div').hide();
	$('.trigger:first').addClass('active').next().show();
	$('.trigger').click(function(){
		if( $(this).next().is(':hidden') ) {
			$('.trigger').removeClass('active').next().slideUp();
			$(this).toggleClass('active').next().slideDown();
		}
		return false;
	});
	
	
	// Add pdf icons to pdf links
	$("a[href$='.pdf']").parents('li').addClass("pdf");
	// Add doc icons to document links (docx, doc, rtf, txt)
	$("a[href$='.docx'], a[href$='.doc'], a[href$='.txt'], a[href$='.rft']").parents('li').addClass("doc");
	// Add powerpoint icons to powerpoint file links (pptx, ppt)
	$("a[href$='.ppt'], a[href$='.pptx']").parents('li').addClass("ppt"); 



    //Video Analytics, but only if the video is on the page
    $videoTabBtn = $("#videos");
    if($videoTabBtn.length > 0){

        //Add the Youtube API Script if a video is detected
        var tag = document.createElement('script');
        tag.src = "http://www.youtube.com/player_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);



        //Bind to show tab event
        $tabs.bind("tabsshow", function (e, tabObj) {

            //If this is the video tab that has just been shown...
            if($(tabObj.panel).attr("id") == "videos"){
                
                // ... and track that the video has been shown
                _gaq.push("_trackEvent", "Video", "Shown", undefined, "1");
            }
        });
    }
 
})


// ==================================
// = Youtube API Functions
// ==================================


function onYouTubePlayerAPIReady() {
    for(var i = 0; i < videos.length; i++){
        var player;
        
        player = new YT.Player(videos[i].node, {
            height: '442',
            width: '589',
            videoId: videos[i].id
        });
        

        //State changes
        player.addEventListener("onStateChange", "onYouTubePlayerStateChange")

        videos[i].player = player; 
    }
}


function onYouTubePlayerStateChange(state) {

    //Play
    if(state.data == 1){
        _gaq.push("_trackEvent", "Video", "Play", undefined, 1);
    }

    //Stop
    if(state.data == 2){
        _gaq.push("_trackEvent", "Video", "Stop", undefined, state.target.getCurrentTime());
    }
}




// ================
// = Image Slider =
// ================
function slider(){
	
	var imageCache = [], animating = false, currentItem = 0, autoPlay = true, timeoutHandler;

	/*
	 * Config  times
	 */
	var transition 			= 300;
	var overlayTransition 	= 400;
	var autoPlayDelay		= 5000;
	
	
	/*
	 * Nodes
	 */
	var $slideWrapper 		= $(".slider");
	var $slideItems			= $slideWrapper.find(".slide");
	var $slideNavItems		= $slideWrapper.find(".slide-nav li:not(:first-child) a");
	
	
	
	// ==============
	// = Initial UI =
	// ==============
	$slideItems.eq(currentItem).addClass("current");
	$slideNavItems.eq(0).addClass("active");
	
	// ======================
	// = Autoplay attribute =
	// ======================
	if($slideWrapper.data("autoplay") == true){
		autoPlay = true;
	};
	autoPlayNext();
	
	
	
	// ==================
	// = Preload images =
	// ==================
	$slideItems.find("img").each(function(i, item){
		var img = document.createElement("img");
		img.src = item.src;
		imageCache.push(img);
	})
	

	
	
	// ==========
	// = Events =
	// ==========
	
	 // Cycle through the nav items, attaching hover event handling
	$slideNavItems.each(function(i, item){
		
		var position = i; // Current nav item - also represents current image
		

		
		$slideNavItems.eq(position).click(function(e) {
			
			e.preventDefault();
			
			clearTimeout(timeoutHandler);
			autoPlay = false;
			
			
			//Bail out if currently animating
			if(animating || position == currentItem) return;
			
			slideTo(position);
			
			/*
			 * Reset the nav wrapper and add the heavy and current classes
			 */
			var liClass = $slideNavItems.eq(position).attr('class');
			$slideNavItems.eq(position).parent().attr("class", "slider-nav medium");
			$slideNavItems.eq(position).parent().addClass(liClass);
		})
	})
	
	
	
	
	function slideTo(newItem) {
		
		//Bail out if currently animating
		if(animating || newItem == currentItem) return;
		
		animating = true;
		
		// Prep z-indexes & visibility
		$slideItems.eq(newItem).addClass("pre_slide");
		
		//Set the current active nav item
		$slideWrapper.find("li a").removeClass("active");
		$slideNavItems.eq(newItem).addClass("active");
		
		// Fade out the top item	
		$slideItems.eq(currentItem).fadeOut(transition, function(){
				
				
				
				
				//Reset the old item
				$slideItems.eq(currentItem).removeClass("current").hide();
				$slideItems.eq(newItem).removeClass("pre_slide").fadeIn().addClass("current");
				
				currentItem = newItem;
				animating = false;
				
				autoPlayNext()

		});
		
	}

	
	
	function autoPlayNext () {
		
		if(autoPlay && $slideItems.length > 1){
			timeoutHandler = setTimeout(function(){
				var next = currentItem+1;
				if(next == $slideItems.length){
					next = 0
				}
		
				slideTo(next);
			}, autoPlayDelay)
			
		}
	}
}

