/* * *
 * JavaScript by Tilo Kussatz, copyright 2003-09.
 * Originally written for http://www.kussatz.com/.
 *
 * Feel free to borrow and modify this code, but
 * be sure to leave this credit in the source!
 */


var leftArrow = '<span class="arrow">&#187;<\/span> ';
var rightArrow = ' <span class="arrow">&#171;<\/span>';

var activeXHRs = 0;

function showDescription(text) {
	if (document.getElementById)
		document.getElementById("whatsthis").innerHTML = leftArrow + text + rightArrow;
}

function clearDescription() {
	if (document.getElementById)
		document.getElementById("whatsthis").innerHTML = "&nbsp;";
}

function highlightTagButton(button) {
	for (i=0; i<tm.length; i++) {
		if (tm[i][0] != button) {
			document.getElementById('bl_'+tm[i][0]).style.background = '#232323';
			document.getElementById('bl_'+tm[i][0]).style.color = '#f5cb4c';
		}
		else {
			document.getElementById('bl_'+tm[i][0]).style.background = '#f5cb4c url(http://www.kussatz.com/media/bg6.png)';
			document.getElementById('bl_'+tm[i][0]).style.color = '#232323';
		}
	}
}

function unlightMenuButtons() {
	for (i=0; i<tm.length; i++) {
		document.getElementById('bl_'+tm[i][0]).style.background = '#232323';
		document.getElementById('bl_'+tm[i][0]).style.color = '#ffe661';
		document.getElementById('bl_'+tm[i][0]).style.borderColor = '#232323';
	}
}

function highlightItemButton(button) {
	for (i=0; i<ip.length; i++) {
		if (ip[i][0] != button) {
			els = getElementsByClassName(document, 'a', 'bl')
			for (i=0; i<els.length; i++) {
				els[i].style.background = '#232323';
				els[i].style.color = '#f5cb4c';
			}
		}
	}
	button.style.background = '#f5cb4c url(http://www.kussatz.com/media/bg6.png)';
	button.style.color = '#232323';
}

function unlightItemButtons() {
	for (i=0; i<ip.length; i++) {
		els = getElementsByClassName(document, 'a', 'bl')
		for (i=0; i<els.length; i++) {
			els[i].style.background = '#232323';
			els[i].style.color = '#ffe661';
			els[i].style.borderColor = '#232323';
		} 
	}
}

function highlightItemButtons(item) {
	for (i=0; i<ip.length; i++) {
		els = getElementsByClassName(document, 'a', 'bl');
		for (i=0; i<els.length; i++) {
			if (els[i].href.indexOf(item) != '-1')
				highlightItemButton(els[i]);
		} 
	}
}


function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all :
        oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];     
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

var thumbnail_ids;
var slideshow_timer;

function showMenu(menu) {
	
	highlightTagButton(menu);
	
	// load content
	getTagItems(menu);

  // hide other menus
	for (i=0; i<tm.length; i++) {
		if (tm[i][0] != menu) {
			tm_fx[tm[i][0]].stop();
			if ($('tm_'+tm[i][0]).offsetHeight > 0) {
				tm_fx[tm[i][0]].start({height: 0, opacity: 0})
			}
		}
		else {
			/*for (j=0; j<tm[i][2].length; j++) {
				getItem(tm[i][2][j][0], tm[i][2][j][1]);
			}*/
		}
	}

	// show new menu
	if ($('tm_'+menu).offsetHeight < $('tm_'+menu).getSize().scrollSize.y) {
		if ($('tm_'+menu).style.visibility!='visible')
			$('tm_'+menu).style.visibility = 'visible';
		tm_fx[menu].stop();
		tm_fx[menu].start({height: $('tm_'+menu).getSize().scrollSize.y, opacity: 1});
	}
	
	hideIntro();
	
  // preload screenshots for this tag's items and prepare slideshow
	preloadScreenshots(menu);
  
}

function preloadScreenshots(menu) {
	// check whether the items have finished loading
	if ($('loading_msg').style.display != 'none') {
		window.setTimeout('preloadScreenshots("'+menu+'");', 200);
		return;
	}
	// preload screenshots for this tag's items and prepare slideshow
	image_obj = new Image();
	thumbnail_ids = new Array();
	for (i=0; i<tm_imgs[menu].length; i++) {
		image_obj.src = tm_imgs[menu][i][0];
		thumbnail_ids.push(tm_imgs[menu][i][1]);
	}  
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}


function getTagItems(tag) {
	if ($("ip_holder_"+tag).innerHTML.trim() == "") {
		new Ajax("/portfolio/load/"+tag+"/", {
			method: 'get',
			update: $("ip_holder_"+tag),
			onRequest: function() {
				activeXHRs++;
				$('loading_msg').style.display = 'block';
			},
			onComplete: function() {   
				activeXHRs--;
				if (activeXHRs<0) 
					activeXHRs = 0;
				if (activeXHRs==0) 
					$('loading_msg').style.display = 'none';
				init_ip_fx();
				if (itemNextInLine)
					window.setTimeout('showItem(itemNextInLine)', 222);
			}
		}).request();
	}
}

var itemNextInLine = 'none';

function showItem(item) {
	if ($('loading_msg').style.display != 'none') {
		itemNextInLine = item;
		return;
	}
	
	if (item == 'none')
		return;
	
	itemNextInLine = 'none';
	
	currentItem = "ip_" + item;
	
	if ($('contentholder').style.display != 'block' || $('contentholder').style.visibility == 'hidden') {
		$('contentholder').style.visibility = 'hidden';
		$('contentholder').style.display = 'block';
	}
	for (i=0; i<ip.length; i++) {
		if (ip[i][0] != item) {
			ip_fx[ip[i][0]].stop();
			if ($('ip_'+ip[i][0]) && $('ip_'+ip[i][0]).offsetHeight > 0) {
				ip_fx[ip[i][0]].start.pass({height: 0, opacity: 0, display: 'none'}, ip_fx[ip[i][0]]).delay(0);
			}
		}
	}
	hideContactForm();
	if ($('ip_'+item).offsetHeight < $('ip_'+item).getSize().scrollSize.y) {		
		ip_fx[item].stop();
		if (!$('contentholder').style.marginTop || $('contentholder').style.marginTop == '0px') {
			contentfx.stop();
			contentfx.start.pass({opacity: [0, 1], marginTop: [0, getContentAreaPosition($('ip_'+item).getSize().scrollSize.y)]}, contentfx).delay(0);
		}
		else {
			window.setTimeout('contentfx.stop();', 400);
			contentfx.start.pass({opacity: 1, marginTop: getContentAreaPosition($('ip_'+item).getSize().scrollSize.y)}, contentfx).delay(444);
		}
		ip_fx[item].start.pass({height: [$('ip_'+item).offsetHeight, $('ip_'+item).getSize().scrollSize.y], opacity: 1, display: 'block'}, ip_fx[item]).delay(0);
	}
	
	stopSlideshow();	
	item_thumbnails = new Array();
	for (i=0; i<thumbnail_ids.length; i++) {
    if (thumbnail_ids[i].indexOf(item) != -1) {
      item_thumbnails.push(thumbnail_ids[i])
    }
  }
	if (item_thumbnails.length > 0) {
    document.getElementById(item_thumbnails[0]).style.display = 'block';
  }
  if (item_thumbnails.length > 1) {
    for (i=1; i<item_thumbnails.length; i++)
      document.getElementById(item_thumbnails[i]).style.display = 'none';
    slideshow_timer = window.setInterval('showNextScreenshot(item_thumbnails);', 4444);
  }
    
}

function stopSlideshow() {
  if (slideshow_timer) {
    clearInterval(slideshow_timer);
    slideshow_timer = false;
  }
}

function showNextScreenshot(ids) {
  next = 0;
  for (i=0; i<ids.length-1; i++) {
    if (document.getElementById(ids[i]).style.display == 'block') {
      next = ++i;
      break;
    }
  }
  for (i=0; i<ids.length; i++) {
    if (i == next)
      document.getElementById(ids[i]).style.display = 'block';
    else
      document.getElementById(ids[i]).style.display = 'none';
  }
}

function returnHome() {
	itemNextInLine = '';
  stopSlideshow();
  // hide all items
  for (i=0; i<ip.length; i++) {
		ip_fx[ip[i][0]].stop();
		if ($('ip_'+ip[i][0]) && $('ip_'+ip[i][0]).offsetHeight > 0) {
			ip_fx[ip[i][0]].start({height: [$('ip_'+ip[i][0]).offsetHeight, 0], opacity: 0});
		}
	}
  hideContactForm();
	// hide all menues
  for (i=0; i<tm.length; i++) {
		tm_fx[tm[i][0]].stop();
		if ($('tm_'+tm[i][0]).offsetHeight > 0) {
			tm_fx[tm[i][0]].start({height: [$('tm_'+tm[i][0]).offsetHeight, 0], opacity: 0});
		}
	}
  
  showIntro();
  
  unlightMenuButtons();
  unlightItemButtons();
}

function showIntro() {
	if ($('intro').offsetHeight == 0 || true) {
		introHeight = ((window.innerHeight||window.getHeight()) - 26 - 96) / 2 + 26;
		// show intro
		introfx.stop();
		introfx.start({ opacity: 1, height: introHeight });//$('intro').getSize().scrollSize.y })
		// hide content holder
		contentfx.stop();
		contentfx.start({opacity: 0, marginTop: 0});
		window.setTimeout('$("contentholder").style.display="none"', 888);
	}
	currentItem = 'intro';
}

var introHeight;

function setIntroPosition() {
	if ($('intro').offsetHeight > 0) {
		introHeight = ((window.innerHeight||window.getHeight()) - 26 - 96) / 2 + 26;
		// show intro
		introfx.stop();
		introfx.start({ opacity: 1, height: introHeight });//$('intro').getSize().scrollSize.y })
	}
}

function hideIntro() {
	// hide intro if visible
	if ($('intro').offsetHeight > 0) {
		introfx.stop();
		introfx.start({ opacity: 0, height: 0 })
	}
}

function showContactForm() {

	itemNextInLine = '';
	currentItem = 'contact_form';
	
	$('contact_button').style.display = 'none';
	
	if ($('contentholder').style.display != 'block' || $('contentholder').style.visibility == 'hidden') {
		$('contentholder').style.visibility = 'hidden';
		$('contentholder').style.display = 'block';
	}
	
	hideIntro();
	
	// hide item presentations
	for (i=0; i<ip.length; i++) {
		ip_fx[ip[i][0]].stop();
		if ($('ip_'+ip[i][0]) && $('ip_'+ip[i][0]).offsetHeight > 0) {
			ip_fx[ip[i][0]].start.pass({height: 0, opacity: 0, display: 'none'}, ip_fx[ip[i][0]]).delay(0);
		}
	}
	
	// hide menues
	/*for (i=0; i<tm.length; i++) {
		tm_fx[tm[i][0]].stop();
		if ($('tm_'+tm[i][0]).offsetHeight > 0) {
			tm_fx[tm[i][0]].start({height: [$('tm_'+tm[i][0]).offsetHeight, 0], opacity: 0});
		}
	}*/
	
	unlightItemButtons();
	
	if ($('contact_form').offsetHeight < $('contact_form').getSize().scrollSize.y) {		
		contactformfx.stop();
		if (!$('contentholder').style.marginTop || $('contentholder').style.marginTop == '0px') {
			contentfx.stop();
			contentfx.start.pass({opacity: [0, 1], marginTop: [0, getContentAreaPosition($('contact_form').getSize().scrollSize.y)]}, contentfx).delay(0);
		}
		else {
			window.setTimeout('contentfx.stop();', 400);
			contentfx.start.pass({opacity: 1, marginTop: getContentAreaPosition($('contact_form').getSize().scrollSize.y)}, contentfx).delay(444);
		}
		contactformfx.start.pass({height: [$('contact_form').offsetHeight, $('contact_form').getSize().scrollSize.y], opacity: 1, display: 'block'}, contactformfx).delay(0);
	}
	
	stopSlideshow();

}

function hideContactForm() {
	
	$('contact_button').style.display = 'block';
	
	for (i=0; i<ip.length; i++) {
		contactformfx.stop();
		if ($('contact_form').offsetHeight > 0) {
			contactformfx.start.pass({height: 0, opacity: 0, display: 'none'}, contactformfx).delay(0);
		}
	}
	
}

function moveThem(evt) {

	var evt = evt||event;
	var width = window.innerWidth||document.body.offsetWidth;

	if (document.getElementById) { // good browser ahead!

		/*if (evt.clientX > width) {
			centerOffset = parseInt(width/2-evt.clientX);
		}
		else {
			centerOffset = parseInt(evt.clientX-width/2);
		}*/

		/*document.getElementById("logo").style.marginLeft = parseInt(-centerOffset*0.22)-137+parseInt(width/2-186)+"px";
		document.getElementById("whatsthis").style.marginLeft = parseInt(centerOffset*0.7)+parseInt(width/2-600)+100+"px";
		document.getElementById("content").style.backgroundPosition = "0px " + parseInt(-evt.clientY/15) + "px";*/
		document.getElementById("body").style.backgroundPosition = -parseInt(evt.clientX/33) + "px " + -parseInt(evt.clientY/33) + "px";

	}

}

var headerHeight = 129;
var footerHeight = 154;
var contentAreaHeight = 0;

function setContentAreaBoundaries() {
	var height = window.innerHeight||window.getHeight();
	contentAreaHeight = height - headerHeight - footerHeight;
	$('content').style.maxHeight = contentAreaHeight + 'px';
}

function getContentAreaPosition(scrollHeight) {
	var marginTop = 30;
	if (scrollHeight < contentAreaHeight) {	
		marginTop += (contentAreaHeight-scrollHeight)/2;
	}
	return marginTop;
}

function setContentAreaPosition() {
	if (!$('contentholder').style.marginTop || $('contentholder').style.marginTop == '0px') {
		contentfx.stop();
		contentfx.start({opacity: [0, 1], marginTop: [0, getContentAreaPosition($(currentItem).getSize().scrollSize.y)]});
	}
	else {
		contentfx.stop();
		contentfx.start({opacity: 1, marginTop: getContentAreaPosition($(currentItem).getSize().scrollSize.y)});
	}
}
window.onresize = function() {

	setContentAreaPosition();
	setIntroPosition();

}
	
window.setInterval('setContentAreaBoundaries();', 1000);


handleLocationHash = function() {
	
	goToIntro = true;
	
	if (document.location.hash.indexOf('contact')!=-1 || goToContactForm) {
		showContactForm();
		goToIntro = false;
	}
	
	// extract tag
	for (i=0; i<tm.length; i++) {
		if (document.location.hash.indexOf(tm[i][0])!=-1) {
			tag = tm[i][0];
			highlightTagButton(tag);
			showMenu(tag);
			goToIntro = false;
		}
	}
	for (i=0; i<ip.length; i++) {
		if (document.location.hash.indexOf(ip[i][0])!=-1) {
			itemNextInLine = ip[i][0];
			highlightItemButtons(ip[i][0]);
			goToIntro = false;
		}
	}
	
	if (goToIntro) {
		//introfx.start({ opacity: [0, 1], height: [0, $('intro').getSize().scrollSize.y]});	
		showIntro();
		currentItem = 'intro';
	}
}
window.onload = function() {
	
	init_fx();
    setContentAreaBoundaries();
	
	handleLocationHash();
	
	if (document.location.search.indexOf('success')!=-1 || document.location.hash.indexOf('success')!=-1)
			$('intro_logo').src = 'http://www.kussatz.com/media/thanks.png';

}

//document.onmousemove = moveThem;