;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

//preloading slide background
var bkgImg = new Image();
bkgImg.src = ($.browser.msie && $.browser.version < 7) ? 'images/ie6/selected.gif' : 'images/selected.png';



$(document).ready(function(){

	

	//=======================================================================================================
	// cookie management functions
	//=======================================================================================================
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	function eraseCookie(name) {
		createCookie(name,"",-1);
	}

	//=======================================================================================================
	// text settings
	//=======================================================================================================

	//hight contrast
	var hc = parseInt(readCookie("cookieHC"));
	if(hc == 1) $("*").not("a").css({fontWeight: 700, color: '#000'});

	$(".hc").click(function(){
		if(!hc) $("*").not("a").css({fontWeight: 700, color: '#000'});
		else $("*").not("a").css({fontWeight: "", color: ""});
		if(hc == 0) hc = 1; else hc = 0;
		createCookie("cookieHC", hc, 60);
		return false;
	});


	//getting font size from a cookie and setting it
	var fontSize = parseInt(readCookie("cookieFontSize"));
	if(!fontSize) fontSize = 0;

	$("body").css("font-size", "");
	$("body").css("font-size", parseInt(parseInt($("body").css("font-size")) + fontSize));

	//font size increase/decrease buttons
	$(".font-inc").click(function(){
		if(fontSize < 3) {
			fontSize++;
			createCookie("cookieFontSize", fontSize, 60);
			$("body").css("font-size", "");
			$("body").css("font-size", parseInt(parseInt($("body").css("font-size")) + fontSize));
		}
		return false;
	});
	$(".font-dec").click(function(){
		if(fontSize > -3) {
			fontSize--;
			createCookie("cookieFontSize", fontSize, 60);
			$("body").css("font-size", "");
			$("body").css("font-size", parseInt(parseInt($("body").css("font-size")) + fontSize));
		}
		return false;
	});

	//=======================================================================================================
	// projects slider
	//=======================================================================================================

	//slider init
	var itemWidth = 130;
	var slideCount = 6;
	var currentPos = 0;
	var listWidth = $(".slider li").length * itemWidth;
	var jumpLength = slideCount * itemWidth;
	var rest = ($(".slider li").length % slideCount) * itemWidth;
	if(rest == 0) rest = jumpLength;
	var minX = jumpLength - listWidth;
	if(listWidth <= jumpLength) minX = 0;
	else if(minX > 0 - rest) minX = 0 - rest;
	$(".slider ul").css("width", listWidth + 31 + "px");

	//slider item element states
	var currentItem = 0;
	var animSpeed = 300;
	var liSelected = {top: -6, left: 0, width: 161, height: 86, marginLeft: -3, marginRight: -15};
	var liNormal = {top: 0, width: 137, height: 75, marginLeft: 0, marginRight: -7};
	var liSelectedA = {width: 161, height: 86, lineHeight: 86};
	var liNormalA = {width: 137, height: 75, lineHeight: 75};
	var liSelectedAImg = {marginTop: 8, width: 145, height: 71};
	var liNormalAImg = {marginTop: 7, marginLeft: 6, width: 125, height: 61};


	//moving the slider to and fro
	//--------------------------------------------------------------------
	$(".slide-forward").click(function(){
		if(currentPos > minX + jumpLength) currentPos = currentPos - jumpLength;
		else if(currentPos >= minX + rest) currentPos = currentPos - rest;
		else currentPos = minX;
		$(".slider ul").animate({left: currentPos}, 1000, "swing");
		return false;
	});
	$(".slide-back").click(function(){
		if(currentPos < 0 - jumpLength) currentPos = currentPos + jumpLength;
		else if(currentPos >= 0 - rest && currentPos < 0) currentPos = currentPos + rest;
		else currentPos = 0;
		$(".slider ul").animate({left: currentPos}, 1000, "swing");
		return false;
	});

	//hiding unwanted images
	//--------------------------------------------------------------------
	$(".presentation img:gt(0)").hide();
	$(".project-descr li:gt(0)").hide();


	//this happens when a slider link is clicked:
	//--------------------------------------------------------------------
	$(".slider a").click(function(){
		var oldItem = currentItem;
		currentItem = $(this).attr("rel");
		if($(this).attr("rel") == oldItem) return false;
		$($(".presentation img").get(currentItem)).show();
		$($(".project-descr li").get(currentItem)).show();
		$($(".presentation img").get(oldItem)).hide();
		$($(".project-descr li").get(oldItem)).hide();
		return false;
	});

	$(".slider li").addClass("selected").prepend('<img src="images/' + ($.browser.msie && $.browser.version < 7 ? 'ie6/selected.gif' : 'selected.png') + '" alt="" class="back">');

	$(".slider a img").mouseover(function(){
		$(this).parent().parent().css({ zIndex: 100 }).animate(liSelected, animSpeed);
		$(this).parent().animate(liSelectedA, animSpeed);
		$(this).animate(liSelectedAImg, animSpeed);
	});
	$(".slider a img").mouseout(function(){
		$(this).parent().parent().animate(liNormal, animSpeed, function() { $(this).css({ zIndex: 1 }); });
		$(this).parent().animate(liNormalA, animSpeed);
		$(this).animate(liNormalAImg, animSpeed);
	});

	//=======================================================================================================
	// modal window with projects inside
	//=======================================================================================================

	// function that changes pictures in the modal window and its size & position
	//--------------------------------------------------------------------
	function changePic(oldPic, newPic, w, h){
		var resizeSpeed = 1000;
		$(".loader").show();
		oldPic.hide();
		$(".prev, .next").hide();
		var margin = 0 - (w + 50)/2;
		if($.browser.msie) {
			$(".modal").animate({marginLeft: margin, width: w+50, height: h}, resizeSpeed);
			$(".modal-top").animate({width: w, height: h}, resizeSpeed);
			$(".modal-bottom").animate({width: w+50, height: h}, resizeSpeed);
			if($.browser.version < 7) {
				var bottomW = $(".modal-bottom-top").width();
				$(".modal-bottom-bottom").animate({width: w + 6, marginLeft: 25});
			}
		} else $(".modal").animate({marginLeft: margin}, resizeSpeed);
		$(".modal-w").animate({height: h}, resizeSpeed);
		$(".modal-e").animate({height: h}, resizeSpeed);
		$(".modal-img-wrapper").animate({width: w, height: h}, resizeSpeed, function(){$(".loader").hide(); newPic.fadeIn(); if($.browser.msie) $(".prev, .next").show(); else $(".prev, .next").fadeIn();});

		$(".modal-bottom-middle").children("ul").children("li").children("a").removeClass("current");
		$($(".modal-bottom-middle").children("ul").children("li").children("a").get(currentPic)).addClass("current");
		
		$(".modal-bottom-middle p").css("width", newPic.width() - $(".img-switch").width() + 100);
		var pH = $(".modal-bottom-middle p").height();
		var ulH = $(".modal-bottom-middle ul").height();
		var bottomH = 0;
		if(pH > ulH) bottomH = pH; else bottomH = ulH;
		$(".modal-bottom-e, .modal-bottom-w, .modal-bottom-middle").animate({height: bottomH});
		
	}
	
	//function that prevents modal window layout from breaking when the picture's too big
	//-------------------------------------------------------------------------------------
	var minBodyW = 0;
	var w = 0;
	for(i = 0; i < $(".project-images img").length; i++){
		w = $($(".project-images img").get(i)).attr("width");
		if(minBodyW < w) minBodyW = w;
	}
	minBodyW = minBodyW + 300;
	
	function bodySize(){
		var bodyW = $(window).width();
		if(bodyW < minBodyW) {
			$("body").css("width", minBodyW + "px");
			$(".overlay").css("width", minBodyW + "px");
			//if(!$.browser.msie)
			$.scrollTo({left: '50%', top: '0'});
		} else bodyReset();
	}
	
	function bodyReset(){
		$("body").css("width", "auto");
		$.scrollTo(0, 0);
		$(".overlay").css("width", "100%");
	}

	//function opening the modal window
	//--------------------------------------------------------------------
	var currentPic = 0;
	function showModal(index){
		bodySize();
		$(".modal-img-wrapper").children("img").remove();

		//putting content from html to the modal window
		$(".modal-img-wrapper").prepend($($(".project-images").get(index)).html());
		$(".modal img").not(".default").hide();
		$(".modal-bottom p").html($($(".project-description").get(index)).html());
		var imgCount = $(".modal-img-wrapper img").length;
		if(imgCount > 1){
			$(".modal-bottom-middle").prepend("<ul class='img-switch'></ul>");
			for(i = 0; i < imgCount; i++){
				var num = i + 1;
				if(i == 0) $(".modal-bottom-middle").children("ul").append('<li><a href="#" rel="' + i + '" class="current">' + num + '</a></li>');
				else $(".modal-bottom-middle").children("ul").append('<li><a href="#" rel="' + i + '">' + num + '</a></li>');
			}
		} else $("ul.img-switch")

		//showing the modal window
		if($.browser.msie) {
			$(".overlay").css("height", $("body").height());
			$(".overlay, .modal").show();
		} else $(".overlay, .modal").fadeIn();

		//getting the default picture and setting modal window size and position
		currentPic = 0;
		var newPic = $($(".modal-img-wrapper").children("img").get(currentPic));
		var w = $($(".modal-img-wrapper").children("img").get(currentPic)).width() + 120;
		var h = $($(".modal-img-wrapper").children("img").get(currentPic)).height();
		changePic(newPic, newPic, w, h);

		//what happens when a screenshot number is clicked
		$(".img-switch a").click(function(){
			$(".img-switch a").removeClass("current");
			$(this).addClass("current");
			old = currentPic;
			currentPic = $(this).attr("rel");
			var oldPic = $($(".modal-img-wrapper").children("img").get(old));
			var newPic = $($(".modal-img-wrapper").children("img").get(currentPic));
			var w = $($(".modal-img-wrapper").children("img").get(currentPic)).width() + 120;
			var h = $($(".modal-img-wrapper").children("img").get(currentPic)).height();

			changePic(oldPic, newPic, w, h);
			return false;
		});
	}

	//closing the modal window
	//--------------------------------------------------------------------
	$(".modal a.close").click(function(){
		//clearing stuff
		currentPic = 0;
		$(".modal-img-wrapper").children("img").remove();
		$(".modal-bottom").children("p").html("");
		$(".modal-bottom-middle").children("ul").remove();

		//hiding the modal
		if($.browser.msie) {
			$(".overlay").hide();
			$(this).parent().hide();
		} else {
			$(".overlay").fadeOut();
			$(this).parent().fadeOut();
		}
		bodyReset();
		return false;
	});

	// the "zoom" button opens the modal window
	//--------------------------------------------------------------------
	$("a.zoom, .presentation img").click(function() { showModal(currentItem); return false; });



	// show previous screenshot
	//--------------------------------------------------------------------
	$(".modal a.prev").click(function(){
		var old = currentPic;
		if(currentPic == 0) currentPic = $(".modal-img-wrapper").children("img").length - 1;
		else currentPic--;

		var oldPic = $($(".modal-img-wrapper").children("img").get(old));
		var newPic = $($(".modal-img-wrapper").children("img").get(currentPic));
		var w = $($(".modal-img-wrapper").children("img").get(currentPic)).width() + 120;
		var h = $($(".modal-img-wrapper").children("img").get(currentPic)).height();

		changePic(oldPic, newPic, w, h);
		return false;
	});

	// show next screenshot
	//--------------------------------------------------------------------
	$(".modal a.next").click(function(){
		var old = currentPic;
		if(currentPic == $(".modal-img-wrapper").children("img").length - 1) currentPic = 0;
		else currentPic++;

		var oldPic = $($(".modal-img-wrapper").children("img").get(old));
		var newPic = $($(".modal-img-wrapper").children("img").get(currentPic));
		var w = $($(".modal-img-wrapper").children("img").get(currentPic)).width() + 120;
		var h = $($(".modal-img-wrapper").children("img").get(currentPic)).height();

		changePic(oldPic, newPic, w, h);
		return false;
	});

	// show particular screenshot - accordingly to the number clicked
	//--------------------------------------------------------------------
	$(".img-switch a").click(function(){
		$(".img-switch a").removeClass("current");
		$(this).addClass("current");
		old = currentPic;
		currentPic = $(this).attr("rel");
		var oldPic = $($(".modal-img-wrapper").children("img").get(old));
		var newPic = $($(".modal-img-wrapper").children("img").get(currentPic));
		var w = $($(".modal-img-wrapper").children("img").get(currentPic)).width() + 120;
		var h = $($(".modal-img-wrapper").children("img").get(currentPic)).height();

		changePic(oldPic, newPic, w, h);
		return false;
	});

//$(window).resize(bodySize);

});


