/**
 * @category  DeWeM
 * @author    DeWeM Team <admin@dewem.com>
 * @copyright	Copyright (C) 2011 dewem.com, All rights reserved.
 * @license		GNU General Public License version 3; see LICENSE.txt or http://www.gnu.org/licenses/gpl-3.0.html
 * @link      http://www.dewem.com 
 * THIS PROGRAM IS DISTRIBUTED "AS IS";. NO WARRANTY OF ANY KIND IS 
 *	EXPRESSED OR IMPLIED. YOU USE AT YOUR OWN RISK. I WILL NOT BE LIABLE FOR DATA 
 *	LOSS, DAMAGES, HACKING, SPAMMING, LOSS OF PROFITS OR ANY OTHER KIND OF LOSS 
 *	WHILE USING OR MISUSING THIS SOFTWARE. 
 */

function showUnitsDewem(event, idMenuUnits, attr){
	var menuUnits = document.getElementById(idMenuUnits);
	menuUnits.style.top = getYDewem(event)+'px';
	menuUnits.style.left = getXDewem(event)+'px';
	menuUnits.style.visibility = 'visible';
	menuUnits.style.zIndex = zIndexGlobalDewem;
}
function unitOnMouseoverDewem(menuElement){  //menuElement
	menuElement.style.backgroundColor = "#fff468";
	if (bNSDewem){
		menuElement.style.opacity = '.70';
	}else if (bIEDewem){
		menuElement.style.filter = 'alpha(opacity=70)';
	}
}
function unitOnMouseoutDewem(menuElement){
	menuElement.style.backgroundColor = "#fff797";
	if (bNSDewem){
		menuElement.style.opacity = '1';
	}else if (bIEDewem){
		menuElement.style.filter = 'alpha(opacity=100)';
	}
}
function setAttributeValueDewem(inputUnitName, unitValue){
	unitValue = unitValue.replace(/,/g, 'dewemComma');
	sendDewemAjaxRequest('Workel.php', 'setAttributeValue', inputUnitName, unitValue);
}
function selectUnitDewem( unitsDivName, inputUnitName, inputValueName, unitValue){
	document.getElementById(inputUnitName).value = unitValue;
	var inputValue = document.getElementById(inputValueName).value;
	setAttributeValueDewem(inputUnitName, inputValue + unitValue);
	document.getElementById(unitsDivName).style.visibility = 'hidden';
}
function selectValueDewem( valuesDivName, inputUnitName, inputValueName, valueValue){
	document.getElementById(inputValueName).value = valueValue;
	var inputUnitEl = document.getElementById(inputUnitName);
	if (inputUnitEl){
		var inputUnit = inputUnitEl.value;
	}else{
		var inputUnit = '';
	}
	setAttributeValueDewem(inputValueName, valueValue + inputUnit);
	document.getElementById(valuesDivName).style.visibility = 'hidden';
}
function editValueDewem(event, parentFormName, attr){
	event = (event) ? event : window.event;
	var element = (event.target) ? event.target: event.srcElement;
	var editName = '';
	editName = parentFormName+'_Attr'+attr;
	if (editName == ''){
		return;
	}

	var editDiv = document.getElementById(editName);
	editDiv.style.position = 'absolute';
	editDiv.style.display = 'block';
	editDiv.style.left =  getXDewem(event)+'px'; 
	editDiv.style.top = getYDewem(event)+'px';
	sendDewemAjaxRequest('Workel.php', 'outputValues', parentFormName+'_Attr'+attr+'_EditValues', attr);	
	editDiv.style.visibility = 'visible';
}
function showConfirmDewem (confirmName, confirmAllName){
	var confirmDiv = document.getElementById(confirmName);
	confirmDiv.style.backgroundImage = 'url(../img/confirm.png)';
	confirmDiv.style.display= 'inline';
	if (confirmAllName){
		var confirmAllDiv = document.getElementById(confirmAllName);
		confirmAllDiv.style.backgroundImage = 'url(../img/confirmAll.png)';
		confirmAllDiv.style.display= 'inline';
	}
}
function confirmValueDewem(attr, inputValueName, inputUnitName){
	var valueValue = document.getElementById(inputValueName).value ;
	var inputUnitEl = document.getElementById(inputUnitName);
	if (inputUnitEl){
		var inputUnit = inputUnitEl.value;
	}else{
		var inputUnit = '';
	}
	if (valueValue == ''){
		var rezValue = ''
	}else{
		var rezValue = valueValue+inputUnit;
	}
	setAttributeValueDewem(inputValueName, rezValue);
}

function confirmAllValuesDewem(workelName, formName, attributesString){
	showHourGlassDewem();
	attributesString = attributesString.substring(0, (attributesString.length-1));
	var attributesArr = attributesString.split(',');
	var paramString = '';
	for(var i=0; i<attributesArr.length; i++) {
		var currValueEl = document.getElementById(formName+'_Attr'+attributesArr[i]+'_Value');
		var currUnitEl = document.getElementById(formName+'_Attr'+attributesArr[i]+'_Unit');
		if (currUnitEl){
			var currValue = currValueEl.value + currUnitEl.value;
			if (currValue == currUnitEl.value){
				currValue = '';
			}
		}else{
			if (currValueEl.type == 'checkbox'){
				if (currValueEl.checked){
					var currValue = currValueEl.value;
				}else{
					var currValue = '';
				}
			}else{
				var currValue = currValueEl.value;
			}
		}
		paramString += attributesArr[i]+'='+encodeURIComponent(currValue)+'&'; //encodeURIComponent(
	}
	paramString = paramString.substring(0, (paramString.length-1));
	sendDewemAjaxRequest( 'Workel.php', 'saveAllAttributes', formName, workelName, paramString);
}
function getPhpDewem(event, attr, phpAreaName){
	var phpArea = createPhpAreaDewem(phpAreaName);
	phpArea.style.top = getYDewem(event)+'px';
	phpArea.style.left = getXDewem(event)+'px';
	phpArea.style.visibility = 'visible';
	sendDewemAjaxRequest('Workel.php', 'getPhp', phpAreaName, attr);
}

function deleteDewem(parentFormName, toDeleteName, art){
	//'WorkelForm_phpTag_Elscript', 'script_phpTag', 'workel'
	if ( !confirm('Are you sure to delete the '+art+' \"'+ toDeleteName+'\"?') ){
		return;
	}
	var classToDel = 'Area.php';
	if (art == 'workel'){
		classToDel = 'Workel.php';
	}else if (art == 'page'){
		classToDel = 'Page.php';
	}
	sendDewemAjaxRequest(classToDel, 'deleteDewem', parentFormName, toDeleteName);
	var innerMenuId = parentFormName+'_innerMenu';
	var innerMenu = document.getElementById(innerMenuId);
	innerMenu.style.visibility = 'hidden';
}
function deleteImpossibleDewem(parentFormName, toDeleteName, art, names){
	//'WorkelForm_phpTag_Elscript', 'script_phpTag', 'workel'
	if (names != ''){
		var confirmMess = 'The '+art+' '+toDeleteName
			+' is the internal of others areas und/or the base of another areas/pages,'
			+' that could to cause the Problems with them. The Dependence List: '+names ;
		confirmMess = confirmMess+'. Sorry, you cannot delete the '+art+' \"'+ toDeleteName
			+'\"! Delete it in the dependence list first.';
		alert(confirmMess) ;
		var errs = document.getElementById('outStandardErrorDewem').innerHTML ;
		document.getElementById('outStandardErrorDewem').innerHTML = errs + '<br/><b><font color="red">' 
			+ confirmMess + '</font></b>';
		return;
	}
}

function deleteImpossibleReleasedDewem(parentFormName, toDeleteName, art){
	//'WorkelForm_phpTag_Elscript', 'script_phpTag', 'workel'
		var confirmMess = 'The '+art+' '+toDeleteName +' is released.'
			+'  First you should to lock it in the inner menu of the page!' ; 
		confirmMess = 'Sorry, you cannot delete the '+art+' '+ toDeleteName+'. '+confirmMess;
		alert(confirmMess) ;
		var errs = document.getElementById('outStandardErrorDewem').innerHTML ;
		document.getElementById('outStandardErrorDewem').innerHTML = errs + '<br/><b><font color="red">' 
			+ confirmMess + '</font></b>';
		return;
}

function deletedAlertDewem(parentFormName, success, deleted){
	if (deleted == '1'){
//		var parentForm = document.getElementById(parentFormName);
//		parentForm.parentNode.removeChild(parentForm);
//		var innerMenuId = parentFormName+'_innerMenu';
//		var innerMenu = document.getElementById(innerMenuId);
//		innerMenu.parentNode.removeChild(innerMenu);
		
		var cssSelector = '.'+ parentFormName;
		var cssStyleSel = cssSelector + '_Attrstyle';
//var phpSelector = cssSelector +'_PhpScript';
		$(cssSelector).remove();
		$(cssStyleSel).remove();
	}else{
		document.getElementById(parentFormName+'_innerMenu').style.visibility = 'hidden';
	}
	alert(success);
}
function confirmidNotToReleaseDewem(inputValueName){
	var checkboxField = document.getElementById(inputValueName);
	if (checkboxField.checked){
		var rezValue = checkboxField.value;
	}else{
		var rezValue = '';
	}
	setAttributeValueDewem(inputValueName, rezValue);
}

function runWorkelInit(formName){
	var formNameArr = formName.split('_');
	var formNameAttributes = '#'+jq(formName)+'>.workelAttributes';
	var formNameTabs = '#'+formName+'_Tabs';
	if ( selectedTabsDewem[formName] ){
		var selectedTab = selectedTabsDewem[formName];
	}else{
		var selectedTab = 0;
	}
	var formNameTabs1 = '#'+jq(formName)+'_Tabs1';
	var formNameTabs2 = '#'+jq(formName)+'_Tabs2';
	var eventsArr = Array('onclick', 'ondblclick', 'onmousedown', 'onmousemove', 'onmouseout',
			'onmouseover', 'onmouseup', 'onkeydown', 'onkeypress', 'onkeyup', 'onabort',
			'onblur', 'onchange', 'onfocus', 'onreset', 'onselect', 'onsubmit', 'onload',
			'onunload');
	var textArr = Array('font-family', 'font-size', 'font-weight', 'font-style', 'font',
			'color', 'text-transform', 'text-decoration', 'letter-spacing', 'word-spacing',
			'line-height', 'text-align', 'vertical-align', 'direction'
	);
	var bckgr = Array('background-color', 'background', 'background-image', 'background-repeat',
			'background-position', 'background-attachment', 'opacity'
	);
	var boxArr = Array('width', 'height', 'top', 'right', 'bottom', 'left', 'margin-top', 
			'margin-right', 'margin', 'margin-bottom', 'margin-left', 'padding-top', 'padding',
			'padding-right', 'padding-bottom', 'padding-left', 'min-height', 
			'max-height', 'min-width', 'max-width'
	);
	var borderArr = Array('border-top-width', 'border',
			'border-right-width', 'border-bottom-width', 'border-left-width', 'border-top-color',
			'border-right-color', 'border-bottom-color', 'border-left-color', 'border-top-style',
			'border-right-style', 'border-bottom-style', 'border-left-style'
	);
	var maketArr = Array('position', 'display', 'visibility', 'z-index', 'overflow-x', 'overflow',
			'overflow-y', 'white-space', 'clip', 'float', 'clear'
	);

	if (formNameArr.length == 3){
		//base attributes + events
		var formNameRows = formNameTabs2+'>.workelAttributeRow';
		var workelTabs = '<div id="'+formName+'_Tabs" ><ul><li><a href="#';
		workelTabs = workelTabs + formName+'_Tabs1">Attributes</a></li><li><a href="#'+formName+'_Tabs2">Events</a></li></ul>';
		workelTabs = workelTabs +'<div id="'+formName+'_Tabs1"></div><div id="'+formName+'_Tabs2"></div></div>';
		var attributes = $(formNameAttributes).html();
		$(formNameAttributes).html(workelTabs);
		$(jq(formNameTabs)).tabs( {selected: selectedTab } );
		$(formNameTabs2).html(attributes);
		var allRows = $(formNameRows);
		for( var i=0; i < allRows.length; i++ ){
			if ( !eventsArr.in_array( $(allRows[i]).children('.workelAttribute').html() ) ){
				$(allRows[i]).detach().appendTo(formNameTabs1);
			} 
		}
	}else{
		//style attributes
		var formNameTabs3 = '#'+formName+'_Tabs3';
		var formNameTabs4 = '#'+formName+'_Tabs4';
		var formNameTabs5 = '#'+formName+'_Tabs5';
		var formNameTabs6 = '#'+formName+'_Tabs6';
		var formNameRows = formNameTabs6+'>.workelAttributeRow';
		var workelTabs = '<div id="'+formName+'_Tabs" ><ul><li><a href="#';
		workelTabs = workelTabs + formName+'_Tabs1">Text</a></li><li><a href="#'+formName+'_Tabs2">Background</a></li>';
		workelTabs = workelTabs + '<li><a href="#'+formName+'_Tabs3">Box</a></li>';
		workelTabs = workelTabs + '<li><a href="#'+formName+'_Tabs4">Border</a></li>';
		workelTabs = workelTabs + '<li><a href="#'+formName+'_Tabs5">Model</a></li>';
		workelTabs = workelTabs + '<li><a href="#'+formName+'_Tabs6">Another</a></li></ul>';
		workelTabs = workelTabs +'<div id="'+formName+'_Tabs1"></div><div id="'+formName+'_Tabs2"></div>';
		workelTabs = workelTabs +'<div id="'+formName+'_Tabs3"></div><div id="'+formName+'_Tabs4"></div>';
		workelTabs = workelTabs +'<div id="'+formName+'_Tabs5"></div><div id="'+formName+'_Tabs6"></div>';
		workelTabs = workelTabs +'</div>';
		var attributes = $(formNameAttributes).html();
		$(formNameAttributes).html(workelTabs);
		$(formNameTabs).tabs( {selected: selectedTab } );
		$(formNameTabs6).html(attributes);
		var allRows = $(formNameRows);
		for( var i=0; i < allRows.length; i++ ){
			if ( textArr.in_array( $(allRows[i]).children('.workelAttribute').html() ) ){
				$(allRows[i]).detach().appendTo(formNameTabs1);
				continue;
			}
			if ( bckgr.in_array( $(allRows[i]).children('.workelAttribute').html() ) ){
				$(allRows[i]).detach().appendTo(formNameTabs2);
				continue;
			}
			if ( boxArr.in_array( $(allRows[i]).children('.workelAttribute').html() ) ){
				$(allRows[i]).detach().appendTo(formNameTabs3);
				continue;
			}
			if ( borderArr.in_array( $(allRows[i]).children('.workelAttribute').html() ) ){
				$(allRows[i]).detach().appendTo(formNameTabs4);
				continue;
			}
			if ( maketArr.in_array( $(allRows[i]).children('.workelAttribute').html() ) ){
				$(allRows[i]).detach().appendTo(formNameTabs5);
				continue;
			}			
		}
	}

}

function bindColorPicker(cpickerId){
	cpickerId = '#'+cpickerId;
	$(cpickerId).ColorPicker({
		onSubmit: function(hsb, hex, rgb) {
			$(cpickerId).val('#'+hex);
			$('.colorpicker').css('display', 'none');
			$(cpickerId).keyup();
		},
		onBeforeShow: function () {
			var colorColPic = 'ff0000';
			if ( $(cpickerId).val().substr(0,1) == '#'){
				colorColPic = $(cpickerId).val().substr(1);
			}
			$(cpickerId).ColorPickerSetColor( colorColPic);
			zIndexGlobalDewem = zIndexGlobalDewem+1;
			$('.colorpicker').css('zIndex', zIndexGlobalDewem);
		}
		}).bind('keyup', function(){
			$(cpickerId).ColorPickerSetColor($(cpickerId).val());
		});
}

