(function($) {
	$.fn.gallery = function(options) { return new Gallery(this.get(0), options); };
	
	function Gallery(context, options) { this.init(context, options); };
	
	Gallery.prototype = {
		options:{},
		init: function (context, options){
			this.options = $.extend({
				infinite: false,								
				duration: 700,									
				slideElement: 1,								
				autoRotation: false,							
				effect: false,									
				listOfSlides: 'ul > li',						
				switcher: false,								
				disableBtn: false,								
				nextBtn: 'a.link-next, a.btn-next, a.next',		
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',		
				circle: true,									
				direction: false,								
				event: 'click',									
				IE: false,										
				autoHeight: false								
			}, options || {});
			var _el = $(context).find(this.options.listOfSlides);
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			this.switcher = $(context).find(this.options.switcher);
			this.nextBtn = $(context).find(this.options.nextBtn);
			this.prevBtn = $(context).find(this.options.prevBtn);
			this.count = _el.index(_el.filter(':last'));
			
			if (this.options.switcher) this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
			else this.active = _el.index(_el.filter('.active:eq(0)'));
			if (this.active < 0) this.active = 0;
			this.last = this.active;
			
			this.woh = _el.outerWidth(true);
			if (!this.options.direction) this.installDirections(this.list.parent().width());
			else {
				this.woh = _el.outerHeight(true);
				this.installDirections(this.list.parent().height());
			}
			
			if (!this.options.effect) {
				this.rew = this.count - this.wrapHolderW + 1;
				if (!this.options.direction) this.anim = '{marginLeft: -(this.woh * this.active)}';
				else this.anim = '{marginTop: -(this.woh * this.active)}';
				eval('this.list.css('+this.anim+')');
			}
			else {
				this.rew = this.count;
				this.list.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
				this.switcher.removeClass('active').eq(this.active).addClass('active');
				if(this.options.autoHeight) this.list.parent().css({height: this.list.eq(this.active).outerHeight()});
			}
			this.flag = true;
			if (this.options.infinite){
				this.count++;
				this.active += this.count;
				this.list.append(_el.clone());
				this.list.append(_el.clone());
				eval('this.list.css('+this.anim+')');
			}
			
			this.initEvent(this, this.nextBtn, true);
			this.initEvent(this, this.prevBtn, false);
			if (this.options.disableBtn) this.initDisableBtn();
			if (this.options.autoRotation) this.runTimer(this);
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
		},
		initDisableBtn: function(){
			this.prevBtn.removeClass('prev-'+this.options.disableBtn);
			this.nextBtn.removeClass('next-'+this.options.disableBtn);
			if (this.active == 0 || this.count+1 == this.wrapHolderW) this.prevBtn.addClass('prev-'+this.options.disableBtn);
			if (this.active == 0 && this.count == 1 || this.count+1 <= this.wrapHolderW) this.nextBtn.addClass('next-'+this.options.disableBtn);
			if (this.active == this.rew) this.nextBtn.addClass('next-'+this.options.disableBtn);
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.ceil(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderW--;
		},
		fadeElement: function(){
			if ($.browser.msie && this.options.IE){
				this.list.eq(this.last).css({opacity:0});
				this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});
			}
			else{
				this.list.eq(this.last).animate({opacity:0}, {queue:false, duration: this.options.duration});
				this.list.removeClass('active').eq(this.active).addClass('active').animate({
					opacity:1
				}, {queue:false, duration: this.options.duration, complete: function(){
					$(this).css('opacity','auto');
				}});
			}
			if(this.options.autoHeight) this.list.parent().animate({height: this.list.eq(this.active).outerHeight()}, {queue:false, duration: this.options.duration});
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		scrollElement: function($this){
			if (!$this.options.infinite) eval('$this.list.animate('+$this.anim+', {queue:false, duration: $this.options.duration});');
			else eval('$this.list.animate('+$this.anim+', $this.options.duration, function(){ $this.flag = true });');
			if ($this.options.switcher) $this.switcher.removeClass('active').eq($this.active / $this.options.slideElement).addClass('active');
		},
		runTimer: function($this){
			if($this._t) clearTimeout($this._t);
			$this._t = setInterval(function(){
				if ($this.options.infinite) $this.flag = false;
				$this.toPrepare($this, true);
			}, this.options.autoRotation);
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				$this.active = $this.switcher.index($(this)) * $this.options.slideElement;
				if($this._t) clearTimeout($this._t);
				if ($this.options.disableBtn) $this.initDisableBtn();
				if (!$this.options.effect) $this.scrollElement($this);
				else $this.fadeElement();
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		initEvent: function($this, addEventEl, dir){
			addEventEl.bind($this.options.event, function(){
				if ($this.flag){
					if ($this.options.infinite) $this.flag = false;
					if($this._t) clearTimeout($this._t);
					$this.toPrepare($this, dir);
					if ($this.options.autoRotation) $this.runTimer($this);
				}
				return false;
			});
		},
		toPrepare: function($this, side){
			if (!$this.options.infinite){
				if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;
				if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;
				for (var i = 0; i < $this.options.slideElement; i++){
					if (side) { if ($this.active + 1 <= $this.rew) $this.active++; }
					else { if ($this.active - 1 >= 0) $this.active--; }
				};
			}
			else{
				if ($this.active >= $this.count + $this.count && side) $this.active -= $this.count;
				if ($this.active <= $this.count-1 && !side) $this.active += $this.count;
				eval('$this.list.css('+$this.anim+')');
				if (side) $this.active += $this.options.slideElement;
				else $this.active -= $this.options.slideElement;
			}
			if (this.options.disableBtn) this.initDisableBtn();
			if (!$this.options.effect) $this.scrollElement($this);
			else $this.fadeElement();
		},
		stop: function(){
			if (this._t) clearTimeout(this._t);
		},
		play: function(){
			if (this._t) clearTimeout(this._t);
			if (this.options.autoRotation) this.runTimer(this);
		}
	}
}(jQuery));


jQuery.fn.galleryAjax = function(options){
	// defaults options	
	var options = jQuery.extend({
		duration: 700
	},options);

	return this.each(function(){
		var hold = $(this);
		var speed = options.duration;
		var wrap = hold.find('ul');
		var el = hold.find('ul > li');
		var next = hold.find('a.next');
		var prev = hold.find('a.prev');
		var count = el.index(el.filter(':last'));
		var w = el.outerWidth();
		var wrapHolderW = Math.ceil(wrap.parent().width()/w);
		var active = 0;
		var temp = 0;
		var flag = false;
		
		el.find('a').css({textDecoration: 'none'});
		
		function loadIMG(ind){
			if (temp <= 2) {
				if(el.eq(ind).attr('title') != ""){
					var img = new Image();
					img.src = el.eq(ind).attr('title');
					el.eq(ind).attr('title', '');
					img = $(img);
					img.css({display: 'none'});
					if (el.eq(ind).find('a').length > 0) 
						el.eq(ind).find('a').empty().append(img);
					else 
						el.eq(ind).empty().append(img);
					img = el.eq(ind).find('img');
					if($.browser.msie && $.browser.version < 9){
						img.ready(function(){
							img.fadeIn(800);
							temp++;
							setTimeout(function(){
								loadIMG(ind + 1);
							}, 200);
						});
					}
					else{
						img.load(function(){
							img.fadeIn(800);
							temp++;
							setTimeout(function(){
								loadIMG(ind + 1);
							}, 200);
						});
					}
				}
				else{
					temp++;
					loadIMG(ind + 1);
				}
			}
			else {
				temp = 0;
				flag = true;
			}
		}
		
		loadIMG(active);
		
		function scrollEl(){
			wrap.eq(0).animate({marginLeft: -(w * active)}, {queue:false, duration: speed});
		}
		
		next.click(function(){
			if (flag){
				flag = false;
				active += 3;
				if (active > (count - wrapHolderW) && active != count+1) active = count - wrapHolderW+1;
				if (active == count+1) active = 0;
				scrollEl();
				loadIMG(active);
			}
			return false;
		});
		
		prev.click(function(){
			if (flag){
				flag = false;
				active -= 3;
				if (active < 0 && active != -3) active = 0;
				if (active == -3) active = count - wrapHolderW+1;
				scrollEl();
				loadIMG(active);
			}
			return false;
		});
	});
}



$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};


$(document).ready(function(){	
	
	$("#main-navigation li").hover(
		function(){ $("ul", this).fadeIn("normal"); }, 
		function() { } 
	);
	if (document.all) {
		$("#navigation li").hoverClass ("sfHover");
	}	
	
	// full screen video BG
	
	$("#container").css("left",($(window).width()-940)/2+"px"); 
	// only on homepage
	$("#footer").css("left",($(window).width()-940)/2+"px"); 
	$("#film-BG").css("height",$(window).height()+"px"); 		

	$(window).resize(function() {
		$("#container").css("left",($(window).width()-940)/2+"px"); 
		// only on homepage
		$("#footer").css("left",($(window).width()-940)/2+"px"); 
		$("#film-BG").css("height",$(window).height()+"px"); 		
    });
	
	// slide gallery
	$('div.gallery').gallery({
		slideElement: 3,
		duration: 700,
		listOfSlides: 'div > ul > li'
	});
		
	// slide gallery
	$('div.press-gallery').galleryAjax({
		duration: 700
	});	
	
	$("a.lightbox").fancybox({
		padding: 0,
		margin: 0,
		autoScale: false,
		overlayColor: '#000',
		titleShow: false,
		overlayShow: false,
		showNavArrows: false,
		onComplete: function(){
			var winW = $(window).width();
			var winH = $(window).height();
			if (winW < $('body').width()) winW = $('body').width();
			if (winH < $('body').height()) winH = $('body').height();
			var startW = $('#fancybox-img').outerWidth();
			var cof = winW / startW;
			var w = $('#fancybox-img').outerWidth();
			var h = $('#fancybox-img').outerHeight();
			var _time;
			
			$('#fancybox-content').css({height: winH, width: winW});
			$('#fancybox-img').css({height: h*cof, width: w*cof});
			$('#fancybox-wrap').css({height: winH, width: winW, visibility: 'visible', margin: 0});
			
			var raz = h*cof-winH;
			var now = winH/raz;
			
			$('#fancybox-wrap').mousemove(function(e){
				if (raz > 0) $('#fancybox-img').css({marginTop:-e.pageY/now});
			});
			
			$(window).resize(function(){
				$('#fancybox-img').css({marginTop: 0});
				if(_time) clearTimeout(_time);
				winW = $(window).width();
				winH = $(window).height();
				if (winW < $('body').width()) winW = $('body').width();
				if (winH < $('body').height()) winH = $('body').height();
				cof = winW / startW;
				
				$('#fancybox-wrap').css({height: winH, width: winW});
				$('#fancybox-content').css({height: winH, width: winW});
				$('#fancybox-img').css({height: h*cof, width: w*cof});
				
				_time = setTimeout(function(){
					winW = $(window).width();
					winH = $(window).height();
					if (winW < $('body').width()) winW = $('body').width();
					if (winH < $('body').height()) winH = $('body').height();
					cof = winW / startW;
					
					raz = h*cof-winH;
					now = winH/raz;
					$('#fancybox-wrap').css({height: winH, width: winW});
					$('#fancybox-content').css({height: winH, width: winW});
					$('#fancybox-img').css({height: h*cof, width: w*cof});
				}, 100);
			});
		}
	});

	
});


