/* common scripts */

var dimI = 0.3;

$(document).ready(function(){
	
	// extern links
	$("a[href^='http']").attr("target", "_blank");
	
	// position on load
	adjustWindow();
	
	// reposition on resize
	$(window).resize(function() {
		adjustWindow();
	});	
	
	$("#wrapper").css({
		display: "block"
	});
	
	// adjust css
	$("#navi ul.nav2").css({
		opacity: "0",
		display: "block",
		paddingBottom: "0"
	});	
	$("#info").css({
		opacity: "0"
	});
	$("#commissions img").css({
		opacity: "0.7"
	});
	
	// blend in firstimage
	if($("#gallery").length) {
		dimI = 0.1;
		var imageToLoad = $("#gallery img").eq(0).attr('src');		
		if(imageToLoad) {
			$('<img />').attr('src', imageToLoad).load(function(){		
				// blend in image
    			showFirst();
    		});
		} else {
			showFirst();
		}
		initGallery();
	}
	
	// blend in slideshow firstimage
	if($("#slideShow").length) {
		dimI = 0.1;
		var imageToLoad = $("#slideShow img").eq(0).attr('src');		
		if(imageToLoad) {
			$('<img />').attr('src', imageToLoad).load(function(){		
				showFirst();
    		});
		} else {
			showFirst();
		}
		$(".imgWrapper").eq(0).addClass("active");
		slideShowId = setInterval("slideShow()", 3000);
	}
	
	// security function
	window.setTimeout(function(){
		showFirst();			
	}, 2000);
	
	if($("#commissions").length) {
		dimI = 0.1;
	}
	
	// navi
	$("#navi ul.nav1 li").not("#navi ul.nav2 li").hover(
		function() {
			naviIn($(this));
		},
		function() {
			naviOut($(this));		
		}
	);	
	
	// introduction
	$("#introDuction").click(function(){
		toggleInfo();
	});
	
	// navi
	$("#commissions img").hover(
		function() {
			$(this).stop().animate({opacity: "1"},200);
		},
		function() {
			$(this).stop().animate({opacity: "0.7"},1000,'easeOutQuad');	
		}
	);
		
});


// blend in first image
function showFirst() {
	if(!$("body").hasClass("loaded")) {
		$(".imgWrapper").eq(0).fadeIn(550,'easeInQuad'); 
		$("body").addClass("loaded");
	}
}

// reposition on resize
function adjustWindow() {
	var theWin = $(window).height();
	if(theWin < 780) { 
		$("#innerWrapper").css({paddingTop: "30px"});
	} else {
		theWin = ((theWin-780)/3)+30;	
		$("#innerWrapper").css({paddingTop: theWin+"px"});
	}
}

// navi
function naviIn(li) {
	li.children("ul").css({height: "auto"});
	li.children("ul").stop().animate({opacity: "1"},500);
	if(!$("body").hasClass("infoed")) {
		$("#content").stop().animate({opacity: dimI},300);
	}
}

function naviOut(li) {
	li.children("ul").css({height: "0"});
	li.children("ul").stop().animate({opacity: "0"},300);
	$("#content").stop().animate({opacity: "1"},1000,'easeOutQuad');
}

// introduction
function toggleInfo() {
	if(!$("body").hasClass("infoed")) {
		$("body").addClass("infoed");
		$("#info").stop().css({display: "block"}).animate({opacity: "1"},500);
		$("div.active").stop().animate({opacity: "0.1"},300);
	} else {
		$("body").removeClass("infoed");
		$("#info").stop().animate({opacity: "0"},300, function(){
			$("#info").css({display: "none"});
		});
		$("div.active").stop().animate({opacity: "1"},1000,'easeOutQuad');		
	}
}

// gallery 
var imgI = 1;
function initGallery() {
	// build html
	var images = $("#gallery img").length;
	if(images <= 9) {
		images = "0"+images;
	}
	$("#pagina").html('\
	<div id="prevImg"><</div>\
	<div id="imgCount">01</div>\
	<div id="imgCount2">/'+images+'</div>\
	<div id="nextImg">></div>');
	$("#galleryInner .clear").remove();
	// init navigation
	$("#gallery .imgWrapper").eq(0).addClass("active");
	$('#nextImg,#galleryInner').click(function() {	
		nextImg("next");
	});
	$('#prevImg,#prev').click(function() {
		nextImg("prev");
	});
}

function nextImg(next) {	
	if(!$("body").hasClass("imaging")) {
		if($("body").hasClass("infoed")) {
			toggleInfo()
		}
		$("body").addClass("imaging");
    	var $active = $('#gallery div.active');
    	// determine image
    	if(next == "next") {
    		if($active.next().length) {
    			var $next = $active.next();
    			imgI++;
    			if(imgI <= 9) {
					$('#imgCount').html("0"+imgI);
				} else {
					$('#imgCount').html(imgI);
				}
    		} else {
    			var $next = $("#gallery .imgWrapper:first");
    			imgI = 1;
    			$('#imgCount').html("01");
    		}
    	} else {     		
    		if($active.prev().length) {
    			var $next = $active.prev();
    			imgI--;
    			if(imgI <= 9) {
					$('#imgCount').html("0"+imgI);
				} else {
					$('#imgCount').html(imgI);
				}
    		} else {
    			var $next = $("#gallery .imgWrapper:last");
    			imgI = $("#gallery .imgWrapper").length;    			
    			if(imgI <= 9) {
					$('#imgCount').html("0"+imgI);
				} else {
					$('#imgCount').html(imgI);
				}
    		}
    	}          
    	// fading image
   		$active.addClass('last-active');   	
   		$('.imgWrapper').not($next).stop().fadeOut(300);   	
    	$next.addClass('active')
    	 	.stop()
    	    .fadeIn(550,'easeInQuad');              
		window.setTimeout(function() {
    	    $active.removeClass('active last-active');
			$("body").removeClass("imaging");
		}, 450);   
	}
}

function slideShow() {
	var $active = $('#slideShow div.active');
	var $next =  $active.next().length ? $active.next()
        : $('#slideShowInner div:first'); 	
   	$('.imgWrapper').not($next).stop().fadeOut(500);   	
    $next.addClass('active')
     	.stop()
        .fadeIn(750,'easeInQuad'); 
    $active.removeClass('active');
}
		



