var jcObject = {
	widthScreen: function () {
		var w, h;
		w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
		h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
		if (w<960) w=960;
	//	if (h<700) h=700;
		return {w:w, h:h};
	},

	contentSize: function (){
		var content = this,
		scrW = jcObject.widthScreen().w,
		scrH = jcObject.widthScreen().h,
		contentList = $('#contentList'),
		list = $("#previewList");

		$(list).click();

		contentList.css('width', (4 * scrW));
		$('#contentList > li').css('width', (scrW - 24));

		for (var i = 0; i < 4; i++) {
			$('#contentList > li').eq(i)
			.css('left', (i * scrW) );
			$('#menu li').eq(i).attr('pos', i);
			if ((i == 0)&&($('#menu li').eq(i).attr("class")=="current")&&(window.location.hash==""))
			{
				$('#menu li').eq(i).append($('<span>').css('float','right').css('left', 0).css('backgroundPosition','top left').css("background-image", "url(../img/mspan.png)"));
				$('#menu li').eq(i).append($('<span>').css('float','right').css('left', $('.current').width()+26).css('backgroundPosition','top right').css("background-image", "url(../img/scrolls.png)"));
			}
		};
		
		$('#menu').unbind('click');
		$('#menu').bind('click', function(event){
			var _this = this,
			el = $(event.target);
			if( el.is('li') && !el.is('.current') && !el.is('#menuz')){
				$('#menu').find('.current').find('span').detach();
				$('#menu').find('.current').find('span').detach();
				$('#menu').find('.current').removeClass('current');
				el.addClass('current');

				if ($('.current').attr('pos') == $('#menu li').size()-2)
				{
					el.append($('<span>').css('float','right').css('left', $('.current').width()+26).css('backgroundPosition','bottom right').css("background-image", "url(../img/mspan.png)"));
					el.append($('<span>').css('float','right').css('left', 0).css('backgroundPosition','0px 74px').css("background-image", "url(../img/scrolls.png)"));
					$('#arrow').css('backgroundPosition','bottom right')
				}
				else
				{
					$('#arrow').css('backgroundPosition','top right')
					if ($('.current').attr('pos') != 0)
					{
						el.append($('<span>').css('float','right').css('left', 0).css('backgroundPosition','bottom left').css("background-image", "url(../img/scrolls.png)"));
						el.append($('<span>').css('float','right').css('left', $('.current').width()+26).css('backgroundPosition','top right').css("background-image", "url(../img/scrolls.png)"));
					}
					else
					{
						el.append($('<span>').css('float','right').css('left', 0).css('backgroundPosition','top left').css("background-image", "url(../img/mspan.png)"));
						el.append($('<span>').css('float','right').css('left', $('.current').width()+26).css('backgroundPosition','top right').css("background-image", "url(../img/scrolls.png)"));
					}
				}
				$(contentList).animate({
					left: (el.attr('pos') * scrW * -1)
					}, 500);
				$(contentList).animate({
					height: (
						(parseInt($('#contentList > li').eq($('.current').attr('pos')).height()) < (scrH - 181))?(scrH - 181):parseInt($('#contentList > li').eq($('.current').attr('pos')).height())
					)
				}, 300);
			}
			if (el.attr("hash")!=undefined)
				window.location.hash=el.attr("hash");
		});
		
		$("#logolist").find("img").bind("click", function(){
			$('#menu li').eq(0).click();
		});
		contentList.css('left',($('.current').attr('pos') * scrW * -1));
		contentList.css('height', (parseInt($('#contentList > li').eq($('.current').attr('pos')).height()) < (scrH - 181))?(scrH - 181):parseInt($('#contentList > li').eq($('.current').attr('pos')).height()));
	},
	projectsListSize: function(obj, filter){
		var list = $("#previewList"),
		width = jcObject.widthScreen().w - 194,
		widthItem =(width-(width%200))/200,
		count = 0;
		$("#portfolioMenu").find('.portfolioCurrent').removeClass('portfolioCurrent');
		$(obj).addClass('portfolioCurrent');
		$(list).find("li").each(function(i){
			if($(this).attr("tags").search("(.)*("+filter+")+") == 0)
			{
				$(this).css("left", (200*count));
				$(this).css("top", 0);
				count++;
			}
			else
				$(this).css("top", 250);
		});

		if (widthItem >= count) {
			widthItem = count;
			width = widthItem*200;
			$("#rightArrow").css("height", 0);
			$("#leftArrow").css("height", 0);
		}
		else{
			$("#rightArrow").css("height", 56);
			$("#leftArrow").css("height", 56);
		}


		$("#examples").css("width", (width-(width%200))+170);
		$("#preview").css("width", (width-(width%200)));

		$(list).attr("pos", widthItem);
		$(list).css("right", 0);

		if (parseInt($(list).css("right"))>count*200-widthItem*200) $("#rightArrow").css("backgroundPosition","-35px 0");
			else $("#rightArrow").css("backgroundPosition","-35px -56px")
		if (parseInt($(list).css("right"))<=widthItem*200)$("#leftArrow").css("backgroundPosition","0 0");
			else $("#leftArrow").css("backgroundPosition","0 -56px");
		$("#leftArrow").unbind("click").attr("flag",0);
		$("#leftArrow").bind("click", function(){
			if ($("#leftArrow").attr("flag")==0){
				$("#leftArrow").attr("flag", 1);
				if (parseInt($(list).css("right"))>=200){
					$(list).animate({right:
						($(list).attr("pos")-widthItem < widthItem)
							?
							0
							:
							(parseInt($(list).css("right"))-widthItem*200)

						}, 300, function(){
							$("#leftArrow").attr("flag", 0);
							$(list).attr("pos", (parseInt($(list).css("right"))+widthItem*200)/200);
						}
					);
					$("#rightArrow").css("backgroundPosition","-35px -56px");
				}
				if (parseInt($(list).css("right"))<=widthItem*200)$("#leftArrow").css("backgroundPosition","0 0");
				$("#rightArrow").attr("flag", 0);
			}
		});
		$("#rightArrow").unbind("click").attr("flag",0);
		$("#rightArrow").bind("click", function(){
			if ($("#rightArrow").attr("flag")==0){
				$("#rightArrow").attr("flag", 1);
				if (parseInt($(list).css("right"))<count*200-widthItem*200){
					$(list).animate({right:
						(count - $(list).attr("pos") < widthItem)
							?
							(parseInt($(list).css("right"))+(count - $(list).attr("pos"))*200)
							:
							(parseInt($(list).css("right"))+widthItem*200)
						}, 300, function(){
							$("#rightArrow").attr("flag", 0); $(list).attr("pos", (parseInt($(list).css("right"))+widthItem*200)/200);
						}
					);
					$("#leftArrow").css("backgroundPosition","0 -56px");
				}
				if (parseInt($(list).css("right"))>(count-1)*200-widthItem*200*2) $("#rightArrow").css("backgroundPosition","-35px 0");
				else $("#rightArrow").css("backgroundPosition","-35px -56px");
				$("#leftArrow").attr("flag", 0);
			}
		});
	},
	getContent: function(id){
		var troll=window.location.hash;
		if (troll.lastIndexOf("/")+2==troll.length)
			troll=troll.slice(0,-2);
		if (troll.lastIndexOf("/")+3==troll.length)
			troll=troll.slice(0,-3);
		if (troll.lastIndexOf("/")+1==troll.length)
		troll+=id;
		else
		troll+="/"+id;
		window.location.hash=troll;
		
		$("#loader").css("left", (jcObject.widthScreen().w/2-50+13));
		$("#loader").css("top", (jcObject.widthScreen().h/2+150));
		$("#project").css("min-height", jcObject.widthScreen().h-100);
		jcObject.contentSize();
		var imgCount = 0,
		imgLoad = 0;
		$("#project").animate({opacity: 0}, 200, function(){
			$(window).scrollTo($("#project"), 800 );
			$("#loader").animate({opacity: 1}, 200, function(){
				$("#loader").get(0).style.cssText = "";
				$("#loader").css("left", (jcObject.widthScreen().w/2-50+13));
				$("#loader").css("top", (jcObject.widthScreen().h/2+150));
			});
			$.get("ajax.php", {id: id}, function(data){				
				$("#project").html("");
				$("#project").html(data);
				$("#project img").each(function(){
					imgCount++;
					$(this).animate({opacity: 0},0);
				});
				$("#project img").each(function(){
					$(this).bind('load', function(){
						imgLoad++;
						$(this).animate({opacity: 1},200);
						if (imgCount == imgLoad){
							$("#loader").animate({opacity: 0}, 200);
							$("#project").animate({opacity: 1}, 200, function(){$("#project").get(0).style.cssText = "";});
						}
						jcObject.contentSize();
					});
				});	
				if (imgCount==0){
					$("#loader").animate({opacity: 0}, 200);
					$("#project").animate({opacity: 1}, 200, function(){$("#project").get(0).style.cssText = "";});
				}
				else{
					$("#project").animate({opacity: 0.5}, 200);
				}
				jcObject.contentSize();
			});
		});
	}

};
$(function(){
$("#previewList").attr("pos", 0);
jcObject.contentSize();
$(function(){
	$('.portfolioCurrent').click();
});


for (var i=0;i<4;i++){
	if (window.location.hash.search("("+$("#menu > li").eq(i).attr("hash")+")+")==0)
	{
		$("#contentList").css("left", i * jcObject.widthScreen().w * -1);
		$('#menu').find('.current').removeClass('current');
		$('#menu > li').eq(i).addClass('current');
		var el = $('.current');
		if ($('.current').attr('pos') == $('#menu li').size()-2)
		{
			el.append($('<span>').css('float','right').css('left', $('.current').width()+26).css('backgroundPosition','bottom right').css("background-image", "url(../img/mspan.png)"));
			el.append($('<span>').css('float','right').css('left', 0).css('backgroundPosition','0px 74px').css("background-image", "url(../img/scrolls.png)"));
			$('#arrow').css('backgroundPosition','bottom right')
		}
		else
		{
			$('#arrow').css('backgroundPosition','top right')
			if ($('.current').attr('pos') != 0)
			{
				el.append($('<span>').css('float','right').css('left', 0).css('backgroundPosition','bottom left').css("background-image", "url(../img/scrolls.png)"));
				el.append($('<span>').css('float','right').css('left', $('.current').width()+26).css('backgroundPosition','top right').css("background-image", "url(../img/scrolls.png)"));
			}
			else
			{
				el.append($('<span>').css('float','right').css('left', 0).css('backgroundPosition','top left').css("background-image", "url(../img/mspan.png)"));
				el.append($('<span>').css('float','right').css('left', $('.current').width()+26).css('backgroundPosition','top right').css("background-image", "url(../img/scrolls.png)"));
			}
		}
		var troll=window.location.hash;
				if (troll.lastIndexOf("/")+2==troll.length){
					troll=troll.slice(troll.lastIndexOf("/")+1);
					jcObject.getContent(troll);
				}
				if (troll.lastIndexOf("/")+3==troll.length){
					troll=troll.slice(troll.lastIndexOf("/")+1);
					jcObject.getContent(troll);
				}
		
		$("#portfolioMenu").find("a").each(function(){
			if (window.location.hash.search("("+$(this).attr("href")+")+")==0)
			{
				$('.portfolioCurrent').removeClass('portfolioCurrent');
				$(this).addClass('portfolioCurrent');
				$(".current").attr("hash", window.location.hash);
				$(this).click();
				var troll=window.location.hash;
				if (troll.lastIndexOf("/")+2==troll.length){
					troll=troll.slice(troll.lastIndexOf("/")+1);
					jcObject.getContent(troll);
				}
				if (troll.lastIndexOf("/")+3==troll.length){
					troll=troll.slice(troll.lastIndexOf("/")+1);
					jcObject.getContent(troll);
				}
			}
		});
	}
}

});

jQuery(window).resize(function(){

	jcObject.contentSize();
	$(function(){
		$('.portfolioCurrent').click();
		$("#loader").css("left", (jcObject.widthScreen().w/2-50+13));
		$("#loader").css("top", (jcObject.widthScreen().h/2+150));
	});
});


