var pane;
var paneApi;
var theWindow;
var currentLang = "ell";
var socialWin;
$(function(){
	
	setTimeout( function(){
		$("#welcomeMsg").animate({"top":"73px"}, 3000, "easeInOutQuart", function(){ 
			$("#welcomeMsg").delay(15000).animate({"top":"-100px"}, 3000, "easeInOutQuart");
		});
	}, 5000 );
	
	currentLang = $("#currentLang").val();
	
	$("#sideMenu a").click( showContentAsync ).hover( menuHoverIn, menuHoverOut );
	
	pane = $("#scollContent");
	pane.jScrollPane({autoReinitialise: true});
	paneApi = pane.data('jsp');
	
	$(".content:first a").fancybox();
	
	//snd
	$("#sndControl").click( toggleSound );
	
	//BGs
	theWindow        = $(window),
		
								
	theWindow.resize(function() {
		resizeBg();
	}).trigger("resize");
	
	initBGGallery();
	
	$("#fbLink").click( FBShare );
});

function FBShare(e)
{
	e.preventDefault();
	var uri = "http://www.facebook.com/sharer.php?u=http://kea-rooms.gr/?op=" + currentLang;	
	socialWin = window.open( uri, "socialWin", "menubar=0,resizable=0,width=650,height=350", true);
	socialWin.focus();
}

function resizeBg() 
{
		
	$bg              = $("#bgPhotoContainer .fullSize img"),
	aspectRatio      = $bg.width() / $bg.height();
	
	if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
		$bg
			.removeClass("bgwidth")
			.addClass('bgheight');
	} else {
		$bg
			.removeClass("bgheight")
			.addClass('bgwidth');
	}
					
}

var bgTmr;
function initBGGallery()
{
	$("#bgPhotoContainer .fullSize").each( function(i,el){
		//var ofsLeft = ($(window).width() - $(el).width())/2;		
		$(el).css({"z-index":2, top:"-850px"});
	});
	
	$("#bgPhotoContainer .fullSize:first").addClass("curSlide").css({top:0,"z-index":1,opacity:1}).show();
	
	bgTmr = setInterval( triggerBGGallery, 20000 );
}

function resetGallery( arImages )
{
	if (bgTmr)
		clearInterval( bgTmr );
	
	$("#bgPhotoContainer .fullSize").stop(true,true).clearQueue().remove();
	
	var imHtml = '';
	for(var i in arImages)
		imHtml += '<div class="fullSize"><img src="' + arImages[i] + '" class="" /></div>';
		
	console.log( imHtml );
	$("#bgPhotoContainer").html( imHtml );
	
	resizeBg();
	initBGGallery();
}

function triggerBGGallery()
{
	var curSlide = $("#bgPhotoContainer .curSlide:first");
	
	var curIdx = $("#bgPhotoContainer .fullSize").index( curSlide ) + 1;
	
	if ( curIdx+1 <= $("#bgPhotoContainer .fullSize").length )
		nextID = curIdx+1;
	else
		nextID	= 1;
	
	var nextSlide = $("#bgPhotoContainer .fullSize:nth-child(" + nextID + ")");
	
	curSlide.removeClass("curSlide").css("z-index",1);
	nextSlide.addClass("curSlide").css({top:"-850px", opacity:0, "z-index":2}).show();
	
	//middle
	var ofsTop = ($(window).height() - nextSlide.height())/2;
	nextSlide.animate({top: -ofsTop, opacity:1}, 3400, "easeInOutQuart", function(){ curSlide.css({top:"-1000px", "z-index":2}); } );	
}
		

function toggleSound(e)
{
	var snd = $("#sndControl");
	if ( $("#sndControl").hasClass("soundOn") )
	{
		snd.removeClass("soundOn").addClass("soundOff");
		$("#sndPlayer").attr("src", "");	
	} else {
		snd.removeClass("soundOff").addClass("soundOn");
		$("#sndPlayer").attr("src", "http://www.youtube.com/embed/ad1nAbAswZM?rel=0&autoplay=1");
	}
}

function menuHoverIn(e)
{
	var p = $(this).parent();
	p.find(".overDrop:first").stop(true,true).clearQueue().fadeTo(300, 1);	
}

function menuHoverOut(e)
{
	var p = $(this).parent();
	if ( $(this).hasClass("selected")) return;
	
	p.find(".overDrop:first").stop(true,true).clearQueue().fadeTo(300, 0);
}

function showContentAsync(e)
{
	e.preventDefault();
	
	var p = $(this).parent();
	p.find(".overDrop:first").stop(true,true).clearQueue().fadeTo(300,1);
	$("#sideMenu .selected").removeClass("selected").parent().find(".overDrop:first").stop(true,true).clearQueue().fadeTo(300,0);
	
	var lnk = $(this).attr("href");
	$(this).addClass("selected");
	$(".view:first h1").stop(true,true).clearQueue().fadeOut(800, function(){
	$.post("index.php", {"op": lnk, "async":""}, function (r){ transContent(r); }, "json");
	});
	$(".view:first .content:first").stop(true,true).clearQueue().fadeOut(800);
	
	_gaq.push(['_trackPageview', "/" + lnk]);
}

function transContent(r)
{
	$(".view:first h1").html( r.title );
	paneApi.getContentPane().html( r.text );
	paneApi.reinitialise();
	$(".view:first h1, .view:first .content:first").stop(true,true).clearQueue().fadeIn(800);
	$(".content:first a[rel='gal']").fancybox();
	
	document.title = r.title + " | " + $("#originalSiteTitle").val();
	
}

function validateForm()
{
	var passForm = true;
	var errMsgs = [];
	var vocab = {
		generalError: {ell:"Παρακαλώ συμπληρώστε όλα τα απαραίτητα πεδία.\n\nΕντοπίστηκαν τα παρακάτω κενά πεδία: ", eng:"Please fill in all obligatory fields.\n\nBlank fields detected: ", ita:"Please fill in all mandatory fields.\n\nBlank fields detected" },
		pleaseWait: { ell: "Παρακαλώ περιμέντε...", eng:"Please Wait...", ita: "Please Wait..."},
		sendOK: {ell: "Η αποστολή ολοκληρώθηκε επιτυχώς. Ευχαριστούμε για την επικοινωνία", eng:"Send was succesfull. Thank you for your interest.", ita: "Send was succesfull. Thank you for your interest."},
		sendFail: {ell:"Η αποστολή απέτυχε. Παρακαλώ ξαναπροσπαθήστε", eng:"Send Failed. Please try again", ita:"Send Failed. Please try again"}
	};
	
	$("#frmContact .invalidField").removeClass("invalidField");
	
	var pObj = {op:"contactFormCmt"};
	
	$("#frmContact *[validate]").each( function(i,el){
		
		var t = $.trim( $(el).val() );
		passForm = passForm && ( t!="" );
		if (!t)
		{
			$(el).addClass("invalidField");
			errMsgs.push( " - " + $(el).attr("validate") );	
		}
	});
	
	$("#frmContact :input").each( function(i,el){
		pObj[ $(el).attr("name") ] = $.trim( $(el).val() );
	});
	
	if (passForm)
	{
		$("#btnSubmit").attr("disabled", "disabled");
		$("#contactResponse").html( vocab.pleaseWait[currentLang] );
		paneApi.scrollTo(0, 1000 );
		
		$.post("index.php", pObj, function(r) {
			if (r=="1")
			{
				$("#contactResponse").html( vocab.sendOK[currentLang] );
				_gaq.push(['_trackPageview', "/contactFormSubmited" ]);
				
				//clear form
				$("#frmContact input[type='text'], #frmContact textarea").val("");
			} else {
				$("#contactResponse").html( vocab.sendFail[currentLang] );
			}
			$("#btnSubmit").removeAttr("disabled");
		});
	} else {
		alert( vocab.generalError[currentLang] + "\n" + errMsgs.join("\n") );
		
	}
	
	return false;
	
}

function showMap()
{
	$.fancybox( 'map.html', {
		'padding'			: 10,
		'width' : "75%",
		'height' : "75%",
		 'autoScale'     	: false,
		'type'              : 'iframe'
	});
}
