/* JS document common variables */

var d = document, w = window, n = navigator.userAgent, init = new Function;

/* Drag object */

var DRG = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, x, y, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o = typeof o == 'object'? o : $$(o);
		oRoot = typeof oRoot == 'object'? oRoot : $$(oRoot);
	
		o.onmousedown	= DRG.start;

		o.hmode = bSwapHorzRef? false : true;
		o.vmode = bSwapVertRef? false : true;

		o.root = oRoot && oRoot != null? oRoot : o;

		o.root.style.left = (o.hmode && !isNaN(x))? x + 'px' : o.root.offsetLeft + 'px';
		o.root.style.top = (o.vmode && !isNaN(y))? y + 'px' : o.root.offsetTop + 'px';
		o.root.style.right = (!o.hmode && !isNaN(x))? x + 'px' : 0;
		o.root.style.bottom = (!o.hmode && !isNaN(x))? y + 'px' : 0;

		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 = DRG.obj = this;
		e = DRG.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;
		}

		d.onmousemove	= DRG.drag;
		d.onmouseup		= DRG.end;

		return false;
	},

	drag : function(e)
	{
		e = DRG.fixE(e);
		var o = DRG.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)

		o.root.style[o.hmode? "left" : "right"] = nx + "px";
		o.root.style[o.vmode? "top" : "bottom"] = ny + "px";
		o.lastMouseX	= ex;
		o.lastMouseY	= ey;

		/* this writes togeter with drag action */
		o.root.onDrag(nx, ny);

		return false;
	},

	end : function()
	{
		d.onmousemove = null;
		d.onmouseup   = null;

		/* this writes end result of the drag action */
		DRG.obj.root.onDragEnd(parseInt(DRG.obj.root.style[DRG.obj.hmode? "left" : "right"]),
		 parseInt(DRG.obj.root.style[DRG.obj.vmode ? "top" : "bottom"]));

		DRG.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = w.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

/* Drag object end */

/* AJAX object */

var AJX = {

	xmlHttp: null,

	url: '',
    
    synchronous: false,

	reqXML: function ()
	{
		try 
		{
		/* Firefox, Opera 8.0+, Safari, IE7 */             
			AJX.xmlHttp = new XMLHttpRequest();
		
			if (AJX.xmlHttp.overrideMimeType)
				AJX.xmlHttp.overrideMimeType('text/xml');
		}
		catch (e) {
		/* Internet Explorer 6, 5 */
			try 
			{
			AJX.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) 
			{
				try 
				{
					AJX.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		
		AJX.xmlHttp.onreadystatechange = AJX.processReqChange;
		
		try 
		{
			AJX.xmlHttp.open('GET', AJX.url, !AJX.synchronous);
			AJX.xmlHttp.send(null);
		}
		catch (e) 
		{
			var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
			alert("Unable to get XML data:\n" + msg);
			return;
		}
		
	},
	
	processReqChange : new Function,
	
	load : new Function,
	
	rdm : function (){return Math.random() * Math.pow(10, 17);}
	
}

/* AJAX object end */

/* Check-flash-plugin-ability parameter MM_FlashCanPlay */

var MM_contentVersion = 8;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;

	if ( plugin )
	{
			var words = navigator.plugins["Shockwave Flash"].description.split(" ");
		    for (var i = 0; i < words.length; ++i)
		    {
			if (isNaN(parseInt(words[i])))
			continue;
			var MM_PluginVersion = words[i]; 
		    }
		var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
	}
	else if (n && n.indexOf("MSIE")>=0 
	   && (navigator.appVersion.indexOf("Win") != -1)) {
		d.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
		d.write('on error resume next \n');
		d.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
		d.write('</SCR' + 'IPT\> \n');
	}

/* Check-flash-plugin-ability ability parameter end */

function acXCt(){

	if (!MM_FlashCanPlay)
	{
		d.write(
			'<table cellspacing="0" width="100%" height="100">\n',
			'<tr><td align="center" style="background: #000; color: #fff; font: 12px Tahoma;">\n',
			'Вие нямате инсталиран подходящ флаш плейър, за да тази анимация.<br />\n',
			'Може да си инсталирате последна версия, като посетите страницата на <a href="http://get.adobe.com/flashplayer/" target="_blank" style="color: orange;">Adobe.com</a>\n',
			'</td></tr></table>'
		);
	}
	else
	{
		var a = acXCt.arguments,
		html = '<embed ';
	
		for (var _x = 0; _x <= a.length - 1; _x++)
			if(_x % 2)
				html += a[_x] + '" ';
			else
				html += a[_x] + '="';
	
		html += 'type="application/x-shockwave-flash" pluginspage="http://get.adobe.com/flashplayer/" />';
	
		d.write(html);
	}
	
}

function ACXobj()
{
	var html = '', a = ACXobj.arguments, aId = [];
	
	html = '<object type="application/x-shockwave-flash"';
	
	for(var x = 0; x <= a.length - 1; x++)
		switch(a[x])
		{
			case 'src':
				html += ' data=' + '"' + a[x + 1] + '"';
				aId.push(x);
				aId.push(x + 1);
			break;
			case 'width':
				html += ' width=' + '"' + a[x + 1] + '"';
				aId.push(x);
				aId.push(x + 1);
			break;
			case 'height':
				html += ' height=' + '"' + a[x + 1] + '"';
				aId.push(x);
				aId.push(x + 1);
			break;
			case 'style':
				html += ' style=' + '"' + a[x + 1] + '"';
				aId.push(x);
				aId.push(x + 1);
			break;
		}
	
	html += '>';
	
	for (var x = 0; x <= a.length - 1; x++)
		if(x % 2 && !(x in aId))
			html += 'value="' + a[x] + '" \/>';
		else if(!(x in aId))
			html += '<param name="' + a[x] + '" ';
		else if(a[x] == 'src')
			html += '<param name="movie" value="' + a[x + 1] + '" \/>'
	
	html += '<\/object>';
	
	return html;
}

/* fix IE events function */

function $f(e)
{
	return e? e : w.event;
}

/* fix IE events function end */

/* get element by class name */

function $c(_e, classId, o)
{
var a = [], o = typeof o == 'string'? $$(o) : typeof o == 'object'? o : d;
	
	for(var x = 0; x <= $$(o, _e).length - 1; x++)
		if($$(o, _e, x).className.indexOf(classId) != -1)
			a[a.length] = $$(o, _e, x);
			
	return a;
}

/* get element by class name end */

/* show-hide */

function $h(id, f)
{
	var o = typeof id == 'object'? id : $$(id);

	if(f != 1)
		o.style.display = o.style.display == 'block'? 'none' : 'block';
	else
		o.style.visibility = o.style.visibility == 'visible'? 'hidden' : 'visible';
}

/* show-hide end */

/* grabs text from node */
function $g(node, maxDepth)
{
	if(node.nodeType == 3)
		return node . nodeValue;
	else if(( 1 == node.nodeType) && (0 < maxDepth))
	{
		var result = '';
		for(var i = 0; i < node.childNodes.length; i++)
			result += $g(node.childNodes[i], maxDepth - 1);
		return result.replace(/(\s)$/,'');
	}
	else
		return;
}
/* grabs text from node end */

/* Find element function */

function $$()
{
	var a = $$.arguments;

	if(typeof a[0] == 'string' && a[0].indexOf('#') != 0)
		return eval('d.getElementById(a[0])' + (a[1] == 1? '.style' : ''));
	else if(typeof a[0] == 'string' && a[0].indexOf('#') == 0)
		return $$($$(a[0].substring(1)), a[1], a[2], a[3]);
	else if(typeof a[0] == 'object' && typeof a[1] == 'string' && typeof a[2] == 'undefined')
		return a[0].getElementsByTagName(a[1]);
	else if(typeof a[0] == 'object' && a[2] >= 0 && typeof a[3] == 'undefined')
		return a[0].getElementsByTagName(a[1])[a[2]];
	else if(typeof a[0] == 'object' && a[2] >= 0 && a[3] == 1)
		return a[0].getElementsByTagName(a[1])[a[2]].style;
	else
		return;
}

/* Find element function end */

/* Browser check class */

function BWC(){

	this.dom = d.getElementById;
	this.ie4 = d.all && !this.dom?  true : false;
	this.ns4 = d.layers? true : false;

	this.ns = n.indexOf('Netscape') > -1 && !this.ns4;
	this.ff = n.indexOf('Firefox') > -1;
	this.mz = n.indexOf('Gecko') > -1 && !this.ns && !this.ff;
	this.op = n.indexOf('Opera') > -1;
	this.ie = n.indexOf('MSIE') > -1 && !this.op && !this.ie4;
	this.ie6 = n.indexOf('MSIE 6') > -1 && !this.op && !this.ie4;

}

/* Browser check class end */

/* Check/Uncheck keyword checkboxes */
function CUK(){
    var checked = d.getElementById('check_keywords').checked;
    var a = $c('input', 'keyword-checkbox', d);
    for(var x in a){
        var parentRow = a[x].parentNode.parentNode;
        a[x].checked = checked;
        if(checked){
            parentRow.className = 'highlight';
        }else{
            if(x%2 == 0){
                parentRow.className = 'odd';
            }else{
                parentRow.className = 'even';
            }
        }
    }
}

function highlightRow(id){
    var a = d.getElementById(id);
    var parentRow = a.parentNode.parentNode;
    if('number' == typeof this.parent.rowIndex) {
        alert('You clicked row: ' + (this.rowIndex + 1));
    }
    if(a.checked){
        parentRow.className = 'highlight';
    }else{
        var rowIndex = parentRow.rowIndex;
        if(rowIndex % 2 == 0){
            parentRow.className = 'even';
        }else{
            parentRow.className = 'odd';
        }
    }
}

function changeStatus(){
    var a = $c('input', 'keyword-checkbox', d);
    var wordInput = d.getElementById('keywords');
    var multipleForm = d.getElementById('multipleForm');
    for(var x in a){
        if(a[x].checked){
            wordInput.value += a[x].value + ',';
        }
    }
    multipleForm.submit();
}
/* Check/Uncheck keyword checkboxes */
