//
// FARBSCHEMA
//

/**
 * Quelle http://www.quirksmode.org/js/cookies.html
 * Farbschema
 */
function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/**
 * Quelle http://www.quirksmode.org/js/cookies.html
 * Farbschema
 */
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/**
 * Quelle http://www.quirksmode.org/js/cookies.html
 * Farbschema
 */
function eraseCookie(name) {
	createCookie(name,"",-1);
}

/**
 * Farbschema
 */
function removeBodyClasses() {
	jQuery("body").removeClass("lp");
	jQuery("body").removeClass("pl");
	jQuery("body").removeClass("grau");
	jQuery("body").removeClass("clean");
};

/**
 * Farbschema
 */
function changeFarbschemata(schema) {
	removeBodyClasses();
	switch(schema) {
		case "lp":
			jQuery("body").addClass("lp");
			saveFarbschemata("lp");
			break;
		case "pl":
			jQuery("body").addClass("pl");
			saveFarbschemata("pl");
			break;
		case "grau":
			jQuery("body").addClass("grau");
			saveFarbschemata("grau");
			break;
		case "clean":
			jQuery("body").addClass("clean");
			saveFarbschemata("clean");
			break;
		default:
			jQuery("body").addClass("lp");
			saveFarbschemata("lp");
			break;
	}

	eraseCookie("farbschemata");
	createCookie("farbschemata",schema,365);
}

/**
 * Farbschema
 */
function checkFarbschemata () {
	fsFC = readCookie("farbschemata");
	if ( fsFC == "xmas" ) {
		fsFC = "lp";
	}	
	if ( fsFC != null ) {
		removeBodyClasses();
		changeFarbschemata(fsFC);
	}
	else {
		changeFarbschemata("lp");
	}
}

/**
 * Farbschema
 */
function saveFarbschemata(schema) {
	var colorThemeURL = portalURL + '/web/pl/linklogin?p_p_id=plde_external_login_exterior&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_cacheability=cacheLevelPage&_plde_external_login_exterior_struts_action=%2Fext%2Flogin%2Fexterior%2Fcolortheme&ck=' + (new Date()).getTime();
	colorThemeResponse = jQuery.get(colorThemeURL + '&_plde_external_login_exterior_fsFC=' + schema,function(data){  });
}

//
// Statistictool
//

/**
 * @author Sebastian Redecker
 * @return void
 */
function saveClick(id) {
	jQuery.ajax({
	   type: "GET",
	   url: "/pl_statistic/statistic?statistic_id="+id,
	   async: true,
	   cache: false
	});
}

//
// Upload_Progress_Bar
//

/**
 * Setzt ein Interval für die ProgressBar.
 * @author Sebastian Redecker
 */
function progressBarTagLess(id, portalURL) {
	var key = Math.round(Math.random()*100000);
	var size = getFileSize(id);

	if(size > 0) {
		setInterval(function() {
			jQuery('#upload-poller').attr('src', portalURL+"/pl_upload/file?size="+size+"&uploadKey="+key); 
			jQuery('#upload-poller').load();
		}, 1500);
	}
	else {
		setInterval(function() {
			jQuery('#upload-poller').attr('src', portalURL+"/pl_upload/file?uploadKey="+key);
			jQuery('#upload-poller').load();
		}, 2150);
	}
}

/**
 * Gibt auf Grund einer ID die Größe einer Datei zurück.
 * @author Sebastian Redecker
 * @return filesize
 */
function getFileSize(id) {
	var bytes = -1;

	try {
		if(jQuery.browser.msie) {
			var fileObject = new ActiveXObject("Scripting.FileSystemObject");
			var filepath = jQuery("#"+id).val();
			//alert(filepath);
			var file = fileObject.getFile(filepath);
			var size = file.size;
			//alert(size + " bytes");
			bytes = size;		
		}
		else {  
			var fileInput = jQuery("#"+id)[0];            
			bytes = fileInput.files[0].fileSize;
		}
	}
	catch(e) {
		//alert("error");
	}

	return bytes;
}

//
// Link
//

/**
 * Öffnet ein neues Fenster zum aufrufen eines Links.
 * @author Sebastian Redecker
 * @return void
 */
function newWindow(url, title, width, height, settings) {
	var windowTitle = '';
	var windowHeight = 600;
	var windowWidth = 800;
	var windowSettings = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no';
	var positionX = (screen.width-windowWidth)/2;
  	var positionY = (screen.height-windowHeight)/4;
	
	if(url != '') {
		//TITEL setzen
	  	if(title != undefined) {
	  		windowTitle = title;
	  	}
	  	
	  	//GRÖSSE setzen
	  	if((width != undefined) && (height != undefined)) {
	  		windowHeight = height;
			windowWidth = width;
			positionX = (screen.width-windowWidth)/2;
		  	positionY = (screen.height-windowHeight)/4;
	  	}
	  	else if((width != undefined) && (height == undefined)) {
	  		windowHeight = width;
			windowWidth = width;
			positionX = (screen.width-windowWidth)/2;
		  	positionY = (screen.height-windowHeight)/4;
	  	}
	  	else if((width == undefined) && (height != undefined)) {
	  		windowHeight = height;
			windowWidth = height;
			positionX = (screen.width-windowWidth)/2;
		  	positionY = (screen.height-windowHeight)/4;
	  	}

	  	//SETTINGS setzen
	  	if(settings != undefined) {
	  		windowSettings = settings;
	  	}

	  	//Fenster öffnen
	  	window.open(url, windowTitle, windowSettings+',width='+windowWidth+',height='+windowHeight+',left='+positionX+',top='+positionY);
	}
	else {
		windowHeight = 420;
		windowWidth = 1000;

		//Fenster öffnen
		window.open('http://www.pflegeleidenschaft.de/error/', 'Fehler', ',width='+windowWidth+',height='+windowHeight+',left='+positionX+',top='+positionY);
	}
}

//
//Location
//

/**
 * Läd und öffnet die Auswahl an möglichen Orten.
 * @author Sebastian Redecker
 * @param locationInputId
 * @param locationSelectBoxId
 */
function openLocations(locationSelectBoxId, locationInputId) {
    var location = jQuery('#'+locationInputId).val();
    
    jQuery("#"+locationInputId).attr("autocomplete", "off");
    
    if(location.length > 3) {
		var html = jQuery.ajax({
							type: "GET",
							contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
			  				url: "/pl_location/location", 
			  				data: "p_selectBoxId="+locationSelectBoxId+"&p_inputId="+locationInputId+"&p_location="+encodeURIComponent(location), 
			  				cache: false, 
			  				async: false 
			 				}).responseText;
		
		jQuery('#'+locationSelectBoxId).html(html);
		jQuery('#'+locationSelectBoxId).css({display: 'block'});
		jQuery('#'+locationSelectBoxId).css('width',jQuery('#'+locationSelectBoxId).parent().css('width'));
    }
}

/**
 * Schließt die von openLocations geöffnete Auswahl an Orten wieder
 * @author Sebastian Redecker
 * @param locationSelectBoxId
 */
function closeLocations(locationSelectBoxId) {
	jQuery('#'+locationSelectBoxId).css({display: 'none'});
	jQuery('#'+locationSelectBoxId).html('');
}

/**
 * Schreibt den angeklicken Ort in das Textfeld.
 * @author Sebastian Redecker
 * @param location
 * @param locationInputId
 * @param locationSelectBoxId
 */
function setLocation(location, locationInputId, locationSelectBoxId) {
	jQuery('#'+locationInputId).val(location);
	closeLocations(locationSelectBoxId);
}
