jQuery(document).ready(function(){
	initMenu();
	jQuery("#featured").kricordion({
			slides: '.featured',
			animationSpeed: 900,
			autorotation: true,
			autorotationSpeed:5,
			event: 'mouseover',
			imageShadow:true,
			imageShadowStrength:0.5
	});
	
	
	
});

function slideSwitch() {
    var $active = $('#slideshow DIV.active');
 
    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
 
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');

 
    $active.addClass('last-active');
 
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

jQuery.fn.equalHeights = function() {
    return this.height(Math.max.apply(null,
        this.map(function() {
           return jQuery(this).height(); }).get()
    ));
};





(function($)
{
	$.fn.kriesi_image_preloader = function(options) 
	{
		var defaults = 
		{
			repeatedCheck: 500,
			fadeInSpeed: 1000,
			delay:150,
			callback: ''
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function()
		{
			var imageContainer = jQuery(this),
				images = imageContainer.find('img').css({opacity:0, visibility:'hidden'}),
				imagesToLoad = images.length;				
				
				imageContainer.operations =
				{	
					preload: function()
					{	
						var stopPreloading = true;
						
						images.each(function(i, event)
						{	
							var image = $(this);
							
							
							if(event.complete == true)
							{	
								imageContainer.operations.showImage(image);
							}
							else
							{
								image.bind('error load',{currentImage: image}, imageContainer.operations.showImage);
							}
							
						});
						
						return this;
					},
					
					showImage: function(image)
					{	
						imagesToLoad --;
						if(image.data.currentImage != undefined) { image = image.data.currentImage;}
												
						if (options.delay <= 0) image.css('visibility','visible').animate({opacity:1}, options.fadeInSpeed);
												 
						if(imagesToLoad == 0)
						{
							if(options.delay > 0)
							{
								images.each(function(i, event)
								{	
									var image = $(this);
									setTimeout(function()
									{	
										image.css('visibility','visible').animate({opacity:1}, options.fadeInSpeed);
									},
									options.delay*(i+1));
								});
								
								if(options.callback != '')
								{
									setTimeout(options.callback, options.delay*images.length);
								}
							}
							else if(options.callback != '')
							{
								(options.callback)();
							}
							
						}
						
					}

				};
				
				imageContainer.operations.preload();
		});
		
	};
})(jQuery);


(function($)
{
	$.fn.kricordion = function(options) 
	{
		var defaults = 
		{
			slides: '>div',	
			animationSpeed: 900,
			autorotation: true,
			autorotationSpeed:3,
			easing: 'easeOutQuint',	
			event: 'mouseover',
			imageShadow:true,
			imageShadowStrength:0.5,
			fontOpacity: 1		
			
		};
		

		var options = $.extend(defaults, options);
		
		return this.each(function()
		{	

			var slideWrapper 	= $(this),	
				slides			= slideWrapper.find(options.slides).css('display','block'),	
				slide_count 	= slides.length,						
				slide_width		= slideWrapper.width() / slide_count,	
				expand_slide 	= slides.width(),						
				minimized_slide	= (slideWrapper.width() - expand_slide) / (slide_count - 1), 
				overlay_modifier = 200 *(1- options.imageShadowStrength),		
				excerptWrapper = slideWrapper.find('.feature_excerpt'),
				interval = '',
				current_slide = 0;
			
				
			
			excerptWrapper.wrap('<span class="feature_excerpt"></span>').removeClass('feature_excerpt').addClass('position_excerpt');
			excerptWrapper = slideWrapper.find('.feature_excerpt');
			
			
				
			
			excerptWrapper.equalHeights().find('.position_excerpt').css({display:'block', opacity:0, position:'absolute'});
			var excerptWrapperHeight = excerptWrapper.height();
			
			
						
			
			
			slides.each(function(i)
			{
				var this_slide = $(this),											
					this_slide_a = this_slide.find('a'),							
					real_excerpt = this_slide.find('.position_excerpt'),			
					real_excerpt_height = real_excerpt.height(),					
					slide_heading =this_slide.find('.sliderheading'),  				
					cloned_heading =   slide_heading.clone().appendTo(this_slide_a) 
													.addClass('heading_clone')
													.css({opacity:options.fontOpacity, width:slide_width-30}),
					clone_height = cloned_heading.height();							
					
					
					this_slide.css('backgroundPosition',parseInt(slide_width/2-8) + 'px ' + parseInt((this_slide.height()- real_excerpt_height)/2 -8) + 'px');						
					
					cloned_heading.css({bottom: (excerptWrapperHeight-clone_height)/2 +9});
					real_excerpt.css({bottom: (excerptWrapperHeight-real_excerpt_height)/2 +9});
												
					this_slide.data( 
					'data',
					{
						this_slides_position: i * slide_width,							
						pos_active_higher: i * minimized_slide,							
						pos_active_lower: ((i-1) * minimized_slide) + expand_slide		
					});

				this_slide.css({zIndex:i+1, left: i * slide_width, width:slide_width + overlay_modifier});
								

				if(options.imageShadow)
				{
					this_slide.find('>a').prepend('<span class="fadeout ie6fix"></span>');
				}
								
			});
			

			jQuery('#featured').kriesi_image_preloader({callback:add_functionality});
			
			function add_functionality()
			{
				
			
				
				
				if(options.autorotation)
				{
					interval = setInterval(function() { autorotation(); }, (parseInt(options.autorotationSpeed) * 1000));
				}
				
				slides.each(function(i)
				{	
					var this_slide = $(this), 
						real_excerpt = this_slide.find('.position_excerpt'), 
						cloned_heading = this_slide.find('.heading_clone');
						

					this_slide.bind(options.event, function(event, continue_autoslide)
					{	

						if(!continue_autoslide)
						{
							clearInterval(interval);
						}
						
						var objData = this_slide.data( 'data' );

						real_excerpt.stop().animate({opacity:options.fontOpacity},options.animationSpeed, options.easing);
						cloned_heading.stop().animate({opacity:0},options.animationSpeed, options.easing);
						
						this_slide.stop().animate({	width: expand_slide + (overlay_modifier * 1.2), 
													left: objData.pos_active_higher},
													options.animationSpeed, options.easing);
											

						slides.each(function(j){
						
							if (i !== j)
							{	
								var this_slide = $(this),
									real_excerpt = this_slide.find('.position_excerpt'),
									cloned_heading = this_slide.find('.heading_clone'),
									objData = this_slide.data( 'data' ),
									new_pos = objData.pos_active_higher;
									
								if(i < j) { new_pos = objData.pos_active_lower; }
								this_slide.stop().animate({left: new_pos, width:minimized_slide + overlay_modifier},options.animationSpeed, options.easing);
								real_excerpt.stop().animate({opacity:0},options.animationSpeed, options.easing);
								cloned_heading.stop().animate({opacity:options.fontOpacity},options.animationSpeed, options.easing);
							}
						
						});
						
					});
				});
				

				slideWrapper.bind('mouseleave', function()
				{
					slides.each(function(i)
					{
						var this_slide = $(this),
							real_excerpt = this_slide.find('.position_excerpt'),
							cloned_heading = this_slide.find('.heading_clone'),
							objData = this_slide.data( 'data' ),
							new_pos = objData.this_slides_position;
							
							this_slide.stop().animate({left: new_pos, width:slide_width + overlay_modifier},options.animationSpeed, options.easing);
							real_excerpt.stop().animate({opacity:0},options.animationSpeed, options.easing);
							cloned_heading.stop().animate({opacity:options.fontOpacity},options.animationSpeed, options.easing);
					});
					
				});
			}

			function autorotation()
			{	
				if(slide_count  == current_slide)
				{
					slideWrapper.trigger('mouseleave');
					current_slide = 0;
				}
				else
				{
					slides.filter(':eq('+current_slide+')').trigger(options.event,[true]);
					current_slide ++;
				}
			}
		});
	};
})(jQuery);


jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {

		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});



$(function() {
    setInterval( "slideSwitch()", 5000 );
});

$(document).ready(function() {

	slidePortfolioIndex(5000, 690);
	slidePortfolioCaption();
	slidingButton();
	imageLoader();
	resetField('.search_input');
	slidePortfolio('.film_image a');
	fadeOthers('.film_item');
	$("a[rel^='prettyPhoto']").prettyPhoto();
});


function slidePortfolioIndex($time, $full_image_width) {
	
	$addCustom = $(".index_port_item.current").length;
	if($addCustom == 0) {
		$(".index_port_item:first").addClass("current");
	}

	$totalPortfolio = ($(".index_port_item").length) - 1; 
	$widthDivide = 960 - $full_image_width;
	$tileWidth = parseInt($widthDivide / $totalPortfolio);
	
	$caption_width = $full_image_width - 40;
	
	$caption_width = $caption_width + 'px';
	$full_image_width = $full_image_width + 'px';
	
	$(".index_port_item").css("width", $tileWidth);
	$(".index_port_item.current").css("width", $full_image_width);
	$(".caption").css({ width: $caption_width, opacity: 0 });
	
	if(typeof(isClicked) == 'undefined') { var isClicked = 0; } 
	
	$(".index_port_item").click(function() { 
		isClicked = 1; 
		
		var classClicked = $(this).attr('class'); 
		
		if(classClicked !== 'index_port_item current') { 
			$(".index_port_item.current").removeClass("current").addClass("prev").stop().animate({ width: $tileWidth }, 500);
			$(this).addClass("current").css("width", $tileWidth).stop().animate({ width: $full_image_width }, 800);
			$(".index_port_item.prev").removeClass("prev"); 
		}

	});
	
	if(typeof(isHovered) == 'undefined') { var isHovered = 0; }
		
	$(".index_port_item").hover(function() { 
		isHovered = 1; 
	}, function() {
		isHovered = 0;
	});
	
	setInterval(function() {		 
		if(isHovered == 0 && isClicked == 0) { 
			nextPortfolioItem('690px', $tileWidth);
		}
	}, $time);
}

function nextPortfolioItem($full_image_width, $tile_width) {
	
	$current = $(".index_port_item.current");
	$next = $(".index_port_item.current").next();
	
	if($next.attr('class') == undefined) {
		$next = $(".index_port_item:first");
	} else {
	
	}
		
	$current.removeClass("current").css("width", $full_image_width).stop().animate({ width: $tile_width }, 500);
	$next.addClass("current").css("width", $tile_width).stop().animate({ width: $full_image_width }, 800);
	
}

function slidePortfolioCaption() {
	
	$(".index_port_item").children('a').hover(function() {
		$(this).parents().children('.caption').stop().animate({ opacity: 1 }, 800);
	}, function() {
		$(this).parents().children('.caption').stop().animate({ opacity: 0 }, 500);
	});
	
}

function slidingButton() {
	
	$(".slide_button_right a").hover(function() {
		$(this).stop().animate({ width: '99px' }, 300);
	}, function () {
		$(this).stop().animate({ width: '78px' }, 150);
	});
	
	$(".slide_button_left a").hover(function() {
		$(this).stop().animate({ width: '99px' }, 300);
	}, function () {
		$(this).stop().animate({ width: '78px' }, 150);
	});
	
}

function resetField($field) {
	
	$($field).one("click", function() {
		$(this).attr('value', '');
	});
	
}

function slidePortfolio($class) {
	
	$($class).hover(function() {
		
		$(this).children('.over').stop().animate({ top: '-120px' }, 300);
		
	}, function() {
		
		$(this).children('.over').stop().animate({ top: '0' }, 150);
		
	});
	
}

function imageLoader() {
    $('.loader').each(function () {
        var loader = $(this);
        var imagePath = loader.attr('title');
        var img = new Image();
        $(img).css("opacity", "0.0").load(function () {
            loader.append(this).removeAttr('title');
            $(this).css("margin", "0px").css("opacity", "0.0").animate({
                opacity: 1.0
            },
            500, function () {
                loader.css("background-image", "none");
            });
        }).attr('src', imagePath);
    });
}

function fadeOthers($class) {
	
	$($class).hover(function() {
		
		$($class).stop().animate({ opacity: 0.4 }, 300);
		$(this).stop().css({ opacity: 1 });
		
	}, function() {
		
		$($class).stop().animate({ opacity: 1 }, 150);
		
	});
	
}

function initMenu() {
	  $('#solmenu ul').hide();
	  $('#solmenu ul:first').show();
	  $('#solmenu li a').click(
	  function() {
	  var checkElement = $(this).next();
	  if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
	  return false;
	  }
	  if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
	  $('#solmenu ul:visible').slideUp('normal');
	  checkElement.slideDown('normal');
	  return false;
	  }
	  }
	  );
	  }	