/////////////// browser detection //////////////////////

var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5")==-1) );
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5")!=-1) );
var videoPlaying = false;

var MAC = navigator.platform.indexOf("Mac") != -1;

/////////////// init //////////////////////
var initOk = false; // main variable to enable functions

function init() {
	// initialize main functions
	initOk = true;
}


//////////////////////////// common functions ////////////////////////////////////



// open a window with a specific size
function openWin(url,winName,wW,wH,opts) {

	if(!opts) opts = "toolbar=1,location=1,directories=0,status=1,menuBar=1,resizable=1,scrollBars=1";
	if (wW && wH) {
		sW = screen.availWidth;
		sH = screen.availHeight;
		wW = parseInt(wW) + 20; // add marginright
		wH = parseInt(wH) + 30; // add marginbottom
		
		// compare the req. window size with screen size (minus margin of 20 px).
		sbW=0;
		sbH=0;
		
		if (sW < wW-60) { // screen-width is smaller than requested window-width
			wW = sW - 60; // set window-width to screen-width minus a small tolerance-margin
			sbH = 40; // window is smaller than requested, so most likely a  vertical scrollbar will appear, add space for it to height
		}
		
		if (sH < wH-60) { // screen-height is smaller than requested window-height
			wH = sH - 60; // set window-height to screen-height minus a small tolerance-margin
			sbW = 40; // window is smaller than requested, so most likely a  vertical scrollbar will appear, add space for it to width;
		}
		
		// if window is smaller than requested show scrollbars
		// if (sbW || sbH) opts = opts + ",scrollBars=1";
		// else opts = opts + ",scrollBars=0";
		
		// add scrollbars to width and height if they appear
		wW = wW+sbW;
		wH = wH+sbH;
		wleft = ((screen.width-wW)/2)-15;
		wtop = ((screen.height-wH)/2)-15;
		opts = "width="+wW+",height="+wH+",left="+wleft+",top="+wtop+opts;
	}
	var win = window.open(url,"_blank",opts); // use this if you want a special window look
	if (parseInt(navigator.appVersion) >= 4) win.window.focus();
}	


//// form functions ////

function seaFieldClear(aField) {
	if(aField.value == "Suche") {
		aField.value = "";
	}
}

function seaFormCheck(aForm) {
	
	/* temporary
	alert('Diese Funktion steht in Kürze zur Verfügung!');
	return false;
	/* temporary */
	
	var seaWord = aForm.seaWord.value;
	if (aForm) {
		if (seaWord.length == 0) {
			alert(seaNoChars);
			return false;
		} else if (seaWord.length < 3) {
			alert(seaFewChars);
			return false;
		}
	} else {
		alert("Es ist ein Problem beim Überprüfen des Formulars aufgetreten.\nBitte informieren Sie ar@aspekt1.net.\n\nAn error occured while checking the form.\nPlease inform ar@aspekt1.net.");
		return false;
	}
	aForm.submit();
	return true;
}

//
// list functions
//

function invertRow (elmt) {
	eA = document.getElementById("r"+elmt+"_1");
	eB = document.getElementById("r"+elmt+"_2");
	eC = document.getElementById("r"+elmt+"_3");
	eA.style.backgroundColor = eB.style.backgroundColor = eC.style.backgroundColor = "#bbbbbb";
	eA.style.color = eB.style.color = eC.style.color = "#000";
}

function invertInactRow (elmt) {
	eA = document.getElementById("r"+elmt+"_1");
	eB = document.getElementById("r"+elmt+"_2");
	eC = document.getElementById("r"+elmt+"_3");
	// eA.style.backgroundColor = eB.style.backgroundColor = eC.style.backgroundColor = "#cccccc";
	eA.style.backgroundColor = eB.style.backgroundColor = eC.style.backgroundColor = "transparent";

}

function normalizeRow(elmt) {
	eA = document.getElementById("r"+elmt+"_1");
	eB = document.getElementById("r"+elmt+"_2");
	eC = document.getElementById("r"+elmt+"_3");
	eA.style.backgroundColor = eB.style.backgroundColor = eC.style.backgroundColor = "transparent";
	eA.style.color = eB.style.color = eC.style.color = "#666666";
}


//
// Image functions
//

// set attributes for preloading the images
var imgPreload = new Array();
var imgCount = 0;
function preloadImage(fn,id) {
	imgPreload[id] = new Image();
	imgPreload[id].onload = function() {
		$(id).setAttribute('rel','ok');
	}
	$(id).setAttribute('rel','loading');
	imgPreload[id].src = fn;
	imgCount++;
}

// preload the images
function preloadImages(tagId) {
	var first = true;
	$$('#'+tagId+" img").each(function(value,index) {
		preloadImage(value.src,value.id);
	});
}

// show the images
function showImg(idWithoutPrefix) {	
	// fade in the right image
	var imgId = "img"+idWithoutPrefix;
	if ($(imgId)) {
		if ($(imgId).getAttribute('rel') != 'ok') {
			// if image isnt loaded yet, show loading image and try again later
			new Element.setStyle('loading',{'display': 'block'});
			setTimeout(function() { showImg(idWithoutPrefix); }, 100) ;
		} else {
			// hide the loading image
			new Element.setStyle('loading',{'display': 'none'});
			// fade-in
			$$("#imageFader img").each(function(value, index) {
				if(value.getAttribute('rel') == 'ok')
					new Effect.Fade(value.id,{duration:0.5, queue:{scope:'showreal'}});
			});
			// fade-out
			new Effect.Appear((imgId), {duration:0.8, queue:{position:'end',scope:'showreal'}});
		} 
	}
}

// start gallery with loop
var currentGalleryImgId = 0;


function showreel() {
	switchreel(); // call fade-in for first image without delay
	window.setInterval("switchreel()", 6000);	// switch every n milliseconds
}
function switchreel() {
	showImg(currentGalleryImgId);
	currentGalleryImgId++;
	// looping through the images
	// alert(currentGalleryImgId+" - "+imgCount);
	if (currentGalleryImgId >= imgCount) currentGalleryImgId = 0;
}


//                                  
//  project gallery                 
//                                  

// toggle the full image

function initGal() {
    if ($('gImg0').getAttribute('rel') != 'ok') {
		// if image isnt loaded yet, show loading image and once again
		setTimeout(function() { initGal(); }, 100) ;
	} else {
	  //preloader is finished -> remove div#loading from display and fade-in first galleryImg;
    new Element.setStyle('loading', {'display': 'none'});
    new Effect.Appear('gImg0', {duration:0.6});
  }

}

function projGalShowImg(id) {	
	// fade in the right image
	
	if(videoPlaying)
	{
	  swfobject.removeSWF("flashbox")
	}
	
	var imgId = "gImg"+id;
	if ($(imgId)) {
		$$("#projGal img").each(function(value, index) {
			new Effect.Fade(value.id,{duration:0.3, queue:{scope:'projGal'}});
		});
		// fade-out
		new Effect.Appear((imgId), {duration:0.3, queue:{position:'end',scope:'projGal'}});
	}
}


//                                  
// menu effects                     
//                                  

function showMenuL4(fadeType) {
	// we only want to fade in menuL4 if the user changed the client, not the project only
	
	/*
	if (fadeType == 'fade') {
	  if ($$("#menuL3 div.sublevel")) {
	    $$("#menuL3 div.sublevel").each(function (element) {
	      menuOpen(element);
	    });
	  }
	} else {
		if ($$("#menuL3 div.sublevel")) {
	    $$("#menuL3 div.sublevel").each(function (element) {
			element.show();
	    });
	  }
	}
	*/
}

function fadeMenues(fadeType)
{
  //menuL12
  if ($('lastViewL2'))
  {
    menuClose($('lastViewL2'));
    menuOpen($('menuL2'));
  }
  else if (fadeType == 'fadeL2' && $('menuL2'))
  {
    menuOpen($('menuL2'));
  }
  
  else if ($('menuL2'))
  {
    $('menuL2').show();
  }
  
  //menuL34
  if ($('lastViewL4')) 
  {
	  menuClose($('lastViewL4'));
	  menuOpen($('menuL4Active'));
	}
	else if (fadeType == 'fadeL4' && $('menuL4Active'))
	{
	  menuOpen($('menuL4Active'));
	}
	else if ($('menuL4Active'))
	{
	  $('menuL4Active').show();
	}
  
}


function menuClose(id) {
		new Effect.BlindUp(id, {duration: 0.6});
}

function menuOpen(id) {
		new Effect.BlindDown(id, {duration: 0.6});
}

//
// video playback & embedding
//

function showVideo() {
  var container = $("flashbox");
  if (!container)
  {
    var d = document.createElement("div");
    d.setAttribute("id","flashbox");
    document.getElementById("stage").appendChild(d);
  }
  
  var params = {};
  var attributes = {};
  $$('#projGal img').each(function(element) {
    element.hide();
  });
  swfobject.embedSWF("flv_player.swf", "flashbox", "511", "287", "9.0.0", "", flashvars, params, attributes);
  videoPlaying = true;
}
