var timeout = 500;
var closetimer = null;
var ddmenuitem = 0;

function dropDown()
{
	// Removing the submenus
	dojo.query('.submenu').style("display", "none");
    
	// Setting the onmouseover and onmouseout on the dropdownmenus
	dojo.query('.submenu')
		.connect('onmouseenter', function(){
        	//mcancelclosetime();
        })
        .connect('onmouseleave', function(){
			//mclosetime();
        });

	// Setting the onmouseover and onmouseout on the navigation
	dojo.query('#menu ul li:has(ul)')
        .connect('onmouseenter', function(){
			dojo.query('ul:first', this).style('display', 'block');
        })
        .connect('onmouseleave', function(){
			dojo.query('ul:first', this).style('display', 'none');
        });
		
	// Setting the onmouseover and onmouseout on the searchbtn
	dojo.query('.search_item')
		.connect('onmouseenter', function(){
			this.style.backgroundColor = "#bccbd3";
        })
        .connect('onmouseleave', function(){
        	this.style.backgroundColor = "#FFFFFF";
        });

	// Setting the onmouseover and onmouseout on the downloadlink
	dojo.query('.download_link')
		.connect('onmouseenter', function(){
			download_box();
			dojo.query('.download_files').style("visibility", "visible");
			dojo.query('.download_files').style("display", "block");
        })
        .connect('onmouseleave', function(){
        	dojo.query('.download_files').style("display", "none");
        });
	
	// Setting the onmouseover and onmouseout on the downloadbox
	dojo.query('.download_files')
		.connect('onmouseenter', function(){
			dojo.query('.download_files').style("display", "block");
		})
		.connect('onmouseleave', function(){
		   	dojo.query('.download_files').style("display", "none");
		});
	
	// Check if download_files
	if(dojo.byId("download_files")){
		// Getting the coords
		var box_height = dojo.byId("download_files").offsetHeight;
		var link_top = findPos(dojo.byId("download_link"))[1];
		
		// Calculating the top
		var top = link_top - box_height;

		// Setting the new styles
		dojo.byId("download_files").style.top = top + "px";
	}
}

// Open the submenu
function mopen(node) {
	// cancel close timer
	mcancelclosetime();
	
	// close old layer
	if(ddmenuitem) ddmenuitem.style.display = 'none';

	// get new layer and show it
	ddmenuitem = node;
	ddmenuitem.style.display = 'block';
}

// Cancel close timer
function mcancelclosetime() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// Go close timer
function mclosetime() {
	closetimer = window.setTimeout(mclose, timeout);
}

// Close showed layer
function mclose() {
	if(ddmenuitem) ddmenuitem.style.display = "none";
}






function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
					curleft += obj.offsetLeft;
					curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
		

function download_box(){
	// Getting the coords
	var coords = dojo.coords("download_link", true);

	// Calculating the left
	var width = dojo.byId("download_link").offsetWidth;
	var x = findPos(dojo.byId("download_link"))[0];
	var left = x - (180 - width);

	// Setting the new styles
	dojo.byId("download_files").style.left = left + "px";
}

function sonitor_search()
{
	dojo.query('.text').connect('onfocus', function(){
		if(this.value == "Search"){
			this.value = "";
		}
	}).connect('onblur', function(){
		if(this.value == ""){
			this.value = "Search";
		}
	});
	
	var search_btn = dojo.byId("search_btn");
	search_btn.onclick = function(){
		var value = dojo.byId("search_field").value;
		if(value == "Search" || value == ""){
			return false;
		} else {
			return true;
		}
	}
}

function sonitor_change_news(page, total){
	var page = parseInt(page);
	var total = parseInt(total);
	if(page >= 1 && page <= total){
		ajaxresponse("/api/ajax/type/newscontent?page=" + page, "news_change", true);
	}
	return false;
}

function Hook_Image_Slideshow(imgarray, divid, fadeIn, fadeOut, delay, width, height)
{
	// Require
	dojo.require("dojo._base.fx");
	fadeIn = parseInt(fadeIn);
	fadeOut = parseInt(fadeOut);
	delay = parseInt(delay);
	
	// The div
	var divNode = dojo.byId(divid);
	divNode.innerHTML = "";
	
	// The img
	var imgNode = document.createElement("img");
	var imgId = "hook_slideshow_img";
	imgNode.id = imgId;
	imgNode.style.opacity = "0";
	imgNode.src = imgarray[0];
	
	// Width and height
	if(height > 0)
		imgNode.height = height;
	if(width > 0)
		imgNode.width = width;
	
	divNode.appendChild(imgNode);
	
	// Fadeing
	var In = dojo.fadeIn({node: imgId, duration:fadeIn });
	dojo.connect(In,"onEnd",function(){ 
		setTimeout(function(){ Hook_Image_Slideshow_Create(imgarray, imgId, 0, fadeIn, fadeOut, delay) }, delay);
	});
	In.play();
}

function Hook_Image_Slideshow_Create(imgarray, imgId, next, fadeIn, fadeOut, delay)
{
	// Fadeing out
	var Out = dojo.fadeOut({node: imgId, duration:fadeOut });
	dojo.connect(Out,"onEnd",function(){
		
		// Finding the next
		var amount = imgarray.length;
		next++;
		next = (next >= amount) ? 0 : next;
		
		// Changing the image
		dojo.byId(imgId).src = imgarray[next];
		
		// Fading In again
		var In = dojo.fadeIn({node: imgId, duration:fadeIn });
		dojo.connect(In,"onEnd",function(){ 
			setTimeout(function(){ Hook_Image_Slideshow_Create(imgarray, imgId, next, fadeIn, fadeOut, delay) }, delay);
		});
		In.play();
	});
	Out.play();
}

function Hook_Calendar(id, month, year, test)
{
	ajaxresponse("/api/ajax?type=calendar&month=" + month + "&year=" + year, id, true);
	return false;
}



function ajaxresponse(url, divid, hidePreloader) {
	document.body.style.cursor = "wait";
	if (divid != "" && hidePreloader != true) {
		dojo.byId(divid).innerHTML = '<img src="/app/design/backend/default/skin/img/preload.gif"/>';
	}
	dojo
			.xhrGet( {
				url :url,
				load : function(response, ioArgs) {

					document.body.style.cursor = "default";
					if (divid != "") {
						dojo.byId(divid).innerHTML = response;
						dojo.parser.parse(divid);
					}

					// Dojo recommends that you always return(response); to
					// propagate
					// the response to other callback handlers. Otherwise, the
					// error
					// callbacks may be called in the success case.
					return response;
				},
				error : function(response, ioArgs) {
					document.body.style.cursor = "default";
					dojo.byId(divid).innerHTML = "An error occurred, with response: "
									+ response;
					return response;
				},
				handleAs :"text"
			});
}













