/* this prevents dom flickering, needs to be outside of dom.ready event: */
document.documentElement.className += 'js_active';
/*end dom flickering =) */

$(document).ready(function() {

	$(".portfolio_entry a:has(img)").each(function() {	
		var $image = $(this).contents("img");
		$newclass = 'lightbox_video';
	
		if($(this).attr('href').match(/(jpg|gif|jpeg|png|tif)/)) $newclass = 'lightbox_image';
		
		if ($image.length > 0)
		{	
			if($.browser.msie &&  jQuery.browser.version < 7) $(this).addClass('ie6_lightbox');
		
			var $bg = $("<span class='"+$newclass+" ie6fix'></span>").appendTo($(this));
		
			$(this).bind('mouseenter', function(){
				$height = $image.height();
				$width = $image.width();
				$pos =  $image.position();		
				$bg.css({height:$height, width:$width, top:$pos.top, left:$pos.left});
			});
		}
	});

	$(".portfolio_entry a:has(img)").hover(function () {
    	$(this).find('img').animate({
    		opacity: 0.5}, 200, function() {
  		});
  	}, 
  	function () {
    	$(this).find('img').animate({
    		opacity: 1}, 200, function() {
  		});
  	}
	);

	$('.fancybox').fancybox({
		overlayColor: '#000000'
	});
	
	$("#content a:has(img)").fancybox({
		overlayColor: '#000000'
	});
	
	$('.fancybox-iframe').fancybox({
		overlayColor: '#000000',
		type: 'iframe',
		width: 920,
		autoScale: true,
		padding: 0
	});
	
	$('.carousel').fadeIn('fast');

	$(".carousel").carouFredSel({
		direction	: "right",
		items		: 2,
		width: 400,
		scroll : {
			items			: 2,
			easing			: "easeOutSine",
			duration		: 1500
		},
		auto : {
			pauseDuration	: 6000,
			pauseOnHover	: true
		}
});
	
	k_form(); //controls the contact form
	k_menu(); // controls the dropdown menu
	
	
});



$(window).load(function(){
	var autorotate = $("meta[name=autorotate]").attr('content');
	var autorotate_duration = $("meta[name=autorotate_duration]").attr('content');
	
	/*image slider (alternate)*/
	var $slide_images = jQuery('#featured_image img');	
	if ($slide_images.length > 0 && autorotate != "2")
	{	
		if(autorotate_duration == '') autorotate_duration = 3;
	
		$slide_images.not('.current_img img').css({display:"none"});
		$slide_images.filter('.current_img img').css({visibility:"visible"});
		// set the automatic image rotation, number is time between transitions in miliseconds
		interval = setInterval(function() { k_fader("#featured_image img",'65'); }, (parseInt(autorotate_duration) * 1000)); 	
	}
});



function k_menu()
{
	$("#nav a, .subnav a").removeAttr('title');
	$(" #nav ul ").css({display: "none"}); // Opera Fix
	
	$("#nav li").each(function()
	{	
		
		var $sublist = $(this).find('ul:first');
		
		$(this).hover(function()
		{	
			$sublist.stop().css({overflow:"hidden", height:"auto", display:"none"}).slideDown(800, function()
			{
				$(this).css({overflow:"visible", height:"auto"});
			});	
		},
		function()
		{	
			$sublist.stop().slideUp(800, function()
			{	
				$(this).css({overflow:"hidden", display:"none"});
			});
		});	
	});
}



function k_fader($items_to_fade, $next_or_prev)
{	
	var $items = jQuery($items_to_fade);
	var $currentitem = $items.filter(":visible");
	var $new_item;
	var $selector;
	
	$items.css('visibility','visible');
	
	if($items.length > 1)
	{
		for(i = 0; i < $items.length; i++)
		{
			if($items[i] == $currentitem[0])
			{	
				$selector = $next_or_prev >= 0 ? i != $items.length-1 ? i+1 : 0 : i == 0 ? $items.length-1 : i-1;
				
				$new_item = jQuery($items[$selector]);
				break;
			}
		}
		
		if( $new_item && $new_item.css("display") == "none" )
			{	
				$currentitem.css({zIndex:2});
				$new_item.css({zIndex:3}).fadeIn(1800, function()
				{
					$currentitem.css({display:"none"});
				});
				
			}
	}
}
