

var timerA = 0;
var $menu = $("#menu");
var $cont = $("#containment");
var $contB = $("#containment_b");
var $page = $("#page");
var $page_title = $("#page_title");

$(document).ready(function(){
	
	isIE = (/msie/i).test(navigator.userAgent);
	isWebKit = (/webkit/i).test(navigator.appVersion);
	isFirefox = (/firefox/i).test(navigator.userAgent);
	isOpera = ('opera' in window);

	var qstr = getQueryString();
	if(qstr['path']){
		lnk = qstr['path'].replace(/,/g,"/");
		location.href = './#'+lnk;
	}
	
	init();


	backgroundSet();
	
	$("#logo").draggable({
		axis: 'x',
		stop: function(){ $(this).animate({left: '40px'}, 400) },
		containment: '#page'
	});
	$("#page_title").draggable({
		axis: 'x',
		containment: '#page'
	});

	$("#menu").children("li").each(function(){
		if($(this).hasClass("divider")){
			$(this).html("\\");
		} else {
			$(this).mouseover(function(){
				$("#menu_selection").stop().fadeTo(0,1).animate({ left: $(this).offset().left, top: $(this).offset().top, width: $(this).width()+10, height: $(this).height() }, 250);
			});
			
			$(this).mouseout(function(){
				var sel = $("#menu").children("li.selected");
				if($(sel).offset()) $("#menu_selection").stop().animate({ left: $(sel).offset().left, top: $(sel).offset().top, width: $(sel).width()+10, height: $(sel).height() }, 250);
			});
			
			$(this).click(function(){
				menuClickAnim(this,false);
			});
		}
	});
	
	$("#menu").mouseleave(function(){
		if($("#menu").children("li.selected").length == 0){
			$("#menu_selection").stop().fadeTo('fast',0);
		}
	});	
	
		
	$.history.init(function(hash){
			if(hash == "") { loadDefault(); }
			else { loadFromHistory(hash); }
		}, { 
		unescape: ",/" 
	});
	
	positioner();
	
});

$(window).load(function(){	
	$menu = $("#menu");
	$cont = $("#containment");
	$contB = $("#containment_b");
	$page = $("#page");
	$page_title = $("#page_title");
	setTimeout(function(){ imagePreload(); }, 500);

	/*setTimeout(function(){ 
		if(!$("#containment").is(":visible")) {
			$("#promo_container").fadeIn();
		}
	}, 1000);*/
});

$(window).resize(function(){
	positioner();
	backgroundSet();
});

function imagePreload() {
	if(document.images) {
		var imgArr = new Array();
		var c = -1;
		
		c++;imgArr[c] = new Image(); imgArr[c].src = "files/promos/promo_minerva_paris.jpg";		
		c++;imgArr[c] = new Image(); imgArr[c].src = "images/ajax-loader.gif";		
		c++;imgArr[c] = new Image(); imgArr[c].src = "images/bg_1920.jpg";
		c++;imgArr[c] = new Image(); imgArr[c].src = "images/bg_1280.jpg";
		c++;imgArr[c] = new Image(); imgArr[c].src = "images/logo_bg.png";		
	}
}


function init(){
	$.datepicker.setDefaults({
		showMonthAfterYear: false, 
		duration: 'fast',
		changeMonth: true,
		changeYear: true,
		gotoCurrent: true,
		dateFormat: 'yy-mm-dd',
		firstDay: 1, // Monday
		showButtonPanel: true,
		dayNames: ['Κυριακή', 'Δευτέρα','Τρίτη','Τετάρτη','Πέμπτη','Παρασκευή','Σάββατο'],
		dayNamesMin: ['Κυ', 'Δε','Τρ','Τε','Πε','Πα','Σα'],
		dayNamesShort: ['Κυρ','Δευ','Τρι','Τετ','Πεμ','Παρ','Σαβ'],
		monthNames: ['Ιανουάριος','Φεβρουάριος','Μάρτιος','Απρίλιος','Μάιος','Ιούνιος','Ιούλιος','Αύγουστος','Σεπτέμβριος','Οκτώβριος','Νοέμβριος','Δεκέμβριος'],
		monthNamesShort: ['Ιαν','Φεβ','Μαρ','Απρ','Μαι','Ιουν','Ιουλ','Αυγ','Σεπτ','Οκτ','Νοε','Δεκ']
	});
	
	
	$("a").each(function() {
		var $this = $(this);
		if($this.attr("href").substr(0,5)=="?path"){
			var lnk = $this.attr("href");
			lnk = lnk.replace(/\?path=/g, "#").replace(/,/g ,"/");
			$this.unbind('click').click(function(e) {
				location.href = lnk;
				return false;
			});
		}
	});
	
	initControls();
}

function getQueryString() {
	var result = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m;
	
	while (m = re.exec(queryString)) {
		result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
	}
	
	return result;
}
function initControls(){
	$("input[type=checkbox]").each(function(){
		if($(this).val()=='1'){
			$(this).attr("checked",true);
		} else {
			$(this).attr("checked",false);
			$(this).val('0');
		}
		
		$(this).change(function(){
			if($(this).attr("checked")){
				$(this).val('1');
			} else {
				$(this).val('0');
			}
		})
	});
}

$.fn.disableSelection = function() {
    $(this).attr('unselectable', 'on')
           .css('-moz-user-select', 'none')
           .each(function() { 
               this.onselectstart = function() { return false; };
            });
};

function gebi(elementID) {
	if(elementID) return document.getElementById(elementID);
	else return false;
}


String.prototype.trim = function() {
	tmp = this.replace(/^\s+/, '');
	return tmp.replace(/\s+$/, '');
};
function isValidEmail(str) {
	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str));
}
function isValidUsername(str) {
	return !(/\W/.test(str));
}

function acp(url, method, selector, callback){
	setBusy(1);
	method = (method=='GET' || method=='POST') ? method : 'GET';
	var varSend ="";
	var queryStr = (url.split("#")[1]) ? url.split("#")[1] : '';
	var nUrl = url.split("#")[0].split("?")[0];
	var exQstr = (url.split("#")[0].split("?")[1]) ? url.split("#")[0].split("?")[1] : '' ;
	
	if(queryStr.length > 0) { 
		for(i=0;i<queryStr.split(";").length;i++){
			if(gebi(queryStr.split(";")[i])) varSend += "&" + queryStr.split(";")[i] + "=" + encodeURIComponent(gebi(queryStr.split(";")[i]).value);
			else alert("WARNING: Element defined in url with id '"+queryStr.split(";")[i]+"' does not exist.");
		}
	}
	// check http://api.jquery.com/jQuery.ajax/  for reference	
	$.ajax({
		url: nUrl +"?"+ exQstr,
		data: varSend,	
		dataType: 'html',
		cache: false,
		type: method,
		statusCode: { 
			404: function() { alert('(404) page not found'); },
			500: function() { alert('(500) internal server error'); }
		},
		success: function(data){ performPostLoadOperations(data, selector, callback); }
	});
}

function performPostLoadOperations(data, selector, callback) {
	$(selector).html(data);
	if(gebi('script')) { eval(gebi('script').innerHTML.replace(/&gt;/g,">").replace(/&lt;/g,"<").replace(/&amp;/g,"&")); gebi('script').parentNode.removeChild(gebi('script')); }
	init();
	if(callback) callback.call(this, data);
	setBusy(0);
}

function setBusy(status){
	if(status) {
		$("#logo .back").show();
		$("#logo .front").hide();
	} else {
		$("#logo .back").hide();
		$("#logo .front").show();
	}
}

