$(document).ready(function() {
	$(".box").hover(function() {
		$(this).find('.title').stop().animate({bottom: '0px'}, 400,"easeOutQuad");
	}, function() {
		$(this).find('.title').stop().animate({bottom: '-38px'}, 400,"easeInQuad");
	});
	
	$(".dropdown").bind("click",function(){
		if($(this).find('li ul').hasClass("down")) {
			$(this).find('li ul').slideUp("fast",function(){$(this).removeClass("down")});
		} else {
			$(this).find('li ul').slideDown("fast",function(){$(this).addClass("down");});			
		}
	});
	NewsScroller.init();
	$('#project_box .project_items .project_item').mouseenter(function(){$(this).css('backgroundColor','#FAFAFA')});
	$('#project_box .project_items .project_item').mouseleave(function(){$(this).css('backgroundColor','#FFFFFF')});
	$('#news_box .news_items .news_item').mouseenter(function(){$(this).css('backgroundColor','#FAFAFA')});
	$('#news_box .news_items .news_item').mouseleave(function(){$(this).css('backgroundColor','#FFFFFF')});
	
	$("a.fancybox").fancybox();
	$(".gallery").fancybox({
		'showCloseButton'	: true,
		'enableEscapeButton': true,
		'centerOnScroll'	: true,
		'titlePosition' 	: 'inside'
	});
	$(".iframe").fancybox({
		'showCloseButton'	: true,
		'enableEscapeButton': true,
		'autoScale'			: true,
		'enableEscapeButton': true,
		'centerOnScroll'	: true,
		'type' 				: 'iframe',
		'width' 			: '95%',
		'height' 			: '90%'
	});
	$(".inline").fancybox({
		'showCloseButton'	: true,
		'enableEscapeButton': true,
		'autoScale'			: true,
		'centerOnScroll'	: true,
		'type'				: 'ajax',
		ajax				:  {
			type 			: "POST",
			data        	: 'fancybox=true' 

		}
	});
	$(".pdf").fancybox({
		'showCloseButton'	: true,
		'enableEscapeButton': true,
		'autoScale'			: true,
		'centerOnScroll'	: true,
		'type' 				: 'iframe',
		'width' 			: '95%',
		'height' 			: '90%'
	});
});

SlideSwitch = {
	init: function(settings) {
		settings = settings || {}
		this.settings = settings;
		this.rolInit(settings.offset);
		
		//Set auto slide
		//setInterval( "SlideSwitch.move('next')", 10000 );
		
		$('#slide').bind("mouseenter",function(){
			$(this).find(".left, .right").css({visibility:"visible",opacity:0}).animate({opacity:0.5},500);
		});

		$('#slide').bind("mouseleave",function(){
			$(this).find(".left, .right").animate({opacity:0},500);
		});

		$('#slide .left').bind("click",function(){
			SlideSwitch.move('prev');
			return false;
		});
		
		$('#slide .right').bind("click",function(){
			SlideSwitch.move('next');
			return false;
		});
		
		$('.rol .rol_left').bind("click",function(){
			SlideSwitch.rolMove('prev');
			return false;
		});
		
		$('.rol .rol_right').bind("click",function(){
			SlideSwitch.rolMove('next');
			return false;
		});
		
		this.updateCounter();
	},
	rolMove: function(orient) {
		pos = $('.rol .images').position();
		var visibleWidth = $('.rol').width();
		var numberOfPositions = $('.rol .images img').size();
		var move = $('.rol .images .scroll').outerWidth() / $('.rol .images img').size();
		var actualPosition = $('.rol').data('actualPosition') || 0;
		
		if(orient == 'prev')
			actualPosition -= 1;
		else if(orient == 'next')
			actualPosition += 1;
		else
			actualPosition = orient;
			
		//Unset old active
		$('.rol .pager .pages img').attr('src','/images/rol_inactive.png');
		$('.rol .pager .pages a').removeClass('active');

		var offset = this.settings.offset || 2 
		
		actualPosition = (actualPosition < 0) ? 0 : (actualPosition > numberOfPositions - offset ) ? numberOfPositions - offset : actualPosition;


		newActive = $('.rol .pager .pages [href=#'+actualPosition+']').addClass('active');
		newActive.find('img').attr('src','/images/rol_active.png');
			
		var position = -1 * move * actualPosition;
		$('.rol .images').stop().animate({left: [position,"easeOutBack"]},1000);
		$('.rol').data('actualPosition',actualPosition);
		return false;
	},
	rolInit: function(offset) {
		
		var page = 0;
		var offset = offset | 2;
		
		$('.rol .images a').each(function(e){
			//Pictures
			/*var src = $(this).attr('href');
			var title = $(this).attr('title');
			var img = $('<img>').attr('src',src).attr('alt',title);
			if($('.slideshow').find('.active').length == 0) {
				img.addClass('active');
			}
			$('.slideshow').append(img);*/
			
			//Rol pages
			title = page;
			a = $('<a href="#'+page+'" class="rol_dot"></a>')
			img = $('<img>');
			a.bind('click',function(){
				SlideSwitch.rolMove($(this).attr('href').replace('#',''));
				return false;
			});
			if ($('.rol .pager .pages .active').length == 0) {
				a.addClass('active');
				img.attr('src','/images/rol_active.png').attr('alt',title);
			} else {
				img.attr('src','/images/rol_inactive.png').attr('alt',title);
			}
			a.empty().append(img);
			if($('.rol .images a').index(this) < $('.rol .images a:lt(30)').length-offset+1)
				$('.rol .pager .pages').append(a);
			page++;
			
			
		})
	
		$('.rol .images a').bind('click',function(){SlideSwitch.rolClick(this); return false;},this);
	},
	rolClick: function(e) {
		SlideSwitch.move('next',e.href)
	},
	move: function(orient,target) {
		$(".slideshow").each(function(){
			if($(".slideshow").data("move")) return false; 
			var $slideshow = $(this)
			var $active = $slideshow.find('IMG.active');
			
			if ( $active.length == 0 ) $active = $slideshow.find('IMG:last');
			if(target) {
				var $next = $(".slideshow [src="+target+"]");
				if($next.hasClass('active')) return false;
				if($next.prevAll('.active').length != 0)
					orient = 'next';
				else
					orient = 'prev';
			} else {
				if(orient == 'next')
					var $next =  $active.next().length ? $active.next(): $slideshow.find('IMG:first');
				else
					var $next =  $active.prev().length ? $active.prev(): $slideshow.find('IMG:last');
			}
			$active.addClass('last-active');

			var duration = 1500;
			$(".slideshow").data("move",true);
			var $activeWidth = $active.width();
			var $nextWidth = $next.width();
			if(orient == 'next') {
				$active.removeClass('active').animate({left: [-$activeWidth,'easeOutBack']},duration);
				$next.css({left:$nextWidth}).addClass('active').animate({left: [0, 'easeOutBack']},duration,function() {
					$active.removeClass('active last-active');
					$(".slideshow").data("move",false);
				});
			} else {
				$active.removeClass('active').animate({left: [$activeWidth,'easeOutBack']},duration);
				$next.css({left:-$nextWidth}).addClass('active').animate({left: [0, 'easeOutBack']},duration,function() {
					$active.removeClass('active last-active');
					$(".slideshow").data("move",false);
				});
			}
		})
		this.updateCounter();
	},
	updateCounter: function() {
		$('.rol .counter').html($(".slideshow img").index($(".slideshow .active"))+1 +" / " + $(".slideshow img").length);
	}
}

NewsScroller = {
	items: {},
	pager: {},
	init: function() {
		this.pager = $('#news_box .news_pager');
		this.items = $('#news_box .news_items');		
		this.pager.find('.up').bind('click',function(){NewsScroller.up()},this);
		this.pager.find('.down').bind('click',function(){NewsScroller.down()},this);
		this.updateButtons();
	},
	up: function(){
		visibles = this.items.find('.visible');
		if(visibles.first().prevAll('.news_item:not(.disabled)').length == 0) return;
		visibles.last().slideUp().removeClass('visible');
		visibles.first().prevAll('.news_item:not(.disabled)').first().slideDown().addClass('visible');
		this.updateButtons();		
	},
	down: function(){
		visibles = this.items.find('.visible');
		if(visibles.last().nextAll('.news_item:not(.disabled)').length == 0) return;
		visibles.first().slideUp().removeClass('visible');
		visibles.last().nextAll('.news_item:not(.disabled)').first().slideDown().addClass('visible');
		this.updateButtons();
	},
	updateButtons: function(){
		visibles = this.items.find('.visible');
		if(visibles.last().nextAll('.news_item:not(.disabled)').length == 0)
			this.pager.find('.down').fadeTo(250,0.1).addClass('inactive')
		else
			this.pager.find('.down').fadeTo(250,1).removeClass('inactive')
		
		if(visibles.first().prevAll('.news_item:not(.disabled)').length == 0)
			this.pager.find('.up').fadeTo(250,0.1).addClass('inactive')
		else
			this.pager.find('.up').fadeTo(250,1).removeClass('inactive')
	},
	updateVisibles: function(catName) {
		$('#news_box .news_items .news_item:not(.'+catName+')').slideUp().removeClass('visible').addClass('disabled');
		$('#news_box .news_items .news_item.'+catName+'').removeClass('disabled');
		$('#news_box .news_items .news_item.'+catName+':lt(4)').slideDown().addClass('visible');
		this.updateButtons();
	}
}


ProjectScroller = {
	items: {},
	pager: {},
	init: function() {
		this.pager = $('#project_box .project_pager');
		this.items = $('#project_box .project_items');		
		this.pager.find('.up').bind('click',function(){ProjectScroller.up()},this);
		this.pager.find('.down').bind('click',function(){ProjectScroller.down()},this);
		this.updateButtons();
	},
	up: function(){
		visibles = this.items.find('.visible');
		if(visibles.first().prevAll('.project_item:not(.disabled)').length == 0) return;
		visibles.last().slideUp().removeClass('visible');
		visibles.first().prevAll('.project_item:not(.disabled)').first().slideDown().addClass('visible');
		this.updateButtons();		
	},
	down: function(){
		visibles = this.items.find('.visible');
		if(visibles.last().nextAll('.project_item:not(.disabled)').length == 0) return;
		visibles.first().slideUp().removeClass('visible');
		visibles.last().nextAll('.project_item:not(.disabled)').first().slideDown().addClass('visible');
		this.updateButtons();
	},
	updateButtons: function(){
		visibles = this.items.find('.visible');
		if(visibles.last().nextAll('.project_item:not(.disabled)').length == 0)
			this.pager.find('.down').fadeTo(250,0.1).addClass('inactive')
		else
			this.pager.find('.down').fadeTo(250,1).removeClass('inactive')
		
		if(visibles.first().prevAll('.project_item:not(.disabled)').length == 0)
			this.pager.find('.up').fadeTo(250,0.1).addClass('inactive')
		else
			this.pager.find('.up').fadeTo(250,1).removeClass('inactive')
	},
	updateVisibles: function(catName) {
		$('#project_box .project_items .project_item').hide().removeClass('visible').addClass('disabled');
		//$('#project_box .project_items .project_item:not(.'+catName+')').slideUp().removeClass('visible').addClass('disabled');
		$('#project_box .project_items .project_item.'+catName+'').removeClass('disabled');
		$('#project_box .project_items .project_item.'+catName+':lt(4)').show().addClass('visible');
		this.updateButtons();
	}
}
PagingFilter = {
	entries: {}, 
	categories: {}, 
	init: function() {
		this.entries = $('div#projects');
		$('.alage_category ul li').bind('click',function(){PagingFilter.filterCats($(this).index())},this);
		$('ul:#pages .page').bind('click',function(){PagingFilter.changePage($(this))},this);
	},
	filterCats: function(catIndex) {
		catName = '.cat_'+catIndex;
		visibles = this.entries.find(catName).length;
		nrPages = Math.ceil(visibles/(document.getElementById('portfolio_main') ? 12 : 9));
		this.makePages(nrPages);
		
		$('.alage_category ul li').removeClass('active');
		$('.alage_category ul').find('li:eq('+catIndex+')').addClass('active');
		$('.alage_category .dropdown_select').html($('.alage_category ul li.active a span').html());
		
		this.entries.find('.box:not('+catName+')').fadeOut('normal');
		this.entries.find('.box'+catName+'').fadeIn('normal');
		this.resizeBoxes(this.entries.find('.box'+catName));
		$('#main').clearQueue();
		this.init();
	},
	makePages: function(nrPages) {
		$('#counter').html((nrPages.toString().length == 1 ? '00' : '0')+nrPages)
		var paging = '';
		for (i = 1; i <= nrPages; i++)  {
			paging += '<li><a href="javascript:void(0);" class="page'+(i == 1 ? ' active' : '')+'"><span>'+i+'</span></a></li>\n';
		}
		$('ul:#pages').html(paging);
		$('div#projects').clearQueue().scrollTo(0, 1000, {easing:'easeOutBack'});
		if (document.getElementById('main'))
			$.scrollTo($('#main').clearQueue(),1000,{easing:'easeOutBack', offset:{top:-10}});
		$('#main').clearQueue();
	},
	changePage: function(clickedPage) {
		clicked = clickedPage.find('span');
		pageNr = clicked.html();
		pageHeight = this.entries.innerHeight();
		scrollto = (pageHeight*(pageNr-1));
		$('ul:#pages .page').parent().parent().find('.active').removeClass('active');
		clickedPage.addClass('active');
		this.entries.scrollTo(scrollto, 1000, {easing:'easeOutBack'});
		$('#main').clearQueue();
		$('#main').stop();
		if (document.getElementById('main'))
			$.scrollTo($('#main').clearQueue(),1000,{easing:'easeOutBack', offset:{top:-10}});
	},
	resizeBoxes: function(entries) {
		var x = 0;
		var y = 0;
		var boxes = new Array();
		boxes[0] = ['big','small','small','small'];
		boxes[1] = ['small','big','small','small'];
		boxes[2] = ['small','small','big','small'];
		boxes[3] = ['small','small','small','big'];
		$.each(entries,function(key,value) {
			if (x == (document.getElementById('portfolio_main') ? 4 : 3)) {
				x = 0;
				y++;
			}
			if (y == (document.getElementById('portfolio_main') ? 4 : 3))
				y = 0;
			box = boxes[y][x];
			$(this).removeClass('big').removeClass('small').addClass(box);
			x++;
		})
	}
}
