var site_base_url = getBaseURL();

var img_width = 600;
var img_height = 800;

var btn_right_width = 33;
var btn_right_height = 53;
var btn_left_width = 33;
var btn_left_height = 53;

var loading_width = 200;
var loading_height = 75;

var row_count = 6;

var min_space_height = 0;

var header_top = 58;

var space_top = 0;

var email_txt = 'info@tomstrohmetz.de';

var dummy_image = 'images/blank.png';
var right_image = 'images/right.png';
var left_image = 'images/left.png';
var images_url = 'images/images.txt';
var images_url_pref = '';

var scroll_right_step = 2;

var hideScrollRightTimer = null;

var scroll_attributes = null;
var scroll_anim = null;

var menu_picures = new Array(
	'images/menu1.png',
	'images/menu2.png',
	'images/menu3.png',
	'images/menu4.png',
	'images/menu5.png',
	'images/menu6.png',
	'images/menu1a.png',
	'images/menu2a.png',
	'images/menu3a.png',
	'images/menu4a.png',
	'images/menu5a.png',
	'images/menu6a.png'
);

var site_content_images = new Array();

var loading_img = new Array();
var waitForImage = false;

var imgLoadGroup = null;

function test_scroll()
{
	var content = YAHOO.util.Dom.get('content');
	var x1 = content.scrollLeft;
	var y1 = content.scrollTop;
	YAHOO.util.Dom.get('test').innerHTML = "pos : " + x1 + ',' + y1;
}

function site_init()
{
//	YAHOO.util.Event.addListener('content', 'scroll', test_scroll);
	YAHOO.util.Event.addListener(window, 'resize', site_init_spacer); 
	YAHOO.util.Event.addListener('content', 'mousemove', site_show_scroll_leftright); // Internet Explorer 9
	var content = YAHOO.util.Dom.get('content');

	site_link_emails();
	YAHOO.util.Event.addListener(window, "load", site_init_content); 
	YAHOO.util.Event.addListener('btn_right', 'click', site_scroll_right);
	YAHOO.util.Event.addListener('btn_left', 'click', site_scroll_left);
	site_init_spacer();
	site_preload_images(menu_picures);
	site_preload_images(new Array(dummy_image,right_image,left_image));
	site_init_menu();
	site_setActiveMenu(1);
}

function site_init_spacer()
{
	var width = parseInt(YAHOO.util.Dom.getClientWidth()); 
	var height = parseInt(YAHOO.util.Dom.getClientHeight()); 

	var content = YAHOO.util.Dom.get('content');
	YAHOO.util.Dom.setStyle(content,'width',width+'px');
	YAHOO.util.Dom.setStyle(content,'height',height+'px');

	if (width < 1200)
	{
		scroll_right_step = 1;
	}
	else
	{
		scroll_right_step = 2;
	}

	var space_height = parseInt((height - img_height) / 2);

	if (space_height < min_space_height)
	{
		space_height = min_space_height;
	}

	space_top = space_height;

	var top_spacer = YAHOO.util.Dom.get('space_1');
	var bottom_spacer = YAHOO.util.Dom.get('space_'+(row_count+1));

	if (top_spacer != null)
	{
		YAHOO.util.Dom.setStyle(top_spacer,'height',space_height+'px');
	}

	if (bottom_spacer != null)
	{
		YAHOO.util.Dom.setStyle(bottom_spacer,'height',space_height+'px');
	}

	var header = YAHOO.util.Dom.get('header');
	if (header != null)
	{
		YAHOO.util.Dom.setStyle(header,'top',(header_top+space_height)+'px');
	}

	var btn_top = parseInt((height - btn_right_height) / 2);
	var btn_right = YAHOO.util.Dom.get('btn_right');

	if (btn_right != null)
	{
		YAHOO.util.Dom.setStyle(btn_right,'margin-top',btn_top+'px');
		YAHOO.util.Dom.setStyle(btn_right,'margin-bottom',btn_top+'px');
	}

	btn_top = parseInt((height - btn_left_height) / 2);
	var btn_left = YAHOO.util.Dom.get('btn_left');

	if (btn_left != null)
	{
		YAHOO.util.Dom.setStyle(btn_left,'margin-top',btn_top+'px');
		YAHOO.util.Dom.setStyle(btn_left,'margin-bottom',btn_top+'px');
	}

	var loading_top = parseInt((height - loading_height) / 2);
	var loading_left = parseInt((width - loading_width) / 2);
	var loading = YAHOO.util.Dom.get('loading');
	if (loading != null)
	{
		YAHOO.util.Dom.setStyle(loading,'width',loading_width+'px');
		YAHOO.util.Dom.setStyle(loading,'height',loading_height+'px');
		YAHOO.util.Dom.setStyle(loading,'top',loading_top+'px');
		YAHOO.util.Dom.setStyle(loading,'left',loading_left+'px');
	}
}

function site_init_menu()
{
	var menu = YAHOO.util.Dom.get('menu');
	if (menu != null)
	{
		var lis = menu.getElementsByTagName('li');
		if (lis != null && lis.length > 0)
		{
			var li;
			for (i = 0; i < lis.length; i++)
			{
				if (lis[i].id)
				{
					YAHOO.util.Event.addListener(lis[i].id, "click", site_menu_click);
				}
			}
		}
	}
}

function site_init_content()
{
	YAHOO.util.Event.removeListener(window, "load", site_init_content); 
	var images_txt_callback = { 
		success: site_images_txt_handleSuccess
	};

	var request = YAHOO.util.Connect.asyncRequest('GET', images_url_pref + images_url, images_txt_callback);
}

function site_show_scroll_leftright()
{
	YAHOO.util.Dom.removeClass('navi_right', 'hide'); 
	YAHOO.util.Dom.addClass('navi_right', 'show');
	YAHOO.util.Dom.removeClass('navi_left', 'hide'); 
	YAHOO.util.Dom.addClass('navi_left', 'show');
	if (hideScrollRightTimer)
	{
		window.clearTimeout(hideScrollRightTimer);
	}
	hideScrollRightTimer=window.setTimeout("site_hide_scroll_leftright()",2000);
}

function site_hide_scroll_leftright()
{
	YAHOO.util.Dom.removeClass('navi_right', 'show'); 
	YAHOO.util.Dom.addClass('navi_right', 'hide'); 
	YAHOO.util.Dom.removeClass('navi_left', 'show'); 
	YAHOO.util.Dom.addClass('navi_left', 'hide'); 
}

function site_images_txt_handleSuccess(o)
{
	if(o.responseText !== undefined)
	{ 
		site_content_images = o.responseText.replace(/\r/g, '');
		site_content_images = site_content_images.split('\n');
	}

	site_init_content_images();
}

function site_init_content_images()
{
	imgLoadGroup = new YAHOO.util.ImageLoader.group('content', 'scroll', 1);
	// imgLoadGroup.foldConditional = true;
	imgLoadGroup.addTrigger(window, 'resize');

	var x = 0;
	var y = 0;

	if (site_content_images && site_content_images.length > 0)
	{
		for (i = 0; i < site_content_images.length; i++)
		{
			if (site_content_images[i] == '')
			{
				y++;
				x = 0;
			}
			else
			{
				if (y < 4)
				{
					site_add_content_image(y,x,site_content_images[i]);
					x++;
				}
			}
		}
	}

	// imgLoadGroup.fetch();
}

function site_add_content_image(y, x, image)
{
	var el;
	var img;

	if (image != '')
	{
		var row = YAHOO.util.Dom.get('row_'+(y+1));
		if (row != null)
		{
			var divs = row.getElementsByTagName('div');

			// add images
			if (x >= divs.length)
			{
				for (var i = divs.length; i <= x; i++)
				{
					el = document.createElement('div');
					el.id = 'img_'+(y+1)+'_'+(x+1);
					YAHOO.util.Dom.addClass(el,'img');
					row.appendChild(el);

					img = document.createElement('img');
					img.setAttribute('alt','');
					img.setAttribute('border','');

					img.id = 'i_'+(y+1)+'_'+(x+1);
					img.style.visibility = 'hidden';
					el.appendChild(img);
					var scrollImg = imgLoadGroup.registerSrcImage(img.id, images_url_pref + image, img_width, img_height); 
					scrollImg.setVisible = true;
					loading_img[loading_img.length] = img;
					site_show_loading();
				}

				// set new width
				var width = img_width * (x+1);
				YAHOO.util.Dom.setStyle(row,'width',width+'px');

				divs = row.getElementsByTagName('div');
			}

			// replace image

			if (x < divs.length)
			{
				img = divs[x].getElementsByTagName('img');
				if (img.length > 0)
				{
					img[0].src = images_url_pref + image;
				}
			}
		}
	}
}

function site_menu_click(e)
{
	var menu_id = this.id;
	var menu_item = parseInt(menu_id.substring(menu_id.lastIndexOf('_')+1));
	if (menu_item > 0)
	{
		site_setActiveMenu(menu_item);
	}
}

function site_setActiveMenu(id)
{
	var menu = YAHOO.util.Dom.get('menu');
	var menu_id = '';
	var menu_item = 0;

	if (menu != null)
	{
		var lis = menu.getElementsByTagName('li');
		if (lis != null && lis.length > 0)
		{
			YAHOO.util.Dom.removeClass(lis,'active');
			YAHOO.util.Dom.addClass(lis,'inactive');
			var li;
			for (i = 0; i < lis.length; i++)
			{
				if (lis[i].id)
				{
					menu_id = lis[i].id;
					menu_item = parseInt(menu_id.substring(menu_id.lastIndexOf('_')+1));

					if (menu_item == 0)
					{
						menu_item = i;
					}

					if (menu_item == id)
					{
						YAHOO.util.Dom.removeClass(lis[i], 'inactive');
						YAHOO.util.Dom.addClass(lis[i], 'active');
					}
				}
			}
		}
	}

	site_scroll_to_y(id);
}

function site_scroll_to_y(y)
{
	site_scroll_to(y,0);
}

function site_scroll_to(y, x)
{
	site_scroll_to_xy(x, y, 2000);
}

function site_scroll_to_xy(x, y, time)
{
	x = (x < 0) ? 0 : x;
	y = (y < 0) ? 0 : y;

	var content = YAHOO.util.Dom.get('content');
	var x1 = content.scrollLeft;
	var y1 = content.scrollTop;

	var XY = new Array();

	var row = YAHOO.util.Dom.get('row_'+y);

	if (row != null)
	{
		var divs = row.getElementsByTagName('div');
		if (x < divs.length)
		{
			if ( divs[x].id != 'clients' && divs[x].id != 'contact')
			{
				XY = YAHOO.util.Dom.getXY(divs[x]);
				XY[0] += x1;
				XY[1] += y1;
			}
			else
			{
				// eventuell zu y+1 x=0 scrollen
				// var XY = YAHOO.util.Dom.getXY(row);
				return;
			}
		}
		else
		{
			// eventuell zu y+1 x=0 scrollen
			// var XY = YAHOO.util.Dom.getXY(row);
			return;
		}

		if (space_top == 0)
		{
			var height = parseInt(YAHOO.util.Dom.getClientHeight()); 

			var add_height = parseInt((img_height - height) / 2);
			XY[1] += add_height;
		}
		else
		{
			XY[1] -= space_top;
		}

		smoothScroll(XY[0],XY[1], time);
	}
}

function site_scroll_right()
{
	var content = YAHOO.util.Dom.get('content');
	var x1 = content.scrollLeft;
	var y1 = content.scrollTop;
	var x = Math.floor( x1 / img_width) + scroll_right_step;
	var y = Math.floor( (y1 + space_top) / (img_height + img_height))+1;

	site_scroll_to_xy(x, y, 1000);
}

function site_scroll_left()
{
	var content = YAHOO.util.Dom.get('content');
	var x1 = content.scrollLeft;
	var y1 = content.scrollTop;
	var x = Math.floor( x1 / img_width) - scroll_right_step;
	var y = Math.floor( (y1 + space_top) / (img_height + img_height))+1;

	site_scroll_to_xy(x, y, 1000);
}

function site_preload_images(pictures)
{
	var img_;

	for (i = 0; i < pictures.length; i++)
	{
		img_ = new Image();
		img_.src = site_base_url + images_url_pref + pictures[i];
		loading_img[loading_img.length] = img_;
		site_show_loading();
	}
}

function getBaseURL()
{
	var url = location.href;
	return url.substring(0,url.lastIndexOf('/')+1);;
}

function smoothScroll(x, y, time)
{
/*	var speed = 50;

	var x1 = currentXPosition();
	var y1 = currentYPosition();
*/

	var content = YAHOO.util.Dom.get('content');
	var x1 = content.scrollLeft;
	var y1 = content.scrollTop;
    var x2 = x;
    var y2 = y;

    if (x1 == x2 && y1 == y2)
    {
    	return;
    }

    if (scroll_anim != null && scroll_anim.isAnimated())
	{
    	scroll_anim.stop(true);
        return;
    }

	scroll_attributes = { scroll: { to: [x2, y2] } };

	if (scroll_anim == null)
	{
		scroll_anim = new YAHOO.util.Scroll('content', scroll_attributes, time/1000, YAHOO.util.Easing.easeOut);
	}
	else
	{
		scroll_anim.init('content', scroll_attributes, time/1000, YAHOO.util.Easing.easeOut);
	}
	scroll_anim.animate();
}

/*
from x1,y1 to x2,y2 in px
speed = scrittweite in ms
step = naechster schrit in ms
time = gesamtzeit in ms
*/
function site_scroll_step(x1,y1,x2,y2,speed,step,time)
{
	if (step > time)
	{
    	isScroll = false;
    	scrollTimer = null;
		return;
	}

	if (x1 == x2 && y1 == y2)
	{
    	isScroll = false;
    	scrollTimer = null;
		return;
	}

	// weg
	var x_dist = x2 - x1;
	var y_dist = y2 - y1;

	if (x_dist == 0 && y_dist == 0)
	{
    	isScroll = false;
    	scrollTimer = null;
		return;
	}

	var t = ( (step / time) * (Math.E - 1) )+1; // t = 1..e

	var s = Math.log(t);

	x_step = x1 + (s*x_dist);
	y_step = y1 + (s*y_dist);

	// zu weit
	if (
		(x_step > x2 && x_dist >= 0) ||
		(y_step > y2 && y_dist >= 0) ||
		(x_step < x2 && x_dist < 0) ||
		(y_step < y2 && y_dist < 0)
	)
	{
    	isScroll = false;
    	scrollTimer = null;
		return;
	}

    window.scrollTo(x_step,y_step);

	step += speed;

    if (scrollTimer != null)
    {
    	window.clearTimeout(scrollTimer);
    }
	scrollTimer = window.setTimeout('site_scroll_step('+x1+','+y1+','+x2+','+y2+','+speed+','+step+','+time+')', speed);
}

function currentXPosition()
{
	if (window.pageXOffset != undefined )
		return window.pageXOffset;

	// Firefox, Chrome, Opera, Safari
	if (self.pageXOffset != undefined)
		return self.pageXOffset;

	// Internet Explorer 6 - standards mode
    if (document.documentElement  != undefined && document.documentElement.scrollLeft != undefined)
    	return document.documentElement.scrollLeft;

    // Internet Explorer 6, 7 and 8
    if (document.body.scrollLeft  != undefined)
    	return document.body.scrollLeft;

    return 0;
}

function currentYPosition()
{
	if (window.pageYOffset != undefined)
		return window.pageYOffset;

	// Firefox, Chrome, Opera, Safari
	if (self.pageYOffset  != undefined)
		return self.pageYOffset;

	// Internet Explorer 6 - standards mode
    if (document.documentElement != undefined && document.documentElement.scrollTop != undefined)
    	return document.documentElement.scrollTop;

    // Internet Explorer 6, 7 and 8
    if (document.body.scrollTop != undefined)
    	return document.body.scrollTop;

    return 0;
}

function site_link_emails()
{
	var email = YAHOO.util.Dom.get('email');

	if (email != null)
	{
		email.innerHTML = '<a href="mailto:' + email_txt + '">' + email_txt + '</a>';
	}
}

function site_show_loading()
{
	var loading = YAHOO.util.Dom.get('loading');
	if (loading != null)
	{
		YAHOO.util.Dom.setStyle(loading,'display','block');
		if (!waitForImage)
		{
			waitForImage = window.setInterval('site_hide_loading()',500);
		}
	}
}

function site_hide_loading()
{
	var loading = YAHOO.util.Dom.get('loading');
	if (loading != null)
	{
		var hide = true;

		for (var i=0; i < loading_img.length; i++)
		{
			if (typeof loading_img[i] != 'undefined')
			{
				hide = hide && loading_img[i].complete;
			}
		}

		if (hide)
		{
			YAHOO.util.Dom.setStyle(loading,'display','none');
			if (waitForImage)
			{
				window.clearInterval(waitForImage);	
			}
			waitForImage = false;
		}
	}
}

