/* remove error messages IE 7 or greater */
function blockError(){return true}
window.onerror = blockError;

/* variables */
var screenW, screenH, ie6, ie6, wrap, wrapControl, win, bgInterval, bgIntervalTime, bgTotal, bgImage, navBtn, navIndex, sizeMe , navInterval, navIntervalTime, navSliderType, slideMe, details, backBtn, fullScreen, fullScreenBtn, fullScreenCloseBtn, preview, previewTop;
	
function init(){
	
	ie6					= (navigator.appVersion.indexOf("MSIE 6.") == -1) ? false : true;
	ie7					= (navigator.appVersion.indexOf("MSIE 7.") == -1) ? false : true;
	navIndex			= wrapControl = sizeMe = previewTop = previewLeft = fullScreen = 0;
	bgImage				= 1;
	bgTotal				= 2;
	bgIntervalTime		= 10000;
	navIntervalTime		= 8000;
	wrap				= $("#wrap");
	win					= $(window);
	navBtn				= $("#nav .tabs a");
	navSliderType		= "pause";
	slideMe				= $("#slideMe");
	details				= $("#details");
	backBtn				= $(".backBtn");
	fullScreenBtn		= $("#fullScreen");
	fullScreenCloseBtn	= $("#fullScreenClose");
	preview				= $("#preview");
	
}init();

/* onload functions */
$(document).ready(function(){ init();
	
	/* window resize */
	win.resize(function(){
		
		screenW = win.width();
		screenH = win.height();
		
		/* skeleton */
		if(fullScreen == 0){
			
			wrapControl = wrap.height();
			sizeMe = ((screenH - wrapControl) / 2);
			if(screenH < 684){sizeMe = 0;}
			wrap.stop().delay(200).animate({"top" : sizeMe + "px"} , 1200, "easeInOutExpo");
			
		}else{
			
			sizeMe = ((screenH - wrapControl) / 2);
			wrap.css({"top" : "0px"});
			details.css({"height" : (screenH - 312) + "px"});
			
		}
		
		/* image preview */
		if(preview.css("display") == "block"){
		
			previewTop = ((screenH - $(".image img" , preview).height()) / 2);
			if(previewTop <= 20){
				
				previewTop = 20;
				$(".image img" , preview).css({"margin-bottom" : "20px"});
				
			}else{
				$(".image img" , preview).css({"margin-bottom" : "0px"});
			}	
			
			$(".image" , preview).css({"height" : screenH + "px"});
			$(".image img" , preview).css({"margin-top" : previewTop + "px"});
		
		}
		
	})
	.trigger("resize");
	
	/* intervals started */
	bgInterval = setInterval("bgImages()" , bgIntervalTime);
	navInterval = setInterval("navIntervalFnc()" , navIntervalTime);
		
});

/* form elements */
$(".form input:not('.button'), .form textarea:not('.button')").live("focus" , function(){
	
	$(".focused" , $(this).parents(".form")).removeClass("focused");
	$(this).addClass("focused");
	
});

$("#mesajGonder").live("click" , function(){
	
	clearInterval(bgInterval);
	
	return false
	
});

/* background images */
function bgImages(){
	
	clearInterval(bgInterval);
	bgImage += 1;
	if(bgImage > bgTotal){bgImage = 1;}
	
	$("body").prepend('<img class="bgImage bi' + bgImage + ' hidden" src="img/bgImages/' + bgImage + '.jpg" />');
	$(".bi" + bgImage + "").load(function(){
		
		$(".bgImage").css({"z-index" : "0"});
		$(this).css({"opacity" : "0.0" , "display" : "inline" , "z-index" : "5"}).stop().animate({"opacity" : "1.0"} , 8000 , "easeInOutExpo" , function(){
		
			$(".bgImage:gt(0)").remove();
			bgInterval = setInterval("bgImages()" , bgIntervalTime);
			
		});
		
	});
	
}

/* navigation */
navBtn.live("click" , function(){
	
	var thisBtn = navBtn.index(this);
	navIndex = thisBtn;
	
	clearInterval(navInterval);
	navBtn.removeClass("selected");
	$(this).addClass("selected");
	
	if(slideMe.height() == 0){
		
		backBtn.css({"display" : "none"});
		fullScreenBtn.css({"display" : "none"});
		
		if(fullScreen == 0){navBtnFNC();}else{
			
			fullScreen = 0;
		fullScreenCloseBtn.hide();
		
		wrap.stop().animate({"top" : sizeMe + "px"} , 600 , "easeOutExpo");
		details.stop().animate({"height" : "372px"} , 600 , "easeOutExpo" , function(){navBtnFNC();});
			
		}
			
	}else{
			
		$("#navSlider").stop().animate({"margin-left" : ((thisBtn * 200) + 76) + "px"} , 600 , "easeOutExpo");
		slideMe.stop().animate({"margin-left" : parseInt(thisBtn * -1020) + "px"} , 1200 , "easeOutBack" , function(){
			
			if(navSliderType == "pause"){
				navInterval = setInterval("navIntervalFnc()" , navIntervalTime);
			}
			
		});
		
	}
	
	return false
	
});

function navBtnFNC(){
	
	wrap.stop().animate({"margin-top" : "186px"} , 600 , "easeOutExpo");
	details.stop().animate({"height" : "0px"} , 600 , "easeOutExpo" , function(){
	
		slideMe.stop().animate({"height" : "372px"} , 1000 , "easeOutExpo");
		wrap.stop().animate({"margin-top" : "0px"} , 1000 , "easeOutExpo" , function(){
			
			$("#navSlider").delay(50).stop().animate({"margin-left" : ((navIndex * 200) + 76) + "px"} , 600 , "easeOutExpo");
			slideMe.stop().animate({"margin-left" : parseInt(navIndex * -1020) + "px"} , 1200 , "easeOutBack" , function(){
				
				if(navSliderType == "pause"){
					navInterval = setInterval("navIntervalFnc()" , navIntervalTime);
				}
				
			});
			
		});
		
	});
		
}


function navIntervalFnc(){
	
	navIndex += 1;
	if(navIndex >= 5){navIndex = 0;}
	
	navBtn.eq(navIndex).trigger("click");
	
}

$("#slider .bttn").live("click" , function(){
	
	navSliderType = $(this).attr("rel")
	
	$("#slider .bttn").hide();
	$("#slider ." + navSliderType + "").show().css({"display" : "inline-block"});
	
	clearInterval(navInterval);
	if(navSliderType == "pause"){
		navInterval = setInterval("navIntervalFnc()" , navIntervalTime);
	}
	
	return false
	
});

$("#slider .arrow").live("click" , function(){
	
	if($(this).attr("rel") == "right"){
	
		navIndex += 1;
		if(navIndex >= 5){navIndex = 0;}
		
	}else{
	
		navIndex -= 1;
		if(navIndex < 0){navIndex = 4;}
	
	}
	
	navBtn.eq(navIndex).trigger("click");
	
	return false
	
});

/* paging */
$(".paging a").live("click" , function(){
	
	var dotts = $("a" , $(this).parent());
	
	dotts.removeClass("selected");
	$(this).addClass("selected");
	
	$(".pushMe" , $(this).parents(".slides")).eq(0).stop().animate({"margin-top" : (dotts.index(this) * -311) + "px"} , 800 , "easeOutBack");
	
	return false
	
});

/* detail pages */
$(".detailBtn:not('.blogBtn')").live("click" , function(){
	
	var target 	= $(this).attr("href"),
		vars 	= $(this).attr("rel");
	
	clearInterval(navInterval);
	details.addClass("pageLoader").empty();
	
	wrap.stop().animate({"margin-top" : "186px"} , 600 , "easeOutExpo");
	slideMe.stop().animate({"height" : "0px"} , 600 , "easeOutExpo" , function(){
	
		details.stop().animate({"height" : "392px"} , 1000 , "easeOutExpo");
		wrap.stop().animate({"margin-top" : "0px"} , 1000 , "easeOutExpo" , function(){
			
			backBtn.css({"display" : "inline-block"});
			fullScreenBtn.css({"display" : "block"});
			
			$.ajax({
				
				type : "POST",
				url : target, 
				data : "id=" + vars,
				success : function(results){
					details.removeClass("pageLoader").html(results);
				}
				
			});
			
		});
		
	});
	
	return false
	
});

backBtn.live("click" , function(){
	
	backBtn.css({"display" : "none"});
	fullScreenBtn.css({"display" : "none"});
	
	if(navSliderType == "pause"){
		navInterval = setInterval("navIntervalFnc()" , navIntervalTime);
	}
	
	if(fullScreen == 0){backBtnFNC();}
	else{
	
		fullScreen = 0;
		fullScreenCloseBtn.hide();
		
		wrap.stop().animate({"top" : sizeMe + "px"} , 600 , "easeOutExpo");
		details.stop().animate({"height" : "372px"} , 600 , "easeOutExpo" , function(){backBtnFNC();});
	
	}
	
	return false
	
});

function backBtnFNC(){
	
	wrap.stop().animate({"margin-top" : "186px"} , 600 , "easeOutExpo");
	details.stop().animate({"height" : "0px"} , 600 , "easeOutExpo" , function(){
	
		slideMe.stop().animate({"height" : "372px"} , 1000 , "easeOutExpo");
		wrap.stop().animate({"margin-top" : "0px"} , 1000 , "easeOutExpo");
		
		$(".paging" , details).show();
		$(".pushArea, .pushMe" , details).removeClass("slideLimiter");
		
	});
	  
}

/* image preview */
$(".borderImg:not('.linkImg')").live("click" , function(){
	
	$("body").css({"overflow" : "hidden"});
	clearInterval(navInterval);
	
	var imgl = $(this).attr("href");
	
	$(".image img" , preview).remove();
	wrap.fadeOut(1200 , "easeInOutExpo");
	preview.fadeIn(1200 , "easeInOutExpo" , function(){
		$(".image" , preview).css({"height" : screenH + "px"}).prepend('<img class="hidden" src="' + imgl + '" />').find("img").load(function(){
			
			previewTop = ((screenH - $(".image img" , preview).height()) / 2);
			if(previewTop <= 20){
				
				previewTop = 20;
				$(".image img" , preview).css({"margin-bottom" : "20px"});
				
			}else{
				$(".image img" , preview).css({"margin-bottom" : "0px"});
			}
			
			$(this).css({"margin-top" : previewTop + "px"}).fadeIn(1200 , "easeInOutExpo" , function(){
				$(".close" , preview).css({"display" : "block"});
			});
			
		
		});
	});
	
	return false
	
});

/* image preview */
$(".close" , preview).live("click" , function(){
	
	preview.fadeOut(1200 , "easeInOutExpo");
	wrap.fadeIn(1200 , "easeInOutExpo");
	$(".image img" , preview).fadeOut(1200 , "easeInOutExpo" , function(){
	
		$(this).remove();
		$(".close" , preview).css({"display" : "none"});
		$("body").css({"overflow" : "visible"});

		if(navSliderType == "pause"){
			navInterval = setInterval("navIntervalFnc()" , navIntervalTime);
		}
	
	});
	
	return false
	
});

fullScreenBtn.live("click" , function(){
	
	fullScreen = 1;
	$(this).hide();
	fullScreenCloseBtn.show();
	
	$(".paging" , details).hide().find("a").removeClass("selected").eq(0).addClass("selected");
	$(".pushArea, .pushMe" , details).addClass("slideLimiter");
	
	wrap.stop().animate({"top" : "0px"} , 600 , "easeOutExpo");
	details.stop().animate({"height" : (screenH - 312) + "px"} , 600 , "easeOutExpo" , function(){
		
		$(".pushArea, .pushMe").delay(100).stop().animate({"margin-top" : "0px"} , 800 , "easeOutBack");
		
	});
	
	return false
	
});

fullScreenCloseBtn.live("click" , function(){
	
	fullScreen = 0;
	$(this).hide();
	fullScreenBtn.show();
	
	$(".paging" , details).show();
	$(".pushArea, .pushMe" , details).removeClass("slideLimiter");
	
	wrap.stop().animate({"top" : sizeMe + "px"} , 600 , "easeOutExpo");
	details.stop().animate({"height" : "392px"} , 600 , "easeOutExpo");
	
	return false
	
});

