var img_path = url_root+"admin/img/";
var site_img_path = url_root+"img/";


//////////////////////////////////////////////////////////////////////////
// RESIZE FUNCTIONS
//////////////////////////////////////////////////////////////////////////
var res_limit 		= 0;
var res_offset 		= 3;
var bar_len			= 495;
	function resize_img() {
		document.getElementById("accept_button").style.display = "none";
		setOverlay();
		req("offset="+res_offset+"&limit="+res_limit,r_resize_img,"index.php?page=img_resize");
	}

	function r_resize_img(r) {
			var p = getHeader(r,"proz");
			document.getElementById("bar").innerHTML = p + " %";
			document.getElementById("bar").style.backgroundPosition = (bar_len*-1)+(bar_len * p) / 100 + "px";
			res_limit+=res_offset;
			if(getHeader(r,"stop")){
				document.getElementById("bar").innerHTML = "100 %";
				document.getElementById("bar").style.backgroundPosition = "0px";
				document.getElementById("accept_button").style.display = "block";
				res_limit 		= 0;
				res_offset 		= 10;
				bar_len			= 495;
				compareTableWithFolder();
			}
			else
				resize_img();
	}

	function compareTableWithFolder() {
		document.getElementById("accept_button").style.display = "none";
		req("com=1",r_compareTableWithFolder,"index.php?page=img_resize");
	}

	function r_compareTableWithFolder(r){
		document.getElementById("pic_error").style.display = "block";
		writeHtml("pic_error",getBody(r));
		document.getElementById("accept_button").style.display = "block";
	}

//////////////////////////////////////////////////////////////////////////
// PNG FUNCTIONS
//////////////////////////////////////////////////////////////////////////
	window.onload = function() {
      //if(document.all) init_png();
    }

    function init_png() {
    	var blank = new Image;
    	blank.src = 'img/spacer.gif';
    	var imgs = document.getElementsByTagName("img");
    	for (var i = imgs.length; --i >= 0;) {
    		var img = imgs[i];
    		var src = img.src;
    		if (!/\.png$/.test(src))
    			continue;
    		var s = img.runtimeStyle;
    		s.width = img.offsetWidth + "px";
    		s.height = img.offsetHeight + "px";
    		s.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
    		img.src = blank.src;
    	}
    }


	function setSort(e) {
		req(Sortable.serialize(e.id));
	}

	function setPicSort(e){
		req(Sortable.serialize(e.id)+"&m=1");
	}

	function r_setSort(e) {
		writeHtml("wrapper",getBody(e));
	}



//////////////////////////////////////////////////////////////////////////
// HELPER FUNCTIONS
//////////////////////////////////////////////////////////////////////////
var url ="";
	function req(params,resp,url) {
			if(!url) url = 	"action.aj.php";
			new Ajax.Request(url,
							   {asynchronous:true,
								evalScripts:true,
								parameters:params,
								method:'post',
                                onComplete:resp});
    }
	function writeHtml(i,c) {
		if(document.getElementById(i)) document.getElementById(i).innerHTML=c;
	}
	function changeClass(i,c) {
		if(document.getElementById(i)) document.getElementById(i).className=c;
	}
	function get_value_from_select(i) {
		select_p = document.getElementById(i)

        return multipleWert(select_p, '|');
	}
    function multipleWert(sObj, trenn) {
        var rVal = '';
        for (var i=0; i<sObj.options.length; i++) with (sObj.options[i])
          if (selected) rVal += trenn + value;
        return rVal.substring(trenn.length);
    }
	function getHeader(r,i) {
		return r.getResponseHeader(i);
	}
	function getBody(r) {
		return r.responseText;
	}

	function no_display(id){
		document.getElementById(id).style.display = "none";
		document.getElementById('overlay').style.display = "none";
		return false;
	}

	var t = null;
	function showTip(text,w)
	{
		t = document.getElementById("tooltip");
		t.style.display = "block";
		t.style.left 	= (m_x + 20) + "px";
		t.style.top 	= (m_y - 0) + "px";
		t.innerHTML		= text;
	}

	function hideTip(h_id){
		document.getElementById(h_id).style.display = "none";
		return false;
	}

	////////////////////////////////////////////////////////
	/// GET X AND Y MAUS
	document.onmousemove = updateMousePosition;
	function updateMousePosition(e) {
		m_x = (document.all) ? window.event.clientX + document.documentElement.scrollLeft : e.pageX;
		m_y = (document.all) ? window.event.clientY + document.documentElement.scrollTop  : e.pageY;
		if (t != null) {
				t.style.left = (m_x + 20) + "px";
				t.style.top  = (m_y + 20) + "px";
		}
	}

	function showCat(type,parent_id,n_id){
		req("type="+type+"&parent="+parent_id+"&id="+n_id,r_showCat);
		return false;
	}

	function r_showCat(r){
		l = document.getElementById("showCat");
		l.innerHTML = getBody(r);
		l.style.display = "block";
		l.style.left 	= (m_x + 20) + "px";
		l.style.top 	= (m_y - 30) + "px";
	}


	/////////////////////////////////////////////////////////////
	// GET PAGE SIZE
	function getPageSize()
	{

		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}


		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	}

	function getPageScroll(){

		var yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}

		arrayPageScroll = new Array('',yScroll)
		return arrayPageScroll;
	}

	function setOverlay() {
		var arrayPageSize 											= getPageSize();
		var arrayPageScroll 										= getPageScroll();
		var position 												= arrayPageScroll[1] + (arrayPageSize[3] / 2 - 100);
		document.getElementById('overlay').style.height 			= arrayPageSize[1]+"px";
		document.getElementById('overlay').style.width	 			= arrayPageSize[0]+"px";
		document.getElementById('overlay').style.display			= "block";
		document.getElementById('progressbar').style.top 			= position + "px";
		document.getElementById('progressbar').style.left 			= (arrayPageSize[2]/2 - 250)+"px";
		document.getElementById('progressbar').style.display 		= "block";
	}

	var elem;
	function changeStatus(s_id){
		elem = document.getElementById("flag_"+s_id);
		req("s_id="+s_id,r_changeStatus);
		return false;
	}

	function r_changeStatus(r){
		elem.src = img_path+"icons/"+getBody(r);
	}

	function showImg() {
		var arrayPageSize 											= getPageSize();
		var arrayPageScroll 										= getPageScroll();
		var position 												= arrayPageScroll[1] + (arrayPageSize[3] / 2 - 100);
		document.getElementById('overlay').style.height 			= arrayPageSize[1]+"px";
		document.getElementById('overlay').style.width	 			= arrayPageSize[0]+"px";
		document.getElementById('overlay').style.display			= "block";

		return false;
	}


	var n_limit = 0;
	var n_offset = 5;
	function sendNewsletter()
	{
		id = document.getElementById("news_id").value;
		setOverlay();
		req("submit=1&news_id="+id+"&limit="+n_limit+"&offset="+n_offset,r_sendNewsletter,"index.php?page=newsletter");
		return false;
	}

	function r_sendNewsletter(r) {
			//writeHtml("wrapper",getBody(r));
			n_limit+=n_offset;
			var p = getHeader(r,"proz");
			if(!p){
				alert("Kann keine Newsletter verschicken, da keine E-mail Adressen vorhanden sind.");
				document.getElementById("accept_button").style.display = "block";
				return;
			}
			document.getElementById("bar").innerHTML = p + " %";
			document.getElementById("bar").style.backgroundPosition = (bar_len*-1)+(bar_len * p) / 100 + "px";

			if(getHeader(r,"mail_error"))
				alert("Das verschicken der Mails ist fehlgeschlagen - Bitte versuchen Sie es neu");
			if(getHeader(r,"stop")){
				document.getElementById("accept_button").style.display = "block";
				n_limit = 0;
				return;
			}
			else {
				sendNewsletter();
			}
	}
