// 
var app_url = "http://www.mostjobs.at/";

function showMessage(text){
	$(".page").append("<div class='messageBoxWrapper'><div class='messageBox'><span ><a class='closeBox' href='javascript:messageClose()'></a></span><h5>Systeminfo</h5><p>"+text+"</p></div></div>");

	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(".messageBoxWrapper").height();
	var popupWidth = $(".messageBoxWrapper").width();
	
	var hBoxDiff = popupWidth/2;
	var hWinDiff = 980/2;
	
	var hTotalDiff = hWinDiff - hBoxDiff;
	
	var vBoxDiff = popupHeight/2;
	var vWinDiff = windowHeight/3;
	
	var vTotalDiff = vWinDiff - vBoxDiff;
	
	//centering
	$(".messageBoxWrapper").css({
								
		"position": "absolute",
		"top": vTotalDiff ,
		"left": hTotalDiff
	});
	
	// show
	$(".messageBoxWrapper").show();
	
	// bind click to Box
	$(document).bind('click', function(e) {
		
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("messageBoxWrapper") ){
			messageClose();
		}
	});	
	
	
	// close auto
	window.setTimeout(function() {
	 	messageClose();
	 }, 15000);
}

// urlencode
function websave(str) {
	str = str.replace(/ä/g, 'ae');
	str = str.replace(/ö/g, 'oe');
	str = str.replace(/ü/g, 'ue');
	
	str = str.replace(/Ä/g, 'Ae');
	str = str.replace(/Ö/g, 'Oe');
	str = str.replace(/Ü/g, 'Ue');
	
	str = str.replace(/ß/g, 'sz');
	
	str = str.replace(/[^a-zA-Z0-9\-_]/g, '_');
	str = str.replace(/_+/g, '-');

	return str;
}

/* init login / register slidein */
initRegister();
initLogin();

function initRegister(myid){
	$("#toggleRegister").click(function() {
		$("#registerBlock").toggle();
		$("#registerusername").focus();
	});	
	
	$(document).bind('click', function(e) {
		
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("registerspan") ){
			$("#registerBlock").hide();
		}
	});	
	
}

function initLogin(myid){
	$("#toggleLogin").click(function() {
		$("#loginBlock").toggle();
		$("#username").focus();
	});	
	
	$(document).bind('click', function(e) {
		
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("loginspan") ){
			$("#loginBlock").hide();
		}
	});	
	
}

// dropdown
function initDropDown(myid,myType) {
    
	$("#" + myid + ".dropdown dt a").click(function() {
		$("#" + myid + ".dropdown dd ul").toggle();
	});
				
	$("#" + myid + ".dropdown dd ul li a").click(function() {
		var text = $(this).html();
		// update 2010-06-01 // $("#" + myid + ".dropdown dt a span").html(text);
		$("#" + myid + ".dropdown dt span.display").html(text);
		$("#" + myid + ".dropdown dd ul").hide();
	   	//$("#result").html('kategorie-' + getSelectedValue("sample") + '-' + websave(getSelectedTitle("sample")));
		
		// 
		if(typeof baseUrl == 'undefined'){
			baseUrl = app_url + 'stellen/';
		}
		
		// replace Type
		switch(myType){
			case "region":
				baseUrl = baseUrl.replace(/\/region-[a-zA-Z0-9_\-]*/g, '');
				break;
			case "kategorie":
				baseUrl = baseUrl.replace(/\/kategorie-[a-zA-Z0-9_\-]*/g, '');
				break;
		}
		
		// remove myType from app_url
		var myLink = 	baseUrl + myType + '-' + getSelectedValue(myid) + '-' + websave(getSelectedTitle(myid));
		
		// go there
		window.document.location = myLink;
	});
				
	function getSelectedValue(id) {
		return $("#" + id).find("dt span.display span.value").html();
	}
	
	function getSelectedTitle(id) {
		return $("#" + id).find("dt span.display span.title").html();
	}

	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("dropdown"))
			$("#" + myid + ".dropdown dd ul").hide();
	});	
};

function messageClose(){
	$('.messageBoxWrapper').fadeOut();
}

//$('#toggleLogin').click(function() { displayLogin(); } );
//$('#toggleRegister').click(function() { displayRegister(); } );



// displayBox
function displayLogin(){
	if (isHidden('#loginBlock') ) {
		hideBox('#loginBlock');
	} else {
		hideBox('#registerBlock');
		showBox('#loginBlock');
	}
}

// displayBox
function displayRegister(){
	if (isHidden('#registerBlock') ) {
		hideBox('#registerBlock');
	} else {
		showBox('#registerBlock');
		hideBox('#loginBlock');
	}
}

// hide
function hideBox(boxName){
	$(boxName).hide();
}

// show
function showBox(boxName){
	$(boxName).show();
}

// isHidden
function isHidden(a){
	return jQuery(a).css('display') != 'none';
}

/** field **/
function clearField(fieldName,RemoveValue){
	if($(fieldName).val() == RemoveValue){
		$(fieldName).val("");	
	}
}

function resetField(fieldName,ResetValue){
	if($(fieldName).val() == ""){
		$(fieldName).val(ResetValue)
		return;
	}	
}


/********************************** AJAX ******************************************/
function callToAction(theAction,theKey,theValue,animateThis,hideThis) {
	$.ajax({
		type: "GET",
		url: app_url+"class/session-writer.php",
		data: "sh="+theAction+"&key="+theKey+"&value="+theValue,
		success: function(msg){
			
			if(typeof hideThis == undefined){
				hideThis = animateThis;
			}
			
			
			
			//alert('animate:' + animateThis + ' hide:' + hideThis);
			if(animateThis == ''){
				
			} else if(animateThis == 'returnMapJobsTotal') {
				//alert('jobtotal'+msg);
				jobsTotal = msg;
			} else if(animateThis == 'returnValue') {
				//alert('page'+msg);
				aktJobMapPage = msg;
			} else {
				displayInfo(msg,animateThis,hideThis);
			}
			//$(theTarget).html(msg);
			//$(theTarget).fadeIn();
		},
		error:function (xhr, ajaxOptions, thrownError){
            alert(xhr.status);
            alert(thrownError);
        }   
	});
}

function getLocation(source){
	switch (source) {
		case "user_id":
		case "company_id":
			var theAction = 'location_'+source;
			break;
		default:
			return false;
	}
	
	$.ajax({
		type: "GET",
		url: app_url+"class/session-writer.php",
		data: "sh="+theAction,
		success: function(msg){
			
			var locationResult = msg.split("|");
			
			$("input[name='street']").val(locationResult[0]);
			$("input[name='zip']").val(locationResult[1]);
			$("input[name='city']").val(locationResult[2]);
		},
		error:function (xhr, ajaxOptions, thrownError){
            alert(xhr.status);
            alert(thrownError);
        }   
	});
}

function displayInfo(myText,animateId,hideId){
	
	$('#messageline').stop(true, true).fadeOut();
	$('#messageline').html("<span>"+myText+"</span>");
	$('#messageline').fadeIn();
	$('#messageline').delay(3000).fadeOut();
	
	/* animate element */
	if(typeof animateId != undefined){
		$(animateId).fadeOut();
	}
	
	/* hide element */
	if(typeof hideId != undefined){
		$(hideId).delay(400).fadeOut('fast');
	}
	
}

function clearSV(skey,svalue,theTarget){
	callToAction('unset',skey,svalue,theTarget);	
}

function saveJob(mysql,theTarget){
	$.ajax({
		type: "GET",
		url: app_url+"class/jobSaver.php",
		data: "mysql="+mysql,
		success: function(msg){
			$("#ajaxcontent").html(msg);
			$(theTarget).html('eingetragen');
		},
		error:function (xhr, ajaxOptions, thrownError){
            alert(xhr.status);
            alert(thrownError);
        }   
	});
}


function rememberJob(myId,animateTarget,hideTarget){
		callToAction('remember','job_id',myId,animateTarget,hideTarget);	
		checkRememberBox('show');
}

function forgetJob(myId,animateTarget,hideTarget){
		callToAction('forget','job_id',myId,animateTarget,hideTarget);
		checkRememberBox('hide');	
}

function checkRememberBox(preferedState){
	
	
	if( $('#rememberButton').is(":visible") && preferedState == 'hide' ){
    	// check if remember session is empty
		$.ajax({
		  type: "GET",
		  url: app_url+"class/session-writer.php",
		  data: "sh=rememberArrayCount",
		  success: function(msg) {
			if(msg == 0){
				$('#rememberButton').fadeOut();	
				 setTimeout(function(){ 
                         window.location = app_url+"stellen"; 
                    }, 1000);
		  	}
		  }
		  });
		
	} else if ( $('#rememberButton').is(":hidden") && preferedState == "show") {
	   // do something else
	   $('#rememberButton').fadeIn();
	}
}


function checkValidEmail(fieldName,theTarget,exclusion){
	var fieldValue = $('input[name='+fieldName+']').val();	

	// show loader
	$(theTarget).html('<img src="'+app_url+'css/pix/mj-loader.gif" />');
	$.ajax({
		   type: "GET",
		   url: app_url+"class/checkEmail.php",
		   data: "fieldName="+fieldName+"&fieldValue="+fieldValue+"&exclusion="+exclusion+"&random="+Math.floor(Math.random()*999),
		   success: function(msg){
			$(theTarget).html(msg);
		},
		error:function (xhr, ajaxOptions, thrownError){
            alert(xhr.status);
            alert(thrownError);
        }   
	});
}

function checkAvailable(fieldName,theTarget,exclusion){
	
	var fieldValue = $('input[name='+fieldName+']').val();	
	
	if(fieldValue == exclusion) {
		return true;	
	} 
	
	// show loader
	$(theTarget).html('<img src="'+app_url+'css/pix/mj-loader.gif" />');
	$.ajax({
		   type: "GET",
		   url: app_url+"class/checkAvailable.php",
		   data: "fieldName="+fieldName+"&fieldValue="+fieldValue+"&random="+Math.floor(Math.random()*999),
		   success: function(msg){
			$(theTarget).html(msg);
		},
		error:function (xhr, ajaxOptions, thrownError){
            alert(xhr.status);
            alert(thrownError);
        }   
	});
}



