/**
 * Javascript library for WuffWuff.de
 **/

/**
 * executed after an image upload with an iframe as target to put the result
 * data into the right position.
 */
function afterImageUpload(url, imgElementId) {
	var d = window.imageTarget.document;

	if (d.location.href != "about:blank") {
		var uuid = d.body.innerHTML;
		document.getElementById(imgElementId).setAttribute('src', url + uuid);
	}
}

function hide(item) {
	var elementToHide = document.getElementById(item);

    elementToHide.style.display = 'none';
	elementToHide.setAttribute('name', '');
}

function show(item) {
	var openElements = document.getElementsByName('open');
	var elementToShow = document.getElementById(item);

	for(var i = 0; i < openElements.length; i++) {
		openElements[i].style.display = 'none';
	}

	// elementToShow.style.display = '';
	Effect.Appear(item, { duration: 0.5 });
	elementToShow.setAttribute('name', 'open');
}

function showWithFadeEffect(item) {
	var openElements = document.getElementsByName('open');
	var elementToShow = document.getElementById(item);

	for(var i = 0; i < openElements.length; i++) {
		Effect.Fade(openElements[i]);
	}

	Effect.Appear(elementToShow);
	elementToShow.setAttribute('name', 'open');
}

function setRating(value) {
	document.getElementById('rateValue').setAttribute('value', value);
}

function checkCharsLeft(formName, textareaName, chars) {
	str = document.forms[formName].elements[textareaName].value;
	if (str.length >= chars-1) {
		str = str.substring(0, chars-1);
		document.forms[formName].elements[textareaName].value = str;
 	}
}

function submitEnter(event, formId) {
	var characterCode;

	if(event && event.which) {
		characterCode = event.which;
	} else {
		characterCode = event.keyCode;
	}

	if(characterCode == 13) {
		document.getElementById(formId).submit();
		return false;
	} else {
		return true;
	}
}

function setAppButtons(){

	if(document.getElementById('bigbuttons') != null){
		var x,y;
		Apps = new Array('dogowner', 'promenade', 'dogparlour', 'dogsitting');
		x = Math.floor(Math.random() * Apps.length);
		firstAppButton = 'firstbutton_'+Apps[x];

		RemainingApps = new  Array();
		for (var i = 0; i < Apps.length; i++) {
			if(i != x){
				RemainingApps.push(Apps[i]);
			}
		}
		y = Math.floor(Math.random() * RemainingApps.length);
		secondAppButton = 'secondbutton_'+RemainingApps[y];

		document.getElementById(firstAppButton).style.display = 'block';
		document.getElementById(secondAppButton).style.display = 'block';
		document.getElementById('thirdbutton_service').style.display = 'block';
	}
}

function jumpTo(){
	if(typeof(jumpToSearchresults) != null && typeof(jumpToSearchresults) == "function") {
		jumpToSearchresults();
	}
}

function emptyfield(obj, val) {
  if (obj.value == val) obj.value ='';
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
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;
}
