var $active = "";
var playSlideshow = 0;
var filename = "";
var linkurl = "";
var myhost = "";
var exec_flag = false;
var time_out = 0;

var pauseTime = 6000;
var scrollTime = 250;
var sleepTime  = 50;

$(document).ready(function() {	

	exec_flag = false;

	$(".main_image .desc").show(); //Show Banner
	$(".main_image .block").animate({ opacity:0.7 }, 1 ); //Set Opacity
	$("ul.image_thumb  li:first").addClass('active'); 
	
	//runs function on click
	$("ul.image_thumb  li").click(function(){ 
		$active = $(this);
		time_out = setTimeout( "slideSwitchClick()" , sleepTime);
		return false;
	})
	.hover(function(){ //Hover effects on list-item
		$(this).addClass('hover'); //Add class "hover" on hover
		}, function() {
		$(this).removeClass('hover'); //Remove class "hover" on hover out
	});
	
	//runs function, set timer here
	$(function() {
		playSlideshow = setInterval( "slideSwitchTimed()", pauseTime );
	});
	
	//pauses on hover
	$('#main').hover(function() {
		clearInterval(playSlideshow);
		playSlideshow = 0;
	},
	function() {
		playSlideshow = setInterval( "slideSwitchTimed()", pauseTime );
	}); 

	$('.mainVl').css('cursor','pointer');
	$('.mainVl').css('background-color','black');
	$('.mainVl').click(function() {
		filename = $('.mainVl').attr('src').replace(/\\/g,'/').replace(/.*\//, '');
		pageTracker._trackPageview('/gaCount/main_'+filename);
		linkurl = $('.main_image h2 a').attr('href');
		myhost = location.host;
		if (linkurl.substr(0, 7) == "http://" && linkurl.substr(0, myhost.length + 7) != "http://" + myhost) {
			window.open(linkurl, "_blank");
		} else {
			location.href=linkurl;
		}
	});

	$(window).unload(function(){
		clearInterval(playSlideshow);
		playSlideshow = 0;
	});

});


function slideSwitchTimed() {
	if(exec_flag) return false;
	$active = $('.image_thumb li.active').next();
	if ( $active.length == 0 ) $active = $('.image_thumb li:first'); //goes back to start when finishes
	slideSwitch();
}

function slideSwitchClick() {
	if (exec_flag) {
		setTimeout( "slideSwitchClick()" , sleepTime);
	} else {
		slideSwitch();
	}
}

var $prev = "";
var imgAlt = "";
var imgTitle = "";
var imgDesc = "";
var imgDescHeight = "";
function slideSwitch() {
	exec_flag = true;
	$prev = $('.image_thumb li.active');
	
	//Show active list-item
	$prev.removeClass('active');
	$active.addClass('active');
	
	//Set Variables
	imgAlt = $active.find('img').attr("alt"); //Get Alt Tag of Image
	imgTitle = $active.find('a').attr("href"); //Get Main Image URL
	imgDesc = $active.find('.block').html(); //Get HTML of the "block" container
	imgDescHeight = $(".main_image").find('.block').height(); //Find the height of the "block"
	
	if ($(this).is(".active")) { //If the list item is active/selected, then?c
		exec_flag = false;
		return false; // Don't click through - Prevents repetitive animations on active/selected list-item
	} else { //If not active then?c
	//Animate the Description
//		$(".main_image .block, .main_image .box, .main_image ul").animate({ opacity: 0.7}, scrollTime , function() {
//			$(".main_image .block, .main_image .box, .main_image ul").animate({ opacity: 0.7, marginBottom: - imgDescHeight + "px" }, scrollTime , function() {
			$(".main_image .block, .main_image .box, .main_image ul").animate({marginBottom: - imgDescHeight + "px" }, scrollTime , function() {
				$(".main_image img").attr({ src: imgTitle , alt: imgAlt}).animate({ opacity:1 }, scrollTime , function() {
//					$(".main_image .block, .main_image .box, .main_image ul").html(imgDesc).animate({ opacity: 0.7, marginBottom: "0px" }, scrollTime );
					$(".main_image .block, .main_image .box, .main_image ul").html(imgDesc).animate({marginBottom: "0px" }, scrollTime );
				});
			});
//		});
	}
	
	exec_flag = false;
	return false;
}

function slideUpCristal() {
}

