var cOpacity = 0.33;
var timeout = 500;
var closetimer = 0;
var promotimer = 0;
var ddmenuitem = 0;
var tmpmenu = null;
var mutex = false;

function slideSwitch(direction,event) {
	var $active = $('#main-promo IMG.active');
	if ( $active.length == 0 ) $active = $('#main-promo IMG:last');
	if (mutex==false || event==true) {
		if (direction==true) {
			var $next =  $active.next().length ? $active.next() : $('#main-promo IMG:first'); // use this to pull the images in the order they appear in the markup
			$active.addClass('last-active');
			$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() { $active.removeClass('active last-active'); });
		} else {
			var $prev =  $active.prev().length ? $active.prev() : $('#main-promo IMG:last'); // use this to pull the images in the order they appear in the markup
			$active.addClass('last-active');
			$prev.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() { $active.removeClass('active last-active'); });
		}
	}
	mutex=false;
	if (event==true) {mutex=true;}
}

function getYouTubeImg(url,size) {
	if(url === null){return "";}
	size = (size === null) ? "big" : size;
	var vid;var results;
	results = url.match("[\\?&]v=([^&#]*)");
	vid = ( results === null ) ? url : results[1];
	if(size == "small"){return "http://img.youtube.com/vi/"+vid+"/2.jpg";}else {return "http://img.youtube.com/vi/"+vid+"/0.jpg";}
}
function YouTubeImage(yId) {
//<script type="text/javascript">YouTubeImage('iPcQ-pTejZY');</script>
	if (yId===null || yId!=undefined || yId!='')
		document.write('<img src="'+getYouTubeImg(yId,"small")+'" width="140" />');
}

function solutions_open() {solutions_canceltimer();solutions_close();ddmenuitem=$(this).find('ul').css('visibility', 'visible');}
function solutions_close() {if(ddmenuitem) ddmenuitem.css('visibility','hidden');}
function solutions_timer() {closetimer=window.setTimeout(solutions_close,timeout);}
function solutions_canceltimer() {if(closetimer){window.clearTimeout(closetimer);closetimer=null;}}
document.onclick = solutions_close;

function openBlank(link){var nw=window.open(link,'_blank');nw.focus();return false;}

$(document).ready(function(){
	$(".tab-content").hide(); //Hide all content
	$("tr.navi-tabs td a.red").addClass("active"); //Activate first tab
	$(".tab-content:first").show(); //Show first tab content
	
	$("tr.navi-tabs td a").click(function() {
		$("tr.navi-tabs td a").removeClass("red").addClass("gr"); //Remove any "active" class
		$(this).removeClass("gr").addClass("red"); //Add "active" class to selected tab
		$(".tab-content").hide(); //fadeOut //Hide all
		var activeTab = $(this).attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
	});
	$("#slider").easySlider({ });
	
	//$('div#sol-block').bind('mouseover', solutions_open);
	//$('div#sol-block').bind('mouseout',  solutions_timer);
	
	$('div#main-promo img').click(function() {window.location.href=$(this).attr('href');/*openBlank($(this).attr('href'));*/});
	$('table img#nextBtn').click(function() {slideSwitch(false,true);});
	$('table img#prevBtn').click(function() {slideSwitch(true,true);});
	
	//$('a#preview').lightBox();
	
	if($("div#main-promo img").size()){promotimer=setInterval("slideSwitch(true)",5000);}
	
	$(".content-sep a[id^='preview']").prettyPhoto();

});

(function($) {
	$.fn.easySlider = function(options){
		var defaults = { speed:750, auto:true, pause:10000 };
		var options = $.extend(defaults, options);
		this.each(function() {
			var obj = $(this);
			var s = $("li", obj).length;
			var w = $("li", obj).width();
			var h = $("li", obj).height();
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);
			$("li", obj).css('float','left');
			$("img#prevBtn").parent().click(function(){ animate("next",true); });
			$("img#nextBtn").parent().click(function(){ animate("prev",true); });
			function animate(dir,clicked){
				var ot = t;
				switch(dir){
					case "next": t = (ot>=ts) ? 0 : t+1; break;
					case "prev": t = (t<=0) ? ts : t-1; break;
					default: break;
				};
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;
				p = (t*w*-1);
				$("ul",obj).animate( { marginLeft: p }, speed );
				if(clicked) clearTimeout(timeout);
				if(dir=="next" && !clicked){timeout = setTimeout(function(){ animate("next",false); },diff*options.speed+options.pause);};
			}; var timeout = setTimeout(function(){ animate("next",false); },options.pause);
		});
	};
	$.extend({
		jYoutube: function( url, size ){
			if(url === null){ return ""; }

			size = (size === null) ? "big" : size;
			var vid;
			var results;

			results = url.match("[\\?&]v=([^&#]*)");

			vid = ( results === null ) ? url : results[1];

			if(size == "small"){
				return "http://img.youtube.com/vi/"+vid+"/2.jpg";
			}else {
				return "http://img.youtube.com/vi/"+vid+"/0.jpg";
			}
		}
	})
})(jQuery);
