/* EBv2 JS by EB */
$(document).ready(function() {
	// start variables
	var indexNum = 9;
	var activeMenu = 9;
	var activeClass = "home";
	var blockNum = 0;
	var menu = ["Recent","Works","About","Contact"];
	var submenu = ["Web","Photos","Articles"];
	var menuPos = [-9, 117, 243, 369];
	var pageType = "none";
	var subStatus = 0;
	var currClass = "none";
	var photoCount = 0;
	var photoWidth = 0;
	var photoHeight = 0;
	var currPhoto = 0;
	var nextPhoto = 0;
	var scrollPoint = 0;
	var workingFlag = 0;
	// functions
	function showBlocks(count) {
		if(blockNum<count) {
			$("#body .block:eq("+blockNum+")").fadeIn(500,function() {
				blockNum++;
				showBlocks(count);
			});
		} else {
			blockNum = 0;
		}
	}
	function showMenu(index,section) {
		var currPos=3;
		for(i=3; i>=0; i--) {
			if(index!=i) {
				if(section=="main") { $("#menu li#item"+(i+1)+" a.mainitem").css({"margin-left":menuPos[currPos]+"px","color":"#928671","padding-left":"18px"}); }
				else if(section=="sub") { $("#menu li#item2-"+(i+1)+" a.subitem").css({"margin-left":menuPos[currPos+1]+"px","color":"#AC9E87","padding-left":"18px"}); }
				else {  }
				currPos--;
			}
			if(i===0) { $("#menu ul li."+section+"menu:not(:eq("+index+")) a."+section+"item").fadeIn(800); }
		}
		if(section=="main" && index==1) {
			currPos=3;
			for(i=3; i>=0; i--) {
				$("#menu li#item2-"+(i+1)+" a.subitem").css({"margin-left":menuPos[currPos+1]+"px","color":"#AC9E87","padding-left":"18px"});
				currPos--;
			}
			$("#submenubar").slideDown(function() {
				$("#menu ul li ul").fadeIn(function() {
					$("#menu ul li ul li a").fadeIn();
					subStatus=1;
				});
			});
		}
	}
	function showContent(index,section) {
		if(section=="main") { newActiveClass = menu[index].toLowerCase(); }
		else if(section=="sub") { newActiveClass = submenu[index].toLowerCase(); }
		else { newActiveClass=section; }
		$("#body").load("/"+newActiveClass+"?ajax=1", function(data) {
			$("#loading").hide();
			if(section=="main" || section=="sub") {
				currClass = $("#body").attr("class");
				$("#body").removeClass(currClass).addClass(newActiveClass);
			}
			$("#body .block").hide();
			activeClass = newActiveClass;
			$("#body").fadeIn(function() {
				var blockCount = $("#body .block").size();
				showBlocks(blockCount);
				if($(".gallery").length) { updateGalleryInfo(); }
				else { showMenu(index,section); }
				if(!$.browser.msie) {
					Cufon.replace('h2, h3, #menu ul li a, #galleries h4, #glist li a', { hover: true });
				}
			});
		});
		activeMenu = index;
	}
	function getImagesInfo() {
		  photoWidth = $("#gallery .first img").width();
		  photoHeight = $("#gallery .first img").height();
		  $(".session, #session").css({"width":photoWidth+"px","height":photoHeight+"px","margin-top":"18px"});
		  if(photoHeight>photoWidth) { $(".session").css({"margin-left":"122px"}); }
		  $("#session").fadeIn("fast");
		  $("#gallery").css({"margin-top":"-"+(photoHeight+18)+"px"});
		  $(".session a").css({"margin-top":(photoHeight/2-27)+"px"});
	}
	function updateGalleryInfo() {
		var arrPageSizes = ___getPageSize();
		photoCount = $("#gallery li").size();
		//$("#test").append("foto sayisi:"+photoCount);
		$(".previous, .next").fadeOut("fast");
		if($("#gallery .first img").width()===0) {
			$("#gallery .first img").load(function () {
				getImagesInfo();
			});
		} else {
			getImagesInfo();
		}
		if($.browser.opera && $.browser.version > "9.5") {
			$("#glist li.active").css({"margin-left":"94px"});
		}
		$('#layer').css({
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]+244
		});
		if(arrPageSizes[3]>774) {
			scrollPoint = ($("#gallery").offset().top)-((arrPageSizes[3]-774)/2);
		} else {
			scrollPoint = $("#gallery").offset().top;
		}
		//$("#test").append("resimwidth:"+photoWidth+" yuk"+photoHeight+" pagehe"+arrPageSizes[1]+" windowhe"+arrPageSizes[3]);
	}
	function endSession() {
		$("#layer").fadeOut();
		$(".next, .previous").fadeOut(function() {
			$(".nonext").removeClass("nonext");
		});
		$(".gallery ul li.first a").removeClass("animated");
		$(".gallery ul li:eq("+currPhoto+")").fadeOut(function() {
			$(".gallery ul li:eq(0)").fadeIn(function(){
				$(".session").fadeIn(function() {
					currPhoto=0;
					workingFlag=0;
				});
			});
		});
	}
	function ___getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	}
	function checkEmail(email){
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(email)) {
			return true;
		}
		return false;
	}
	jQuery.preloadImages = function() {
	  for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	  }
	};
	// after load actions
	$("body").append('<div id="loading">Loading...</div><div id="layer"><a href="#"></a></div>');
	if($(".gallery").length) { updateGalleryInfo(); }
	$.preloadImages("http://www.emrebuyukozkan.com/img/previous.gif", "http://www.emrebuyukozkan.com/img/next.gif","http://www.emrebuyukozkan.com/img/shadow.png","http://www.emrebuyukozkan.com/img/layer.png");
	var menuTop = $("#menubar").offset().top;
	// interactions
	$("#menu ul li a").click(function(e) { if(!$.browser.msie) {
		e.preventDefault();
		if($(this).hasClass("mainitem")===true) { pageType = "main"; }
		else { pageType = "sub"; }
		var indexNum = $("li."+pageType+"menu").index($(this).parent());
		var $sibs = $(this).parent().siblings().find("a");
		$sibs.fadeOut(function() { 
			if ( $sibs.index(this) == $sibs.length-1 ) {
				$("#loading").show();
				if(pageType=="main") {
					$("h1").fadeOut("fast");
					$("#menu li.mainmenu a.activated").removeClass("activated");
				}
				else {
					$("h2").fadeOut("fast");
					$("#menu li#item2 a.mainitem").css({"color":"#928671"});
					$("#menu li.submenu a.activated").removeClass("activated");
				}
				$("#menu li a.active").removeClass("active");
				$("#body").fadeOut("slow");
				if(pageType=="main" && !(indexNum==1)) {
					$("#menu ul li ul li a").fadeOut(function() {
						$("#menu ul li ul").slideUp();
						$("#submenubar").slideUp();
						subStatus=0;
					});
				}
				$("#menu li."+pageType+"menu:eq("+indexNum+") a."+pageType+"item").animate( { "marginLeft":"-369px", "paddingLeft":"0px", "color":"#fff"}, 1000, function() {
					$(this).addClass("active");																																					
					if($("#menubar").hasClass("started")===false) {
						$("#menubar").css({"top":menuTop+"px"}).animate({"top":"63px"}, 800, function() {
							showContent(indexNum,pageType);
							$("#menubar").addClass("started");
						});
					} else {
						showContent(indexNum,pageType);
					}
				});						
			} 
		});
	} });
	$(".webleft a").live("mouseover", function() {
			var thisParent = $(this).parent();
		    thisParent.parent().find(".active").removeClass("active");
			$(this).addClass("active");
			var imgIndex = thisParent.parent().find('li').index(thisParent);
			thisParent.parent().parent().find("img.shown").removeClass("shown");
			thisParent.parent().parent().find('img:eq('+imgIndex+')').addClass("shown");
	});
	$("#galleries li, #glist li").live("mouseover", function() {
		$(this).addClass("hover");
	});
	$("#galleries li, #glist li").live("mouseout", function() {
		$(this).removeClass("hover");
	});
	$(".webleft ul li a, #galleries li a, #glist li a, .gallery ul li a, #worksphoto a").live("click", function(e) {
		  if(!$.browser.msie) { e.preventDefault(); }
	});
	$("#galleries li, #glist li, #worksphoto li").live("click", function(e) {
		if(!$.browser.msie) {
			$("#body").fadeOut("slow");
			var showgallery = $(this).find("a").attr("rel");
			showContent(0,"works/photos/"+showgallery);
		}
	});
	$(".internal").live("click", function(e) {
		if(!$.browser.msie) {
			e.preventDefault();
			var newpage= $(this).attr("rel");
			$("#body").fadeOut("slow");
			showContent(0,newpage);
		}
	});
	$(".session a").live("click", function(e) {
		e.preventDefault();
		$("#layer").fadeIn(function() {
			$('html, body').animate({
				//scrollTop: $("#gallery").offset().top
				scrollTop: scrollPoint
			}, 1500, function() {
				$(".session").fadeOut(500, function() {
					$(".next").fadeIn();
					$(".gallery ul li.first a").addClass("animated");				
				});
			});	
		});
	});
	$("#layer a").live("click", function(e) {
		e.preventDefault();
		endSession();
	});
	$(".previous, .next").live("click", function(e) {
		e.preventDefault();
		if(workingFlag===0) {
			workingFlag=1;
			var operation=$(this).attr("title");
			if(currPhoto===0) { $(".previous").fadeIn(); }
			$(".gallery ul li:eq("+currPhoto+")").fadeOut(function() {
				$(".nonext").removeClass("nonext");
				if(operation=="next") {
					if(currPhoto+1==photoCount) {
						endSession();
					} else {
						$(".gallery ul li:eq("+(currPhoto+1)+")").fadeIn(function() {
							currPhoto++;
							if(currPhoto+1==photoCount) { $(".next").addClass("nonext"); }
							workingFlag=0;
						});	
					}
				} else {
					if(currPhoto===0) {
						endSession();
					} else {
						$(".gallery ul li:eq("+(currPhoto-1)+")").fadeIn(function() {
							currPhoto--;
							if(currPhoto===0) { $(".previous").addClass("nonext"); }
							workingFlag=0;
						});
					}
				}
			});
		}
	});
	$("#contactform").submit(function(e) {
		e.preventDefault();
	});
	$("#contactform .submit").live("click", function(e) {
		e.preventDefault();
		inputmail = $("#email").val();
		if(checkEmail(inputmail)===true) {
			$("#contactresult:visible, #contactform").slideUp(function() {
				$("#contactresult").html("Please wait... ").slideDown();
			});
			var formValues=$("#contactform").serialize();
			$.ajax({
				type: "POST",
				url: "/includes/actions.php",
				data: "action=sendEmail&"+formValues,
				success: function(result) {
					$("#contactform").slideUp(function() {
						$("#contactresult").append(result);
					});
				}
			});
		} else {
			$("#contactresult").html("Check your email.").slideDown();
		}
	});
	if(!$.browser.msie) {
		Cufon.replace('h1, h2, h3, #menu ul li a, #galleries h4, #glist li a', { hover: true });
	}
	// diagonistics
	//$("p").append("Test");
});