// $Id$
currentProject = 0;
currentSlide = 0;
projectChanged = false;
changeTitle = true;

$(document).ready(function() {
	resizeWindow();

	$("#bottom").hover(
		//function () { $("div.bottom_navigation").animate({"margin": "0px 0 0 187px"}, 400, function () { $("div.bottom_navigation div.caption").removeClass('caption_up'); });
		//$("div.bottom_navigation a").fadeTo(1000,1); },
		//function () { $("div.bottom_navigation").animate({"margin": "92px 0 0 187px"}, 400, function () { $("div.bottom_navigation div.caption").addClass('caption_up'); });
		//$("div.news_navigation div.bottom_navigation").animate({"margin": "100px 0 0 187px"}, 400);
		//$("div.bottom_navigation a").fadeTo(100,0); }
	);

	// populate the project names on navigation
	if (next.length > 0) {
		$("a.next_navigation").html(next[currentProject+1]);
	} else {
		$("a.next_navigation").html("Next client");
	}
	$("a.bottom_navigation").html("");
	//$("a.bottom_navigation").html("More " + client[currentProject]);
	//$(".caption").html("<b>Above: </b>" + captions[currentProject][currentSlide]);
});
	
function showProject(pos, duration, changeTitle) {
	currentSlide = 0;
	currentProject = pos;
	
	var slidesInProject = $("#prvi_nivo div.drugi_nivo:eq(" + currentProject + ") li.second").length;
	$("p.page_info").html("Pages: 1 of " + slidesInProject);

	$("#loader").removeClass("show");
	$("#prvi_nivo").scrollTo( 'li.first:eq(' + currentProject + ')', {duration:duration, onAfter: function(){$("#prvi_nivo div").scrollTo('li:eq(0)');}});

	if (changeTitle) {
	$("div.absolute_heading").fadeTo(100,0).delay(1000,function(){
		$("div.absolute_heading").html("<span class='orange'>&amp;</span> " + (typeof(title) == 'undefined' ? client[currentProject] : title[currentProject]));
		//$("div.absolute_heading").fadeTo("slow",1);
		$("div.absolute_heading").fadeTo(10,1);
	});
	}

	$("a.top_navigation").addClass('none');
	$("a.prev_navigation").removeClass('none');
	if($("#prvi_nivo div.drugi_nivo:eq(" + currentProject + ") li.second:eq(1)").length > 0) {
		$('a.bottom_navigation').removeClass('none');
	} else {
		$('a.bottom_navigation').addClass('none');
	}
	
		$("a.prev_navigation").html(prev[currentProject-1]);
		$("a.next_navigation").html(next[currentProject+1]);
	$("a.bottom_navigation").html("");
	//$("a.bottom_navigation").html("More " + client[currentProject]);
	//$(".caption").html(captions[currentProject][currentSlide]);

	if(currentProject == 0) $('a.prev_navigation').addClass('none');
	else $('a.prev_navigation').removeClass('none');
	
	if(currentProject + 1 == $("#prvi_nivo ul.project_list li.first").length) $('a.next_navigation').addClass('none');
	else $('a.next_navigation').removeClass('none');

	if($('#main_menu li li.selected a').html() != '- Studio' && $('#main_menu li li.selected a').html() != '- Articles' && $('#main_menu li.selected a').html() != 'About') {
		$('#main_menu li li.selected').removeClass('selected');
		var sel = $('#main_menu li li:eq(' + currentProject + ')').addClass('selected');
		sel.parent('ul').addClass('hold');
		if(!sel.is(':visible')) {
			sel.parent('ul').show('slow', function(){});
		}
		$('#main_menu ul:not(.hold)').hide('slow');
		$('#main_menu ul:not(:first)').removeClass('hold');
	}
}

function changeProject(inc, absolute, changeTitle) {
	pos = absolute ? inc : currentProject + inc;
	var slide = $("#prvi_nivo li.first:eq(" + pos + ") li.second:eq(0)");
	if(!slide.hasClass('loaded')) {
		slide.addClass("loaded");
		var image = $("#prvi_nivo li.first:eq(" + pos + ") li.second:eq(0) img");
		$("#loader").addClass("show");
		image.attr('src', image.attr('rel'));
		image.load(function() { showProject(pos, absolute ? 0 : 700, changeTitle); } );
	} else {
		showProject(pos, absolute ? 0 : 700, changeTitle);
	}
}

jQuery(function( $ ){
	$('a.prev_navigation').click(function(){
		changeProject(-1, 0, true);
	});

	$('a.next_navigation').click(function(){
		changeProject(1, 0, true);
	});

	$('a.bottom_navigation').click(moveDown = function(){
		var slide = $("div.drugi_nivo:eq(" + currentProject  + ") li.second:eq(" + (currentSlide + 1) + ")");
		var slidesInProject = $("#prvi_nivo div.drugi_nivo:eq(" + currentProject + ") li.second").length;
		//$(".caption").html(captions[currentProject][currentSlide+1]);
		$("p.page_info").html("Pages: " + (currentSlide+2) + " of " + slidesInProject);
		if(!slide.hasClass('loaded')) {
			slide.addClass("loaded");
			var image = $("div.drugi_nivo:eq(" + currentProject  + ") li.second:eq(" + (currentSlide + 1) + ") img");
			$("#loader").addClass("show");
			image.attr('src', image.attr('rel'));
			image.load(function() {
				$("#loader").removeClass("show");
				$("div.drugi_nivo:eq("+ currentProject +")").scrollTo("li:eq(" + (currentSlide + 1) + ")", { duration:700 });
				if(++currentSlide + 1 == $("#prvi_nivo div.drugi_nivo:eq(" + currentProject + ") li.second").length) {
					$('a.bottom_navigation').addClass('none');
				} else {
					$('a.bottom_navigation').removeClass('none');
					$("a.top_navigation").removeClass('none');
				}
			});
		} else {
			$("div.drugi_nivo:eq("+ currentProject +")").scrollTo("li:eq(" + (currentSlide + 1) + ")", { duration:700 });
			if(++currentSlide + 1 == $("#prvi_nivo div.drugi_nivo:eq(" + currentProject + ") li.second").length) {
				$('a.bottom_navigation').addClass('none');
			} else {
				$('a.bottom_navigation').removeClass('none');
				$("a.top_navigation").removeClass('none');
			}
		}
	});

	$('a.top_navigation').click(moveUp = function(){
		var slidesInProject = $("#prvi_nivo div.drugi_nivo:eq(" + currentProject + ") li.second").length;
		$("p.page_info").html("Pages: " + (currentSlide) + " of " + slidesInProject);
		//$(".caption").html(captions[currentProject][currentSlide-1]);
		$("div.drugi_nivo:eq(" + currentProject + ")").scrollTo("li:eq(" + (currentSlide - 1) + ")", { duration:700 });
		if(--currentSlide == 0) {
			$('a.top_navigation').addClass('none');
		} else {
			$('a.top_navigation').removeClass('none');
			$("a.bottom_navigation").removeClass('none');
		}
	});
});

$(window).bind("resize", resizeWindow);

function resizeWindow( e ) {


        //Gather browser dimensions
        var browserwidth = $(window).width() - 187;
        var browserheight = $(window).height();
		normalHeight = 1112;
		normalWidth = 1733;
		minWidth = 400;
		minHeight = (normalHeight * minWidth) / normalWidth;
		
		$("li.first").width(browserwidth).height(browserheight);
		$("ul.project_list_detail").width(browserwidth).height(browserheight);
		$("li.second").width(browserwidth).height(browserheight);
		
        if (browserwidth > browserheight) { 
            $("li.second img").not('.no-resize').width(browserwidth);
			 $("li.second img").not('.no-resize').height((normalHeight * browserwidth)/normalWidth);
			if (browserwidth < minWidth) {  $("li.second img").not('.no-resize').width(minWidth*2).height(minHeight*2);  }
			if (browserheight < minHeight) {  $("li.second img").not('.no-resize').width(minWidth*2).height(minHeight*2);  }

			if (((normalHeight * browserwidth)/normalWidth) <  browserheight) {
				 $("li.second img").not('.no-resize').width((normalWidth * browserheight) / normalHeight);
				 $("li.second img").not('.no-resize').height(browserheight);
				if (browserwidth < minWidth) {  $("li.second img").not('.no-resize').width(minWidth*2).height(minHeight*2);  }
				if (browserheight < minHeight) {  $("li.second img").not('.no-resize').width(minWidth*2).height(minHeight*2);  }
			}
        } else { // browserwidth < browserheight
				 $("li.second img").not('.no-resize').width((normalWidth * browserheight) / normalHeight);
				 $("li.second img").not('.no-resize').height(browserheight);
				if (browserwidth < minWidth) {  $("li.second img").not('.no-resize').width(minWidth*2).height(minHeight*2);  }
				if (browserheight < minHeight) {  $("li.second img").not('.no-resize').width(minWidth*2).height(minHeight*2);  }
		}

		//Make sure the image stays center in the window
         $("li.second img").not('.no-resize').css('margin-left', ((browserwidth - $("li.second img").width())/2));
         $("li.second img").not('.no-resize').css('margin-top', ((browserheight - $("li.second img").height())/2));
		
		// resize other elements
		$("a.middle").css('left', (browserwidth-165)/2 + 175 + 86);
		$("p.middle").css('left', (browserwidth-165)/2 + 175 + 86 - 40);
		if (nav == "projects") {
			$("a.prev_navigation").css('left', (browserwidth-165)/2 + 175 + 86 - 220);
			$("a.next_navigation").css('right', (browserwidth-165)/2 + 175 - 67 - 220);
		}
		$("li.first").width(browserwidth).height(browserheight);
		$("ul.project_list_detail").width(browserwidth).height(browserheight);
		$("li.second").width(browserwidth).height(browserheight);
	
	$("#prvi_nivo").scrollTo( 'li.first:eq(' + currentProject + ')');
	$("div.drugi_nivo:eq(" + currentProject + ")").scrollTo("li:eq(" + currentSlide + ")");

};


jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	return this;
}


//if(window.addEventListener) document.addEventListener('DOMMouseScroll', moveObject, false);
//document.onmousewheel = moveObject;
//function moveObject(event) {
//	var delta = 0;
//	if(!event) event = window.event;
//	if(event.wheelDelta) {
//		delta = event.wheelDelta / 120;
//	} else if(event.detail) {
//		delta = -event.detail / 3;
//	}
//	if(delta == 1) {
//		if(currentSlide > 0) moveUp();
//	} else if(delta == -1) {
//		if(currentSlide < $("#prvi_nivo div.drugi_nivo:eq(" + currentProject + ") li").length - 1) moveDown();
//	}
//}
