
actid = 0;
BlindDuration = 0.4;
FadeDuration = 0.4;
atool = '';
acttool = '';
feedbackText = '';
		
IPEColor = '#dddddd';
IPEEColor = '#fafafa';


window.onload = function (){
	id = getCookie("actid");
	acttool = getCookie("atool");
	if (atool == acttool && id != '')
		toggleDataDiv(id);

	if($('alloverlay') !=  null){
		var arrayPageSize = getPageSize();
		Element.setHeight('alloverlay', arrayPageSize[1]);
		new Effect.Fade('alloverlay', {duration: FadeDuration, queue: 'front'});
	}
	navLinks = document.getElementsByTagName('a');
	if(navLinks)
		for (i=0; i<navLinks.length; i++) {
			node = navLinks[i];
			node.onfocus=function(){
				if(this.blur)this.blur();
			}
		}
}

toggleDataDiv = function(id){
	if($('divdata_' + id) == null)
		return false;
		
	if($('toolbar').style.display != 'none')
		Effect.toggle('toolbar','blind', {duration:0.1});
		
	if(actid){
		new Effect.BlindUp('divdata_' + actid, {duration: BlindDuration, queue: 'end'});
		$('divhead_' + actid).className = 'divhead';
	}
	document.cookie = "atool=" + atool;
	if(actid != id){
		document.cookie = "actid=" + id;
		new Effect.BlindDown('divdata_' + id, {duration: BlindDuration, queue: 'end'});
		$('divhead_' + id).className = 'divhead highlight';
		actid = id;
	}else{
		actid = 0;
		document.cookie = "actid=0";
	}
}

getCookie = function(name){
	var i = 0;//SuchpositionimCookie
	var suche = name + "=";
	while(i < document.cookie.length){
		if(document.cookie.substring(i,i+suche.length) == suche){
			var ende = document.cookie.indexOf(";",i+suche.length);
			ende = (ende >- 1) ? ende : document.cookie.length;
			var cook = document.cookie.substring(i+suche.length,ende);
			return unescape(cook);
		}
		i++;
	}
	return null;
}

confirmLink = function (theLink, confirmQuery){
	if (confirmQuery == '' || typeof(window.opera) != 'undefined')
		return true;

	if (confirmQuery)
		var is_confirmed = confirm(confirmQuery);
	
    if (is_confirmed || !confirmQuery)
        theLink.href += '&is_js_confirmed=1';

    return is_confirmed;
} 

feedbackMessage = function (text, iserror){
	if(text){
		if(iserror)
			Element.addClassName('feedbackMessage', 'messageError');
		else
			Element.addClassName('feedbackMessage', 'messageAccept');

		$('feedbackText').innerHTML = text;
		new Effect.Appear('alloverlay', {to: 0.9, duration: FadeDuration, delay:1.0, queue: 'end'});
		new Effect.Appear('feedbackMessage', {duration: FadeDuration, queue: 'end'});
		var arrayPageSize = getPageSize();
		window.setTimeout('Element.setHeight(\'alloverlay\', arrayPageSize[1]);', 1000);
		window.setTimeout('closeFeedbackMessage();', 10000);
	}
}

closeFeedbackMessage = function (){
	new Effect.Fade('alloverlay', {duration: FadeDuration});
	new Effect.Fade('feedbackMessage', {duration: FadeDuration});
}

editCompleteFunction = function (transport, element){
	var rt = transport.responseText;
	var starttag = '<script>';
	var endtag = '</script>';
	ct_start = rt.indexOf(starttag);
	if( ct_start != -1) {
		ct_start = ct_start + starttag.length;
		ct_end = rt.indexOf(endtag) - ct_start;;
		ct = rt.substr(ct_start, ct_end);
		feedbackMessage(ct);
	}
	new Effect.Highlight(element, {startcolor: IPEColor}); 
}

myAjaxUpdater = function(element, cat, val, oval, session) {
	document.getElementById(element).innerHTML = 'Loading ...';
	url = 'inplaceedit.inc.php?' + cat + '=' + val + '&oval=' + oval + session;
	new Ajax.Updater(element, url, {asynchronous:true});
}

checkproduktform = function(form){
	if(!form.hersteller_id.value){
		alert("Bitte geben Sie einen Hersteller an!");
		form.hersteller_id.focus();
		return false;
	}
	if(!form.distributor_id.value && !form.grund.value){
		alert("Bitte geben Sie einen Distributor an!");
		form.distributor_id.focus();
		return false;
	}
	if(!form.kunden_id.value){
		alert("Bitte geben Sie einen Kunden an!");
		form.kunden_id.focus();
		return false;
	}
	if(!form.datum.value){
		alert("Bitte geben Sie ein Datum an!");
		form.datum.focus();
		return false;
	}
}

getPageSize = function(){
	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;
}

Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

Ajax.MyInPlaceCollectionEditor = Class.create();
Object.extend(Ajax.MyInPlaceCollectionEditor.prototype, Ajax.InPlaceEditor.prototype);
Object.extend(Ajax.MyInPlaceCollectionEditor.prototype, {
  createEditField: function() {
    if (!this.cached_selectTag) {
      var selectTag = document.createElement("select");
      //selectTag.setAttribute('size', 3);
      var collection = this.options.collection || [];
      var optionTag;
      collection.each(function(e,i) {
        optionTag = document.createElement("option");
        optionTag.value = (e instanceof Array) ? e[0] : e;
        if((typeof this.options.value == 'undefined') && 
          ((e instanceof Array) ? this.element.innerHTML == e[1] : e == optionTag.value)) 
          	optionTag.selected = true;
          	
        if(this.options.value==optionTag.value) 
        	optionTag.selected = true;
        	
        optionTag.appendChild(document.createTextNode((e instanceof Array) ? e[1] : e));
        selectTag.appendChild(optionTag);
      }.bind(this));
      this.cached_selectTag = selectTag;
    }

    this.editField = this.cached_selectTag;
    if(this.options.loadTextURL) this.loadExternalText();
    this.form.appendChild(this.editField);
    //this.options.callback = function(form, value) {
    //  return "value=" + encodeURIComponent(value);
    //}
  }
});

// Enable/Disable next/previous buttons
function buttonStateHandler(button, enabled) {
	if (button == "prev-arrow") 
		$('prev-arrow').className = enabled ? "arrowbuttonenabled" : "arrowbuttondisabled"
	else 
		$('next-arrow').className = enabled ? "arrowbuttonenabled" : "arrowbuttondisabled"
}

// Anim effects before and after scrolling
function animHandler(carouselID, status, direction) {
  var region = $(carouselID).down(".carousel-clip-region")
  if (status == "before") {
    Effect.Fade(region, {to: 0.3, queue: { position:'end', scope: "carousel" }, duration: 0.2})
  }
  if (status == "after") {
    Effect.Fade(region, {to: 1, queue: { position:'end', scope: "carousel" }, duration: 0.2})
  }
}

// Show/hide "loading" overlay before and after ajax request
function ajaxHandler(carousel, status) {
  var overlay = $('overlay');
  if (status == "before") {
    if (overlay) {
      overlay.setOpacity(0);
      overlay.show();
      Effect.Fade(overlay, {from: 0, to: 0.8, duration: 0.2})
    }
    else
      new Insertion.Top("ajax-carousel", "<div id='overlay' >Loading...<br><img src='../images/ajax-loader.gif'></div>");
  }
  else 
    Effect.Fade(overlay, {from: 0.8, to: 0.0, duration: 0.2})
}
