var IE = /*@cc_on!@*/false;
var IE5 = (IE && (navigator['appVersion'].indexOf('MSIE 5') > 0)) ? true : false;
var IE6 = (IE && (navigator['appVersion'].indexOf('MSIE 6') > 0)) ? true : false;
var IE7 = (IE && (navigator['appVersion'].indexOf('MSIE 7') > 0)) ? true : false;
var IE8 = (IE && (navigator['appVersion'].indexOf('MSIE 8') > 0)) ? true : false;
var IE9 = (IE && (navigator['appVersion'].indexOf('MSIE 9') > 0)) ? true : false;

function site () {
	var th = this;
	
	this.speed		= 300;
	this.colors		= ["#9C0","#639","#F33","#F60"];
	this.rockanim	= true;
	this.holoanim 	= false;
	this.page_class = $("div.all_cont").attr("id");
	this.slide		= $("div.all_cont").attr("slide") * 1;
	this.rotator	= false;
	
	this.underlineLeft;
	this.underlineWidth;
	this.underlineColor;
	this.hout;
	
	this.fadeBg = function (n) {
		var selector = "div.bg.";
		if (n == 0) {selector += "index"}else
		if (n == 1) {selector += "services"}else
		if (n == 2) {selector += "portfolio"}else
		if (n == 3) {selector += "contact"};
		$("div.bg").stop().fadeOut(th.speed);
		$(selector).stop().fadeIn(th.speed);
	}
	
	this.portfolioShow = function () {
		if (th.slide == 2) {$("div#portfolio").fadeIn(th.speed)} 
		else {$("div#portfolio").fadeOut(th.speed)}
	}
	
	this.navigationRotate = function (n) {
		$("div.navigation ul li").removeClass("active prev next title");
		$("div.navigation ul li:nth-child(" + (n+1) + ")").addClass("active prev");
		$("div.navigation ul li:nth-child(" + (n+2) + ")").addClass("title");
		$("div.navigation ul li:nth-child(" + (n+3) + ")").addClass("active next");		
	}
	
	this.setCaption = function (n) {
		var capt_b = $("div.navigation ul li.title span.caption b").html();
		var capt_s = $("div.navigation ul li.title span.caption span").html();
		$("h1.caption b").html(capt_b);
		$("h1.caption span").html(capt_s);
		$(document).attr("title", "Martin Grand : " + capt_b + " (" + capt_s + ")");
		var animearr = {"background-color" : th.colors[n], "color" : (n == 1 ? "#FFF" : "#000")};		
		$("h1.caption > b, h1.caption > span").animate(animearr, th.speed);	
	}
	
	this.getUnderlineParameters = function (n) {
		var ret = Array();
		ret["background-color"] = th.colors[n];
		ret["color"] = n == 1 ? "#FFF" : "#000";
		ret["width"] = $("div.head_navig > a:eq(" + n + ")").outerWidth();
		//alert(n+1 + ":" + $("div.head_navig > a:eq(" + (n+1) + ")").text());
		var sepW = $("div.head_navig span.separator").outerWidth();
		ret["margin-left"] = 0;
		for (i = 0; i < n; i++) {
			ret["margin-left"] += $("div.head_navig a:eq(" + (i) + ")").outerWidth();
		}
		ret["margin-left"] += n * sepW;
		return ret;
	}
	
	this.initUnderNavPosition = function () {
		$("span#underline").stop(true,true).animate(th.getUnderlineParameters(th.slide),th.speed);
		$("div.head_navig a").css("color", "#666");
		$("div.head_navig a:eq("+ th.slide +")").css("color", (th.slide == 1 ? "#FFF" : "#000"));			
	}
	
	this.navigationInit = function () {
		setTimeout(function(){
			$("span#underline").css(th.getUnderlineParameters(th.slide)).fadeIn(th.speed);
		},750);
		$("div.head_navig a[href=\"/hello\"]").hover(function(){
			$("span#underline").stop(true,true).animate(th.getUnderlineParameters(0),th.speed, "easeInOutCirc");
		});
		$("div.head_navig a[href=\"/services\"]").hover(function(){
			$("span#underline").stop(true,true).animate(th.getUnderlineParameters(1),th.speed, "easeInOutCirc");
		});
		$("div.head_navig a[href=\"/portfolio\"]").hover(function(){
			$("span#underline").stop(true,true).animate(th.getUnderlineParameters(2),th.speed, "easeInOutCirc");
		});
		$("div.head_navig a[href=\"/contact\"]").hover(function(){
			$("span#underline").stop(true,true).animate(th.getUnderlineParameters(3),th.speed, "easeInOutCirc");
		});
		$("div.head_navig a").hover(function(){
			th.hout = false;
			$("div.head_navig a").css("color", "#666");
			$(this).css("color", ($(this).attr("href") == "/services" ? "#FFF" : "#000"));
		},function(){
			th.hout = true;
			setTimeout(function(){
				if(th.hout){
					$("div.head_navig a").css("color", "#666");
					$("div.head_navig a:eq("+ th.slide +")").css("color", (th.slide == 1 ? "#FFF" : "#000"));
					$("span#underline").stop(true,true).animate(th.getUnderlineParameters(th.slide),th.speed, "easeInOutCirc");
				}
			},th.speed);
		});
	}
	
	this.slideto = function (n) {
		$("div.body").animate({
			"margin-left" : (n * -800)+"px"
		}, th.speed, "easeInOutCirc");		
		th.initUnderNavPosition();
		th.portfolioShow();		
		th.setSizes();
		th.fadeBg(n);
		th.navigationRotate(n);
		th.setCaption(n);	
		th.animateRock();
	}
	
	this.navto = function (n, href) {
		th.slide = n;
		var sel = "#page_" + (n+1);
		if($(sel).html() == "") {
			$("#loader").fadeIn(th.speed);
			$.get(href, function(data){
				var content = $(sel, $(data));
				$(sel).html(content.html());
				$("#loader").stop().fadeOut(th.speed/2);
				th.pageLoaded();
				th.slideto(n);				
			}); 
		}else{
			th.slideto(n);
		}			
	}
	this.pageLoaded = function() {
		th.mailProtector();
		if(th.slide == 2 && !th.rotator) {
			th.rotator = new pageRotator();	
		}
	}
	this.navFadeAttach = function () {
		var fade = 0.85;
		$("div.navigation").fadeTo(th.speed, fade);
		$("div.navigation").hover(function(){
				$("div.navigation").stop(true,true).fadeTo(th.speed, 1);		
			},function(){
				$("div.navigation").stop(true,true).fadeTo(th.speed, fade);			
		});
	}
	this.captionHoverAttach = function () {
		$("h1.caption").hover(function(){
				var params = {
					"background-image" : "url('" + ROOT + "a/app/i/wn.gif')",
					"background-position" : " 50% 50%",
					"color" : "#FFF"
				}
				$(this).children("b").css(params);
				$(this).children("span").css(params);		
			},function(){
				var params = {
					"background-image" : "none",
					"color" : (th.slide == 1 ? "#FFF" : "#000")
				}
				$(this).children("b").css(params);
				$(this).children("span").css(params);			
		});		
	}
	this.attachNavig = function () {
		$("a.navig").click(function(){
			window.location.hash = $(this).attr("href");
			return false;
		});
		$("a.navig.main").click(function(){
			th.navto(0, $(this).attr("href"));
		});
		$("a.navig.services").click(function(){
			th.navto(1, $(this).attr("href"));
		});	
		$("a.navig.portfolio").click(function(){
			th.navto(2, $(this).attr("href"));
		});
		$("a.navig.contact").click(function(){
			th.navto(3, $(this).attr("href"));
		});
	}
	this.attaches = function () {
		th.attachNavig();
		th.navFadeAttach();
		th.captionHoverAttach();
		setTimeout(function(){th.setSizes();},750);
		$(window).resize(function(){th.setSizes()})		
	}
	this.setSizes = function () {
		$("div.body-handler").css("height", $("div.body > div.page:nth-child(" + (th.slide+1) + ")").outerHeight());
		$("div.background").css({"height": 0});
		if(th.slide != 2) {
			$("div.background").css({"height": $(document).height() - (IE8 ? 9 : 0)});
		}else{
			$("div.background").css({"height": "100%"});
			if(IE7) {
				$("div#portfolio").css({"height": $(window).height()});	
				$("div#portfolio").css({"width": $(window).width()});
			}
		}
		if($(window).width() <= 1152) {
			if($(window).width() >= 1014){
				$("body").removeClass("mini-doc");	
				$("body").addClass("small-doc");
			}else{	
				$("body").addClass("small-doc");
				$("body").addClass("mini-doc");
			}
		}else{
			$("body").removeClass("small-doc");	
			$("body").removeClass("mini-doc");			
		}
	}
	this.animateRock = function () {
		$("#white_noise .monitor_sym .rock_text").fadeTo(
			Math.floor(
				Math.random() * 101
			), (
				Math.random() / 3 + 0.2
			), function(){
				if(!th.slide) {
					th.animateRock();	
				}
			}
		);	
	}
	this.mailProtector = function () {
		$(".m").each(function(){
			if($(this).attr("href") !== undefined){
				$(this).attr("href", $(this).attr("href").replace("{m}", "@martingrand.com"));
				$(this).attr("href", $(this).attr("href").replace("%7Bm%7D", "@martingrand.com"));
			}
			$(this).html($(this).html().replace("{m}", "@martingrand.com"));
		});
	}
	// konsturktor:
	th.navto(th.slide, false);
	th.attaches();
	th.pageLoaded();
	th.navigationInit();
	th.animateRock();
}

function pageRotator(){	
	var th = this;
	
	// properties:
	this.speed =			500;
	this.ease = 			"easeOutExpo";	
	this.lrOpacity =		0.6;
	this.backOpacity =		0.4;
	
	// selectors:
	this.rotatorContainer = "div#portfolio  ul";
	this.rotA = 			"div#portfolio  ul li.center";
	this.rotB = 			"div#portfolio  ul li.left";
	this.rotC = 			"div#portfolio  ul li.right";
	this.rotD = 			"div#portfolio  ul li.back";
	
	// inner vars:
	this.plength =			0;
	this.sildeing = 		0;	
	this.actHelpVar;
	this.actCenter = 		th.rotA;
	this.actLeft = 			th.rotB;
	this.actRight = 		th.rotC;
	this.actBack = 			th.rotD;
	this.t = 				0;
	this.wait =				0;
	this.echo =				100;	
	this.scrollbarWidth =	0;
	
	this.animateTo = function (w, to, n){
		th.sildeing++;
		th.setDelay (n, w);
		var paramWidth = 		$(to).css ("width");
		var paramHeight = 		$(to).css ("height");
		var paramLeft = 		$(to).css('left').substr(-1, 1) == "%" ? parseFloat($(to).css('left')) : (Math.round( 100 * (parseFloat($(to).css('left')) / parseFloat($(to).parent().css('width'))) * 0.2 ) * 5);
		if (paramLeft < 25) {
			paramLeft = 5+"%";
		}else if (paramLeft < 75){
			paramLeft = 50+"%";			
		}else{
			paramLeft = 95+"%";			
		}
		var paramMarginTop = 	$(to).css ("margin-top");
		var paramMarginLeft = 	$(to).css ("margin-left");
		var opacity =			$(to).css ("opacity");
		var paramZIndex = 		$(to).css ("z-index");
		var classname =			$(to).attr("class");
		$(w).attr("ic", classname);
		th.timer(th.wait + th.t, function() {
			$(w).addClass("move");
			$(w).animate({
				width:			paramWidth,
				height:			paramHeight,
				marginLeft:		paramMarginLeft,
				marginTop:		paramMarginTop,
				left:			paramLeft,
				opacity: 		opacity
			}, th.speed, "easeInOutCirc", function () {
				th.sildeing--;	
			});
		});
		th.timer(th.wait + th.speed/2, function() {
			$(w).css("zIndex", paramZIndex);
		});		
	}
	
	this.setDelay = function (n, w) {
		if (w == th.actCenter)	{th.t = 3*th.echo} else
		if (w == th.actBack)	{th.t = 1*th.echo} ;
		if (n == "l") {
			if (w == th.actLeft)	{th.t = 0} else
			if (w == th.actRight)	{th.t = 2*th.echo} ;
		} else {
			if (w == th.actRight)	{th.t = 0} else
			if (w == th.actLeft)	{th.t = 2*th.echo} ;		
		}
	}
	
	this.chagePosVarsRight = function (){		
		var actPid = $(th.actCenter).attr("pid")*1;
		th.actHelpVar = "li[pid=\"" + $(th.actCenter).attr("pid") + "\"]";
		th.actCenter = "li[pid=\"" + $(th.actLeft).attr("pid") + "\"]";
		th.actLeft = "li[pid=\"" + (actPid < 3 ? (actPid == 2 ? th.plength : th.plength-1) : actPid - 2 ) + "\"]";
		th.actBack =  "li[pid=\"" + $(th.actRight).attr("pid") + "\"]";
		th.actRight =  "li[pid=\"" + $(th.actHelpVar).attr("pid") + "\"]";	
	}
	this.chagePosVarsLeft = function (){		
		var actPid = $(th.actCenter).attr("pid")*1;
		th.actHelpVar = "li[pid=\"" + $(th.actCenter).attr("pid") + "\"]";
		th.actCenter = "li[pid=\"" + $(th.actRight).attr("pid") + "\"]";
		th.actRight =  "li[pid=\"" + (actPid + 1 >= th.plength ? (actPid == th.plength ? 2 : 1) : actPid + 2 ) + "\"]";
		th.actBack =  "li[pid=\"" + $(th.actLeft).attr("pid") + "\"]";
		th.actLeft = "li[pid=\"" + $(th.actHelpVar).attr("pid") + "\"]";
	}
	
	this.timer = function (time, funct) {
		setTimeout(funct, time);
	}
	this.rotAfter = function () {
		th.timer (4*th.echo, function (){				
			$("div#portfolio ul > li.move").each(function(){
				$(this).attr("class", $(this).attr("ic"));
			});
		})		
	}
	this.animateRight = function () {
		if (th.sildeing == 0){
			th.chagePosVarsRight();
			th.animateTo (th.actCenter,	th.actRight, "r");
			th.animateTo (th.actRight,	th.actBack, "r");
			th.animateTo (th.actBack,	th.actLeft, "r");
			th.animateTo (th.actLeft,	th.actCenter, "r");
			th.rotAfter();
		}
	}
	this.animateLeft = function () {
		if (th.sildeing == 0){
			th.chagePosVarsLeft();
			th.animateTo (th.actCenter,	th.actLeft, "l");
			th.animateTo (th.actLeft,	th.actBack, "l");
			th.animateTo (th.actBack,	th.actRight, "l");
			th.animateTo (th.actRight,	th.actCenter, "l");
			th.rotAfter();
		}
	}
	this.init = function () {
		$("div#portfolio ul > li").each(function(){
			$(this).attr("pid", ++th.plength);
		});
		$("div#portfolio a#navig_back").click(function(){
			th.animateLeft();
			var thisnav = $(this);
			thisnav.css("display", "none");
			setTimeout(function(){thisnav.css("display", "block")}, th.speed);
		});
		/*
		$("div#portfolio a#navig_back").hover(function(){
			$("div#portfolio ul > li").each(function(){
				if ($(this).attr("pid") != $(th.actRight).attr("pid")) {
					$(this).children("img").stop(true,true).fadeTo(th.speed, 0.6);
					$(this).css("background", "none");
				}
			});
		}, function(){
			$("div#portfolio ul > li").each(function(){
				$(this).children("img").stop(true,true).fadeTo(th.speed, 1);
				setTimeout(function(){$(this).css("background", "#FFF")}, th.speed);
			});
		});
		*/
		$("div#portfolio a#navig_next").click(function(){
			th.animateRight();
			var thisnav = $(this);
			thisnav.css("display", "none");
			setTimeout(function(){thisnav.css("display", "block")}, th.speed);
		});
		/*
		$("div#portfolio a#navig_next").hover(function(){
			$("div#portfolio ul > li").each(function(){
				if ($(this).attr("pid") != $(th.actLeft).attr("pid")) {
					$(this).children("img").stop(true,true).fadeTo(th.speed, 0.6);
					$(this).css("background", "none");
				}
			});
		}, function(){
			$("div#portfolio ul > li").each(function(){
				$(this).children("img").stop(true,true).fadeTo(th.speed, 1);
				setTimeout(function(){$(this).css("background", "#FFF")}, th.speed);
			});
		});
		*/
		$("div#portfolio ul li").fadeTo(1,0);
		$("div#portfolio ul li.center").fadeTo(1,1);
		$("div#portfolio ul li.left, div#portfolio ul li.right").fadeTo(1,th.lrOpacity);
		$(document).keypress(function(e){
			if (e.keyCode == 39) {
				th.animateLeft();
			}
			if (e.keyCode == 37) {
				th.animateRight();
			}
		});
	}
	th.init();

}

$(document).ready(function(){
	var ws = new site();
});
