var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

function moveCenterScreen(objID){
	
	var innerHeight_ = window.innerHeight ? window.innerHeight : document.documentElement.offsetHeight;
	var obj = document.getElementById(objID);
	
	obj.style.left = ( document.body.clientWidth / 2 - obj.clientWidth / 2 ) + 'px';
	obj.style.top = ( document.documentElement.scrollTop + innerHeight_ / 2 - obj.clientHeight / 2 ) + 'px';
	
}

function showMessage()
{
	
	document.getElementById('screenlayer').style.height = getDocumentHeight()+'px';
	document.getElementById('screenlayer').style.display="";
	
	document.getElementById('messagelayer').style.display="";
	moveCenterScreen('messagelayer');
	
	Drag.init(document.getElementById('messagelayer'));
	
	document.getElementById('messagelayer_close').focus();
}

function showFormLayer()
{
	
	document.getElementById('screenlayer').style.height = getDocumentHeight()+'px';
	document.getElementById('screenlayer').style.display="";
	
	document.getElementById('formlayer').style.display="";
	moveCenterScreen('formlayer');
	
	//Drag.init(document.getElementById('formlayer'));
	
	document.getElementById('formlayer_close').focus();
}

function messageRestore()
{
	
	_ajx_callFunction('xajax','RunXajax','ajx_dropMessage','');
	
	document.getElementById('messagelayer').style.display='none';
	
	if(document.getElementById('formlayer').style.display=='none'){
		
		document.getElementById('screenlayer').style.display='none';
	}
}

function formLayerRestore()
{
	if(document.getElementById('messagelayer').style.display!='none'){
		
		messageRestore();
	}
	
	document.getElementById('formlayer').style.display='none';
	document.getElementById('screenlayer').style.display='none';
}



function highlight_div(checkbox_node)
{
    label_node = checkbox_node.parentNode;
    if (checkbox_node.checked)
    {
	    label_node.style.backgroundColor='#0a246a';
	    label_node.style.color='#fff';
    }
    else
    {
        label_node.style.backgroundColor='#fff';
        label_node.style.color='#000';
    }
}

function getDocumentHeight()
{
	if( document.height )
	{
		return document.height;
	}
	else if( document.body.clientHeight )
	{
		return document.body.clientHeight;
	}
	return 0;
};


// Expand UL menu tree - Fix for IE
sfHover = function() {
	//var sfEls = document.getElementById(id).getElementsByTagName('LI');
	var sfEls = this.getElementsByTagName('LI');
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=' sfhover';
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"),'');
		}
	}
}


var tnyTextareasArr = new Array;
function setTextareaToTinyMCE(sEditorID) {
	var oEditor = document.getElementById(sEditorID);
	if(oEditor && (tnyTextareasArr[sEditorID]=='undefined' || !tnyTextareasArr[sEditorID])) {
		tinyMCE.execCommand('mceAddControl', true, sEditorID);
		tnyTextareasArr[sEditorID] = true;
	}
	return;
}
function unsetTextareaToTinyMCE(sEditorID) {
	var oEditor = document.getElementById(sEditorID);
	if(oEditor && tnyTextareasArr[sEditorID]!='undefined' && tnyTextareasArr[sEditorID]) {
		tinyMCE.execCommand('mceRemoveControl', true, sEditorID);
		tnyTextareasArr[sEditorID] = false;
	}
	return;
}

function addEditor(id) {
	
	if (!tinyMCE.get(id)) tinyMCE.execCommand('mceAddControl', false, id);
}

function removeEditor(id) {
	
	if (tinyMCE.get(id)) tinyMCE.execCommand('mceRemoveControl', false, id);
}

function toggleEditor(id){
	if (!tinyMCE.get(id)) tinyMCE.execCommand('mceAddControl', false, id);
	else tinyMCE.execCommand('mceRemoveControl', false, id);
}

function addToFavorites() {

	if (window.sidebar) { // Mozilla Firefox Bookmark
		
		window.sidebar.addPanel(document.title, document.location.href,"");
	}
	else if( window.external ) { // IE Favorite
		
		window.external.AddFavorite(document.location.href, document.title);
	}
	else if(window.opera && window.print) { // Opera Hotlist
		return true;
	}
	else{
		
		alert("Sorry! Your browser doesn't support this function.");
	}
 }
 
function addToFavorites() {
	
	var title = document.title;
	var url = document.location.href;
	alert(title);
	if (window.sidebar) { // firefox
		
		window.sidebar.addPanel(title, url, "_top");
	}
	else if (document.all) { // IE
		
		window.external.AddFavorite(url, title);
	}
	else if (window.opera && window.print) { // opera
		
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
}

$.debugObject = function(obj, replace){
	
	var data = '';
	
	$.each(obj, function(i, n){
		data = data+"<p>Name: "+i+", Value: "+n+'</p>';
	});
	
	if(replace){
		
		$('#xajax_debug').attr('innerHTML', data);
	}
	else{
		
		$('#xajax_debug').append(data);
	}
}

submitAjaxForm = function(plugin, controller, action, form_id){
	
	tinyMCE.triggerSave();
	
	$('#'+form_id).validate();
	
	if($('#'+form_id).valid()){
		
		_ajx_callFunction(plugin, controller, action, xajax.getFormValues(form_id));
	}
	
	return false;
}


$.sevenSearch = function(submit){
	
	if($('#main_search_part').attr('value')=='catalogue'){
		
		$('#main_search').attr('action', '/shop/');
	}
	else{
		
		$('#main_search').attr('action', '/search/');
	}
	
	
	if(submit) $('#main_search').submit();
}

/*IE select box disable property emulation*/
/*
window.onload = function() {
	
	if (document.getElementsByTagName) {
		
		var s = document.getElementsByTagName("select");

		if (s.length > 0) {
			
			window.select_current = new Array();

			for (var i=0, select; select = s[i]; i++) {
				
				select.onfocus = function() {
					
					window.select_current[this.id] = this.selectedIndex;
				}
				
				select.onchange = function() {
					
					restoreDisabledOption(this);
				}
				
				emulateDisableOptionFunctionality(select);
			}
		}
	}
}

function restoreDisabledOption(s) {
	
	if (s.options[s.selectedIndex].disabled) {
		
		s.selectedIndex = window.select_current[s.id];
	}
}

function emulateDisableOptionFunctionality(s) {
	
	for (var i=0, option; option = s.options[i]; i++) {
		
		if (option.disabled) {
			
			option.style.color = "graytext";
		}
		else {
			
			option.style.color = "menutext";
		}
	}
}
*/



