/*
// File written by Jochen "Khuri" Höhmann <khuri@khuris.com>
// For Werbeagentur Rechl <www.rechl.de>
// Copyright 2010
//
// File        : main.js
// Begin       : 2008.07.07 13:27:13
// Last Update : 2010.11.17 09:31:07
*/

var clientbrowser = navigator.userAgent.toLowerCase();
var is_ie = ((clientbrowser.indexOf("msie") != -1) && (clientbrowser.indexOf("opera") == -1));
var is_nav = ((clientbrowser.indexOf('mozilla')!=-1) && (clientbrowser.indexOf('compatible') == -1));
var menu_interval = new Array();
var menu_interval_pos = new Array();
var gallery_next = 0;
var gallery_img = new Array();
var gallery_shrink;
var gsize = document.getElementById('gallery_block');
var gallery_stime = 5000; // the initial wait time for images to switch, 5s

function convert_mail(mail) {
	window.location.href="mailto:"+decodeURIComponent(mail);
}
// Statusbox
function statusbox_close() {
  document.getElementById('statusbox').style.visibility='hidden';
  document.getElementById('statusbox').style.display='none';
}
function gowhere(where,how) {
	if(how == 1) {
		window.open(where);
	}
	else {
		window.location.href=where;
	}
}

function fade_menu(mid,dir) {
	mid = parseInt(mid);
	window.clearInterval(menu_interval[mid]);
	move_menu(mid,1,dir);
}
function move_menu(mid,init,dir) {
	theelement = document.getElementById('menu_id_'+mid);
	if(init == 1) {
		window.clearInterval(menu_interval[mid]);
		menu_interval_pos[mid] = (parseInt(theelement.style.marginLeft) > 0) ? parseInt(theelement.style.marginLeft) : 0;
		menu_interval[mid] = window.setInterval("move_menu("+mid+",0,"+dir+")",25);
	}
	if(dir == 1) {
		if(menu_interval_pos[mid] == 8) {
			window.clearInterval(menu_interval[mid]);
		}
		else {
			menu_interval_pos[mid] = menu_interval_pos[mid] +1;
			theelement.style.marginLeft = '+'+menu_interval_pos[mid]+'px';
		}
	}
	if(dir == 2) {
		if(menu_interval_pos[mid] == 0) {
			window.clearInterval(menu_interval[mid]);
		}
		else {
			menu_interval_pos[mid] = menu_interval_pos[mid] -1;
			theelement.style.marginLeft = '+'+menu_interval_pos[mid]+'px';
		}
	}
}

// Image switching fade functions

var rb_akti = 0;
var rb_imgb = new Array();
var rb_fadeid;
var rb_fadelock=0;
var rb_gallery_stime = 8000; // the initial wait time for images to switch
var rb_banner_fadespeed = 75;
var rb_banner_fademultiply = 50;

function rb_gallery_cache(rb_frun) {
	var rb_pload;
	if(rb_frun == 1) {
		for(i=0;i<gallery_array.length;i++) {
			rb_imgb[i] = new Image();
			rb_imgb[i].src = gallery_array[i];
		}
	}
	for(i=0;i<2;i++) {
		// If first 2 images are cached, start fading loop...
		if(rb_imgb[i].complete == false) {
			rb_pload = 0;
			break;
		}
		else {
			rb_pload = 1;
		}
	}
	if(rb_pload == 0) {
		setTimeout("rb_gallery_cache(0)",50);
	}
	else {
		switch_imgb(2);
		setTimeout("rb_fade(0,0)",3000); // launch first fade after 3s
	}
}
function rb_opac(i,obj) {
	var rb_object = document.getElementById(obj).style;
	rb_object.opacity = (i / rb_banner_fademultiply);
	rb_object.MozOpacity = (i / rb_banner_fademultiply);
	rb_object.KhtmlOpacity = (i / rb_banner_fademultiply);
	rb_object.filter = "alpha(opacity="+(i * (1 / rb_banner_fademultiply * 100))+")";
}
function rb_fade(rb_dostop,rb_newakti) {
	if(rb_fadelock == 0) {
		var rb_timer = 0;
		var rb_j = 1;
		window.clearInterval(rb_fadeid);
		if(rb_dostop == 1) {
			rb_akti = (rb_newakti < (rb_imgb.length -1) && rb_newakti >= 0) ? rb_newakti : ((rb_newakti > (rb_imgb.length -1)) ? 0 : (rb_imgb.length -1));
		}
		// set a lock on this function
		rb_fadelock = 1;
		setTimeout("switch_imgb(1);",rb_banner_fadespeed +10);
		for(i=1;i<=rb_banner_fademultiply;i++) {
			rb_timer++;
			setTimeout("rb_opac("+i+",'img_bgi');",rb_timer * rb_banner_fadespeed);
			setTimeout("rb_opac("+(rb_banner_fademultiply - i)+",'img_bgi2');",rb_timer * rb_banner_fadespeed);
		}
		setTimeout("switch_imgb(2);",rb_banner_fademultiply * rb_banner_fadespeed);
		setTimeout("rb_fadelock=0",rb_banner_fademultiply * rb_banner_fadespeed);
		if(rb_dostop == 0) {
			rb_fadeid = window.setInterval("rb_fade("+rb_dostop+",0)",rb_gallery_stime);
		}
	}
}
function switch_imgb(i) {
	if(i == 1) {
		document.getElementById('img_bgi').src = rb_imgb[rb_akti].src;
	}
	else {
		// Change the text
		result = gallery_array[rb_akti].match(/id_(\d+)_(\d+)[_]?(cl|ex|ea|ec)?/ig);
		result_ext = result[0].match(/(cl|ex|ea|ec)/ig);
		if(result_ext) {
			line_top_string = '';
			switch(result_ext[0]) {
				case "cl": line_top_string='classic';line_top_classid=12;break;
				case "ex": line_top_string='exclusive';line_top_classid=13;break;
				case "ea": line_top_string='emotionART';line_top_classid=14;break;
				case "ec": line_top_string='economy';line_top_classid=31;break;
				default: line_top_string=line_top_name;line_top_classid=0;break;
			}
			// remove old markers on menu
			document.getElementById('mt_12').className = "";
			document.getElementById('mt_13').className = "";
			document.getElementById('mt_14').className = "";
			document.getElementById('mt_31').className = "";
			// Add marker and set top text
			if(line_top_classid > 0) {
				document.getElementById('mt_'+line_top_classid).className = "selected";
			}
			document.getElementById('line_top').innerHTML = line_top_string;
		}
		else {
			document.getElementById('line_top').innerHTML = line_top_name;
		}
		// Change image and buttons
		document.getElementById('img_bgi2').src = rb_imgb[rb_akti].src;
		akti_p = ((rb_akti -1) < 0) ? (rb_imgb.length -1) : (rb_akti -1);
		rb_akti = (rb_akti < (rb_imgb.length -1)) ? (rb_akti +1) : 0;
		if(is_nav) {
			document.getElementById('img_an').setAttribute("onclick","javascript:rb_fade(1,"+rb_akti+");");
			document.getElementById('img_ap').setAttribute("onclick","javascript:rb_fade(1,"+akti_p+");");
		}
		else {
			document.getElementById('img_an').setAttribute("onclick",function(){rb_fade(1,rb_akti)});
			document.getElementById('img_ap').setAttribute("onclick",function(){rb_fade(1,akti_p)});
		}
	}
}
