/**
 * system - created on Jan 20, 2011 07:49:25 PM by thomi
 * @copyright		win-soft.ch 2011
 * @author			Thomas Winteler <http://www.win-soft.ch>
 */


$(function()
{
//	if(searchBoxName === undefined) { searchBoxName = '';}
//	if(searchBoxCSS === undefined) { searchBoxCSS = '';}
//	if(searchBoxOutputName === undefined) { searchBoxOutputName = '';}

	if(searchBoxName && searchBoxCSS && searchBoxOutputName)
	{
		$(document).ready(function() {
			$.get("system/core/system.php?tool=search_box&searchBoxName="+searchBoxName+"&searchBoxCSS="+searchBoxCSS+"&searchBoxOutputName="+searchBoxOutputName+"&searchBoxIcon="+searchBoxIcon+"&searchBoxIconDimension="+searchBoxIconDimension+"&searchBoxIconPosition="+searchBoxIconPosition+"&searchBoxIconStyle="+searchBoxIconStyle, function(data) {
				$("#"+searchBoxName).html(data);
//				setTimeout(function() {  
//	        		$("input[type='text']:enabled:first").focus();
//	        	}, 500);
			});
		});
	}

	if(languageBoxName && languageBoxCSS)
	{
		$(document).ready(function() {
			$.get("system/core/system.php?tool=language_box&languageBoxName="+languageBoxName+"&languageBoxCSS="+languageBoxCSS+"&languageBoxIcon="+languageBoxIcon+"&languageBoxIconDimension="+languageBoxIconDimension+"&languageBoxIconPosition="+languageBoxIconPosition+"&languageBoxIconStyle="+languageBoxIconStyle, function(data) {
				$("#"+languageBoxName).html(data);
			});
		});
	}

	if(floatingContent && floatingContent !== undefined)
	{
		$(document).ready(function() {
			for(var contentID in floatingContent)
			{
				$.get("system/core/frontend/content.php?modulName=content&contentID="+contentID+"&navigationID="+navigationID, function(data) {
					$("#"+floatingContent[contentID]).html(data);
				});
			}
		});
	}

	if(contentNewsTickerListDIVID && contentNewsTickerListULID)
	{
		$(document).ready(function() {
			$.get("system/core/system.php?tool=content_news_ticker&contentNewsTickerListULID="+contentNewsTickerListULID, function(data) {
				$("#"+contentNewsTickerListDIVID).html(data);
			});
		});
	}

});

function showSystemMessageBox(message, type, show, timeout)
{
	if(timeout === undefined) { timeout = 0; }
	
	$("#DIV_system_messages").html("");
	switch (type)
	{
		case "success":
			$("#DIV_system_messages").html("<img src=\"http://"+window.location.hostname+"/admin/img/16_success.png\" style=\"margin-right: 10px; vertical-align: bottom;\"><b>"+message+"</b>");
			break;
	
		case "progress":
			$("#DIV_system_messages").html("<img src=\"http://"+window.location.hostname+"/admin/img/16_progress.png\" style=\"margin-right: 10px; vertical-align: bottom;\"><b>"+message+"</b>");
			break;
	
		case "alert":
			$("#DIV_system_messages").html("<img src=\"http://"+window.location.hostname+"/admin/img/16_exclamation.png\" style=\"margin-right: 10px; vertical-align: bottom;\"><b>"+message+"</b>");
			break;
	
		default:
			break;
	}
	
	if(show)
	{
		$("#DIV_system_messages").show();
		var optionsDown = {
				"top": "100px",
				"border-top-left-radius": "10px",
				"border-top-right-radius": "10px",
				"-moz-border-radius-topleft": "10px",
				"-moz-border-radius-topright": "10px",
				"-webkit-border-top-left-radius": "10px",
				"-webkit-border-top-right-radius": "10px"
		};

		var optionsUp = {
				"top": "0px",
				"border-top-left-radius": "0px",
				"border-top-right-radius": "0px",
				"-moz-border-radius-topleft": "0px",
				"-moz-border-radius-topright": "0px",
				"-webkit-border-top-left-radius": "0px",
				"-webkit-border-top-right-radius": "0px"
		};

		$("#DIV_system_messages").animate(optionsDown, "fast");
		setTimeout(function() {  
			$("#DIV_system_messages").animate(optionsUp, "slow");;
		}, 1000);

		if(timeout)
		{
			setTimeout(function() {  
				$("#DIV_system_messages").hide();
				$("#DIV_system_messages").html("");
			}, timeout);
		}
	}
	else
	{
		$("#DIV_system_messages").hide();
		$("#DIV_system_messages").html("");
	}
}

function searchBoxSearch(searchBoxNameID, searchBoxOutputNameID)
{
	if($("#"+searchBoxNameID).val().length > 3)
	{
		consoleLog(searchBoxResultInfo);
		$.get("system/core/system.php?tool=search_box_search&keyword="+$("#"+searchBoxNameID).val()+"&searchBoxName="+searchBoxName+"&searchBoxResultTitle="+searchBoxResultTitle+"&searchBoxResultInfo="+searchBoxResultInfo+"&searchBoxResultSize="+searchBoxResultSize+"&searchBoxResultLinkClass="+searchBoxResultLinkClass+"&searchBoxNoResultInfo="+searchBoxNoResultInfo+"&searchBoxKeywordPersist="+searchBoxKeywordPersist, function(data) {
			$("#"+searchBoxOutputNameID).html(data);
		});
	}
}

function languageBoxChange(languageID, languageName)
{
//	showDebugDialog(languageID);

	$.get("system/core/system.php?tool=language_box_change&languageID="+languageID+"&languageName="+languageName, function(data) {
		location.reload(true);
	});
	
}

function getWidth()
{
	var x = 0;
	if(self.innerWidth)
	{
		x = self.innerWidth;
	}
	else if(document.documentElement && document.documentElement.clientWidth)
	{
		x = document.documentElement.clientWidth;
	}
	else if(document.body)
	{
		x = document.body.clientWidth;
	}
	
	return x;
}

function getHeight()
{
	var y = 0;
	if(self.innerHeight)
	{
		y = self.innerHeight;
	}
	else if(document.documentElement && document.documentElement.clientHeight)
	{
		y = document.documentElement.clientHeight;
	}
	else if(document.body)
	{
		y = document.body.clientHeight;
	}
	
	return y;
}



/*
 * 
 * System Management Functions
 * 
*/

function object2string(obj)
{
	obj = JSON.stringify(obj);
	obj = obj.replace(/\"/g, "\'");
	return obj;
}

function showSystemManagementDialog(dialogTitle)
{
	checkLoginData();

	$('#DIV_admin_panel_menu').toggle("slow");
	$.get("admin/system_management.php", function(data) {
		$(function() {
			$("#DIV_system_management_dialog").html(data);
			var system_management_dialog_buttons = {};
			system_management_dialog_buttons[adminLanguage["System_Management_Close"]] = function() {
					$(this).dialog("close");
				};
//				system_management_dialog_buttons[adminLanguage["System_Management_Add_File"]] = function() { 
//				showFileManagementManageFileDialog('edit_file');
//				};
			$("#DIV_system_management_dialog").dialog({
				title: dialogTitle,
				height: 600,
				width: 800,
				modal: true,
				resizable: false,
				draggable: false,
				closeOnEscape: true,
				buttons: system_management_dialog_buttons,
				open: function() {
	                $('.ui-dialog-buttonpane').find('button:contains("'+adminLanguage["System_Management_Close"]+'")').button({
	                    icons: {
	                        primary: 'ui-icon-circle-close'
	                    }
	                });
//	                $('.ui-dialog-buttonpane').find('button:contains("'+adminLanguage["File_Management_Add_File"]+'")').button({
//	                    icons: {
//	                        primary: 'ui-icon-circle-plus'
//	                    }
//	                });
				},
				close: function() {
					location.reload(true);
				}
			});
			$("#DIV_system_group_tab").tabs({ ajaxOptions: { async: false } });
		});
		
	});

}


function modulSchemeInit(modulName, update)
{
	checkLoginData();
	
	if(update === undefined) { update = 0;}
	if(update)
	{
		$("#DIV_SM_modul_progress_message").html("<img src=\"http://"+window.location.hostname+"/admin/img/16_progress.png\" style=\"margin-right: 10px;\">"+adminLanguage["System_Management_Modul_Scheme_Update"]);
	}
	else
	{
		$("#DIV_SM_modul_progress_message").html("<img src=\"http://"+window.location.hostname+"/admin/img/16_progress.png\" style=\"margin-right: 10px;\">"+adminLanguage["System_Management_Modul_Scheme_Init"]);
	}

	var dataModulInit = $.ajax({
		url: "admin/system.php?mode=system_modul&modul="+modulName+"&remove=0",
		async: false
	});
	
//	alert(dataModulInit.responseText.length);
	
	if(dataModulInit.responseText.length > 1)
	{
		$("#DIV_SM_modul_progress_message").html("<img src=\"http://"+window.location.hostname+"/admin/img/16_failed.png\" style=\"margin-right: 10px;\">"+adminLanguage["System_Management_Modul_Scheme_Error"]+"<br>"+dataModulInit.responseText);
	}
	else
	{
		if(update)
		{
			$("#DIV_SM_modul_progress_message").html("<img src=\"http://"+window.location.hostname+"/admin/img/16_success.png\" style=\"margin-right: 10px;\">"+adminLanguage["System_Management_Modul_Scheme_Update_Success"]);
		}
		else
		{
			$("#DIV_SM_modul_progress_message").html("<img src=\"http://"+window.location.hostname+"/admin/img/16_success.png\" style=\"margin-right: 10px;\">"+adminLanguage["System_Management_Modul_Scheme_Init_Success"]);
		}

		$("#DIV_SM_modul_progress_message").fadeOut(4000, function() {
			$("#DIV_system_group_tab").tabs("load", 2);
		});

	}
	
}

function modulSchemeRemove(modulName)
{
	
	checkLoginData();
	
	if(confirm(adminLanguage["System_Management_Modul_Scheme_Remove"]+"\n"+modulName))
	{
		$("#DIV_SM_modul_progress_message").html("<img src=\"http://"+window.location.hostname+"/admin/img/16_progress.png\" style=\"margin-right: 10px;\">"+adminLanguage["System_Management_Modul_Scheme_Remove_Progress"]);
		
		var dataModulRemove = $.ajax({
			url: "admin/system.php?mode=system_modul&modul="+modulName+"&remove=1",
			async: false
		});
		
		if(dataModulRemove.responseText.length > 1)
		{
			$("#DIV_SM_modul_progress_message").html("<img src=\"http://"+window.location.hostname+"/admin/img/16_failed.png\" style=\"margin-right: 10px;\">"+adminLanguage["System_Management_Modul_Scheme_Error"]+"<br>"+dataModulRemove.responseText);
		}
		else
		{
			$("#DIV_SM_modul_progress_message").html("<img src=\"http://"+window.location.hostname+"/admin/img/16_success.png\" style=\"margin-right: 10px;\">"+adminLanguage["System_Management_Modul_Scheme_Remove_Success"]);
			
			$("#DIV_SM_modul_progress_message").fadeOut(4000, function() {
				$("#DIV_system_group_tab").tabs("load", 2);
			});
			
		}
		
	}
}


function systemChangeIconMode(divID, fieldID, mode, title_edit, title_save, context)
{
	icon_edit = "<img src=\"http://"+window.location.hostname+"/admin/img/16_edit.png\" style=\"margin: 0 2px;\" border=\"0\" title=\""+title_edit+"\" width=\"16px\" height=\"16px\" tooltip=\"system_icon\">";
	icon_save = "<img src=\"http://"+window.location.hostname+"/admin/img/16_save.png\" style=\"margin: 0 2px;\" border=\"0\" title=\""+title_save+"\" width=\"16px\" height=\"16px\" tooltip=\"system_icon\">";
	
	context = object2string(context);
	
	//~ context = JSON.stringify(context);
	//~ context = context.replace(/\"/g, "\'");
	
	//~ consoleLog(context);
	
	switch(mode)
	{
		case 'edit':
			
			$("#"+fieldID).attr("disabled", "disabled");
			$("#"+fieldID).removeClass("ui-state-highlight");
			$("#"+divID).html("<a href=\"#\" onclick=\"systemChangeIconMode('"+divID+"', '"+fieldID+"', 'save', '"+title_edit+"', '"+title_save+"', "+context+");\">"+icon_edit+"</a>")
			
			break;
		
		case 'save':
			
			$("#"+fieldID).removeAttr("disabled");
			$("#"+fieldID).addClass("ui-state-highlight");
			$("#"+divID).html("<a href=\"#\" onclick=\"systemSaveFieldValue('"+fieldID+"', "+context+");systemChangeIconMode('"+divID+"', '"+fieldID+"', 'edit', '"+title_edit+"', '"+title_save+"', "+context+");\">"+icon_save+"</a>")
			
			break;
		
		default:
			break;
	}
	
	//~ $("input[tooltip=system_icon], select[tooltip=system_icon], img[tooltip=system_icon]").tooltip({ showURL:false, track:true, showBody:' - '});
	
}


function systemSaveFieldValue(fieldID, context)
{
	var dataSave = $.ajax({
		url: "admin/system.php?mode=system_save_field&table="+context.table+"&id="+context.id+"&field="+context.field+"&value="+$("#"+fieldID).val(),
		async: false
	});

	consoleLog(dataSave.responseText);

	if(dataSave.responseText.search(/success/) != -1)
	{
		showSystemMessageBox(adminLanguage["Admin_General_Save_Success"], "success", 1, 4000);
	}
	else
	{
		$("body").append(dataSave.responseText);
	}
	
}

function consoleLog(data)
{
	if($.browser.mozilla || $.browser.safari)
	{
		console.log(data);
	}
}








