// JavaScript Document

var winHeight;
var winWidth;
var homePicturesSize;
var sitedir;
var themedir;
var activePage;
var topHeight;
var bottomHeight;

var winWidthCenter;
var winWidthCenterr;

var mouseX;
var mouseY;

var barPictures = Array();
var barPicturesId = Array();

var isBig = function (value, equal){
	var value;
	var equal;
	if (value>equal) return true; else return false;
}

function changeIt(){
	activePictures = new Array();
	activePictures = barPictures;
	activePicturesId = new Array();
	activePicturesId = barPicturesId;
}

var cShowed = false;
var contactShow = function (){
	if (cShowed == false){
		$("#contact INPUT[type=text]").val("");
		$("#contact TEXTAREA").val("");
		$("#contact").css("top", winHeight-bottomHeight-221);
		$("#contact").css("right", 300);
		$("#contact FORM").css("left", 30);
		$("#contact").slideDown();
		cShowed = true;
	} else {
		$("#contact").slideUp();
		cShowed = false;
	}
}

var panelState='expanded';

var panelAction = function (top, b1){
	var top;
	var b1;
	var b2;
	var b1Height = 74;
	var b2Height = 80;
	topHeight = 0;
	
	if (b1=='+'){
		bottomHeight = b1Height + b2Height;
		$(".blackPanel").fadeTo("slow", 1);
		$("#furkanbigPicture").fadeTo("slow", 1);
		//$("#furkanrightpicture").fadeTo("slow", 1);
		panelState = 'expanded';
	} else {
		bottomHeight = b2Height;
		$(".blackPanel").fadeTo("slow", 0);
		$("#furkanbigPicture").fadeTo("slow", 0);
		//$("#furkanrightpicture").fadeTo("slow", 0);
		panelState = 'collapsed';
	}
	
	
	$("#center").animate( { 'top': topHeight });
	$("#center").animate(	{	'height': (winHeight - (topHeight + bottomHeight)) });
	
}

var newsToUp = function (){
	var fark = $("#content").height() - $("#newscontent").height();
	var to = (0 - fark);
	$("#content").stop();
	$("#content").animate( { 'top': to }, 2000);
}

var newsToDown = function (){
	var to = (0);
	$("#content").stop();
	$("#content").animate( { 'top': to }, 2000);
}

var unload = function (to){
	
}

var resizeWindow = function (){
	topHeight = 0;
	winWidth = $(window).width();
	winHeight = $(window).height();
	var centerY = bottomHeight + topHeight;
	
	
	
	winWidthCenter = ($(window).width() - 200) / 2;
	winWidthCenterr = $(window).width() / 2;
	
	$('body').css("overflow", 'hidden');
	$(".center").css( 'height', (winHeight-centerY)+'px');
	$(".center").css( 'top', '55px');
	$(".center").css( 'left', '0px');
	
	$(".blackPanel .right_button").stop();
	$(".blackPanel .right_button").animate({ 'left': winWidth-50});
	
	$("#thumbnailPreview").css( 'top', (winHeight-118-140)+'px');
	$(".background").css( 'width', winWidth+'px');
	$(".background").css( 'height', winHeight+'px');	
	
	$(".bottomPanel").stop();
	$(".center").stop();
	$("#furkanbigPicture").stop();
	$(".bottomPanel").animate( { 'width': winWidth});
	$(".center").animate({ 'width': winWidth});
	$("#furkanbigPicture").animate({ 'left': winWidth-170, 'top': winHeight-154-232});
	
	homePicturesSize = winWidth -120;
	$("#homePictures").stop();
	$("#homePictures").animate( { 'width': homePicturesSize});
	
	var theTop = ((winHeight-bottomHeight+43) / 2);
	$("#slideLeft").animate( { 'top': theTop });
	$("#slideRight").animate( { 'top': theTop });
	
	$("#pictureSlide").css( { 'width': winWidth, 'height': winHeight-80 });
	$("#pictureSlide #org").attr("width", winWidth);
	$("#pictureSlide #org").attr("height", winHeight-80);
	$("#siteLoading").css("width", winWidth);
	$("#siteLoading").css("height", winHeight);
}

var loadPicture = function (id){
	var url;
	for (var i = 0; i<=activePicturesId.length-1; i++){
		if (activePicturesId[i]==id){
			url = activePictures[i];
			backIndex = activePicturesId[i-1];
			nextIndex = activePicturesId[i+1];
		}
	}
	var hX = winHeight-(80);
	$("#pictureSlide #org").attr("src", sitedir+"/components/resizePhoto/image.php?pic="+url+"&w="+winWidth+"&h="+hX);
}

var loadAnyArticle = function(val){
	var val;
	ajaxPost(sitedir+'/article?a=article&getRow=true&onlyAction=true&cid='+ajaxHash+'&id='+val, 'content', '');
}

var sObject;
var showHint = function (obj, message){
	
	$(".prebar #pre_text").html(message);
	
	$(".prebar").stop();
	$(".prebar").css("z-index", 20000);
	$(".prebar").fadeTo("normal", 0.7);
	$(".prebar").css( { 'left': mouseX+20, 'top': mouseY} );
}
var hideHint = function (){
	$(".prebar").stop();
	$(".prebar").fadeOut("fast");
}

var changeCard = function (id){
	var id;
	var nLeft = $("#furkan"+id).position().left;
	var zindex = $("#furkan"+id).css("z-index");
	$("#furkan"+id).animate( { left: nLeft+400 }, "slow", function (){ $(this).css("z-index", zindex-5); $("#furkan"+id).animate( { left: nLeft }, "fast"); });
}

$(document).ready (function (){
	
	winWidth = $(window).width();
	winHeight = $(window).height();
	bottomHeight = $(".bottomPanel").height();
	topHeight = $(".topPanel").height();
	
	$(document).mousemove(function (e){
		mouseX = e.pageX;
		mouseY = e.pageY;
	});
	
	resizeWindow();
	
	$("#siteLoading").css("background-color", "#000");
	$("#siteLoading").css("z-index", "40000");
	
	$(".bottomPanel").fadeIn('slow');
	
	$(".background").fadeTo('slow', 1);
	
	$("#barToleft").mouseenter(function(){ lastBarToleft(); });
	$("#barToright").mouseenter(function(){ lastBarToright(); });
	$("#barToleft").mouseout(function(){ $("#thumbNails").stop(); });
	$("#barToright").mouseout(function(){ $("#thumbNails").stop(); });
	$(".tmb").mouseenter(function (){ thumbShow(this); })
	$(".tmb").mouseout(function (){ thumbHide(this); })
	$("#thumbnailPreview").mouseenter(function (){ clearInterval(closeInterval); });
	$(".center").fadeTo('slow', 1);
});
var exFile;
function changeBg(file){
	var file;
	if (file==exFile) return false;
	exFile = file;
	$('.background').css('background-position', 'middle center');
	$(".background").fadeTo("slow", 0.05, function (){ 
	$('.background').css('background-image', 'url('+themedir+'/images/'+file+')'); setTimeout(function (){ $(".background").fadeTo("slow", 1);}, 500); } );
}

function hideSlide(){
	$('#pictureSlide').stop();
	$('#pictureSlide').fadeTo('slow', 0, function (){ $(this).hide(); $('#pictureSlide #org').attr('src', ''); });
}

function loadHome(){
	activaPage = 'home';
	changeBg('bg.jpg');
	hideSlide();
	$('.background').css('background-position', 'center bottom');
	panelAction('+', '+');
	if (wimpyStatus != 'Playing' && u_Selected == false){
			$("img#play").click();
	}
	
	if (cShowed == true)
		contactShow();
	ajaxPost(sitedir+'/home?a=load&onlyAction=true&cid='+ajaxHash, 'center', '');
}

function loadAbout(){
	activaPage = 'about';
	panelAction('+', '+');
	hideSlide();
	changeBg('bg.jpg');
	$('.background').css('background-position', 'center bottom');
	if (wimpyStatus != 'Playing' && u_Selected == false){
			$("img#play").click();
	}	
	if (cShowed == true)
		contactShow();
	setTimeout(function () {
	ajaxPost(sitedir+'/about?a=load&onlyAction=true&cid='+ajaxHash, 'center', '');
	}, 1000);
}

function loadPortfolio(){
	activaPage = 'portfolio';
	hideSlide();
	changeBg('bgPort.jpg');
	$('.background').css('background-position', 'center bottom');
	if (wimpyStatus != 'Playing' && u_Selected == false){
			$("img#play").click();
	}	if (cShowed == true)
		contactShow();
	panelAction('+', '-');
	ajaxPost(sitedir+'/portfolio?a=cats&onlyAction=true&cid='+ajaxHash, 'center', '');
}

function loadVideo(){
	activaPage = 'video';
	changeBg('bg.jpg');
	hideSlide();
	$('.background').css('background-position', 'center bottom');
	panelAction ('+', '+');
	if (cShowed == true)
		contactShow();
	setTimeout(function () {
		ajaxPost(sitedir+'/video?a=load&onlyAction=true&cid='+ajaxHash, 'center', '');
	}, 1000);
}

function loadGBook(){
	activaPage = 'gbook';
	changeBg('bg.jpg');
	hideSlide();
	$('.background').css('background-position', 'center bottom');
	panelAction('+', '+');
	if (wimpyStatus != 'Playing' && u_Selected == false){
			$("img#play").click();
	}	
	if (cShowed == true)
		contactShow();
	setTimeout(function () {
		ajaxPost(sitedir+'/gbook?a=load&onlyAction=true&pg=0&cid='+ajaxHash, 'center', '');
	}, 1000);
}

function loadArticle(){
	activaPage = 'article';
	$('.background').css('background-position', 'center bottom');
	changeBg('bg.jpg');
	hideSlide();
	if (wimpyStatus != 'Playing' && u_Selected == false){
			$("img#play").click();
	}
		if (cShowed == true)
		contactShow();
	panelAction('+', '+');
	ajaxPost(sitedir+'/article?a=load&onlyAction=true&cid='+ajaxHash, 'center', '');
}

var u_Selected = false;
function userSelected(){
	u_Selected = true;
}

var scrollMax
var ratio
var scrollSize
var scrollInterval

function calcScroll(maxs, size){
	ratio = maxs / size;
}

var getBrowser = function (){
	return navigator.appName;
}

function scrollToContent(y, target){
	var y;
	if (0-(y*ratio)<0){
		var pos = y * ratio;
		if (pos<20){
			clearInterval (scrollInterval);
			$("#"+target).stop();
			$("#"+target).animate({ top:0 }, 1000);
			return false;
		}
		clearInterval (scrollInterval);
		scrollInterval = setInterval(function (){
			$("#"+target).stop();
			$("#"+target).animate({ top: (0-(y*ratio)) }, "slow");
		}, 25);
	}
	
}

var catToLeft = function (){
	$("#cats").animate({ 'left': (0-$("#cats").width()+$("#cat").width()) }, "slow", function (){  setTimeout(function (){ catToRight(); }, 1000); });
}

var catToRight = function (){
    	$("#cats").animate({ 'left': (0) }, "slow", function (){ setTimeout(function (){ catToLeft(); }, 1000); });
}

var lastBarToleft = function (){
	var barWidth = $("#homePictures").width();
	var thmWidth = $("#thumbNails").width();
	
	var lft = 0;
	var dura = thmWidth - barWidth - $("#thumbNails").position().left;
	$("#thumbNails").css('position', 'absolute');
	$("#thumbNails").animate ( { 'left': 0}, dura*10);
	
	
}

var activePictures = Array();
var activePicturesId = Array();
var activeIndex;
var backIndex;
var nextIndex;

var lastBarToright = function (){
	var barWidth = $("#homePictures").width();
	var thmWidth = $("#thumbNails").width();
	
	var lft = barWidth - thmWidth;
	var dura = (thmWidth - barWidth) - $("#thumbNails").position().left;
	$("#thumbNails").css('position', 'absolute');
	$("#thumbNails").animate ( { 'left': lft},  dura*10);
}

var thumb_Opened;
var thumb_Opening;
var thumb_Closing;
var thumb_Fading;
var thumb_Moving;

var loadInterval;
var closeInterval;
var showInterval;

var findthumbPos = function (obj){
	if (panelState=="expanded"){
		var lft = $(obj).offset().left;
		$("#thumbnailPreview").animate({ 'left': lft-45 }, 500, function (){ thumb_Moving = false; });
	}
}

var thumbFade = function (to){
	if (panelState=="collapsed" && $(this).attr('className')=="tmb") return false;
	var to;
	if (!thumb_Opening || !thumb_Closing){
		if (to==0){
			thumb_Closing = true;
			$("#thumbnailPreview").hide();
			thumb_Closing = false;
			thumb_Opened = false;
			thumb_Opening = false;
		} else {
			thumb_Opening = true;
			$("#thumbnailPreview").show();
			
			thumb_Closing=false;
			thumb_Opened = true;
			thumb_Opening = false;
		}
	}
		//$("#thumbnailPreview").fadeTo('fast', to, function (){ if (to==0) thumb_Opened=false; else thumb_Opened=true; thumb_Closing=false; thumb_Opening=false; } );
}

var tracks = new Array('upload/others/track1.mp3', 'upload/others/track2.mp3', 'upload/others/track3.mp3', 'upload/others/track4.mp3');
var tID=0;
var nextTrack = function (){
	tID++;
	if (tID==3) tID=0;
	changeTrack(tID);
}
var changeTrack = function (id){
	var id;
	$("#pause").click();
	document.getElementById('play').onclick = function (){
		wimpyButtonPlay(sitedir+'/'+tracks[id]);
	};
	$("#play").click();
}

var thumbShow = function (obj){
	if (panelState=="collapsed") return false;
	
	clearInterval(closeInterval);
	clearInterval(loadInterval);
	var url = sitedir+'/components/resizePhoto/zoom.php?pic='+$(obj).attr("rel")+'&w=137&h=89&q=30';
	loadInterval = setInterval(function (){ $("#thumbImage").attr("src", url); }, 500);
	var int=0;
	if (!thumb_Opened){
		showInterval = setInterval(function () {
		$("#thumbnailPreview").stop();
		findthumbPos(obj);
		
		if (!thumb_Opening && !thumb_Closing && !thumb_Opened)
			thumbFade(1);
		}, 500);
	} else {
		showInterval = setInterval(function () {
		$("#thumbnailPreview").stop();
		findthumbPos(obj);
		
		if (!thumb_Opening && !thumb_Closing && !thumb_Opened)
			thumbFade(1);
		}, 100);
	}
}

var thumbHide = function (){
	clearInterval(showInterval);
	
	if (thumb_Opened)
	closeInterval = setInterval(function (){ $("#thumbnailPreview").stop(); thumbFade(0);
	clearInterval(closeInterval); }, 1000);
}

var photoIn = function (obj){
	var obj;
	var tW = 223;
	var tH = 149;
	$(obj).css("position", "absolute");
	$(obj).animate({ left:+3, top:+3, width: tW, height: tH }, 200);
}

var photoOut = function (obj){
	var obj;
	$(obj).css("position", "absolute");
	$(obj).animate({ left:1, top:1, width: 229, height: 155 }, 300);
}

var printWarn= function(obj, title, message, interval) {
	var title;
	var message;
	if (!isNaN(interval)){
		setTimeout(function (){
			$(obj).html('');
			}, interval);
		}	
	var htm = '<div class="ui-corner-all ui-state-highlight" style="margin-bottom:5px; padding:5px">'+
				 '<p>'+
				    '<span class="ui-icon ui-icon-info"></span><strong style="display:inline">'+title+' </strong>'+
							message+
				  '</p>'+
				'</div>';
	$(obj).html(htm);
	
};

var printError = function(obj, title, message, interval) {
	var title;
	var message;
	if (!isNaN(interval)){
		setTimeout(function (){
			$(obj).html('');
			}, interval);
		}
	var htm = '<div class="ui-corner-all ui-state-error" style="margin-bottom:5px; padding:5px">'+
				 '<p>'+
				    '<span class="ui-icon ui-icon-alert"></span>'+
				    '<strong style="display:inline">'+title+'! </strong>'+message+
				  '</p>'+
				'</div>';
	$(obj).html(htm);
};
