var ie = false;
var imgs = new Array();
var timer;

var mouseX;
var mouseY;

var toolTipTimer;

function mousePos(event){
	mouseX = Event.pointerX(event);
	mouseY= Event.pointerY(event);
}

var emailDomain = 'erzrf-cnpxneg.sv'; //remes-packart.fi
function showEmail(to,domain,text) {
	if(!domain || typeof(domain) == "undefined")
		domain = emailDomain;

	var email = to+'@'+domain;
	if(!text || typeof(text) == "undefined")
		text = email;

	return '<a href="'+rot13('znvygb:'+email)+'">'+rot13(text)+'</a>';
}

function showEmails() {
	var emails = $$('.hiddenemail');
	for(i = 0; i < emails.length; i++) {

		var t = $(emails[i]).select('.to');
		var to = t.length ? t[0].value : null;

		var d = $(emails[i]).select('.domain');
		var domain = d.length ? d[0].value : null;

		var t = $(emails[i]).select('.text');
		var text = t.length ? d[0].value : null;

		$(emails[i]).update(showEmail(to,domain,text));
	}
}

Event.observe(window, 'load',showEmails);

//this function is called when the page is loaded
function init(){
	center();
}

//this function is called when the page is resized or loaded
//center the content vertically
//disabled
function center(){
	//calculate the needed top margin for the page to be in the middle of the viewport
	//var topMarg = Math.round((document.viewport.getHeight() - $('height').offsetHeight)/2);
	//apply the top margin if it is positive, else set it to 0
	//if (topMarg < 0) topMarg = 0;
	//$('all').style.top = topMarg+'px';
}

//mouseover for the navi
function over(div){
	if (div.src.indexOf('current.png') == -1) div.src = div.src.replace('link.png', 'current.png');
}

//mouseout for the navi
function out(div){
	if(div.className == 'link')
		div.src = div.src.replace('current.png', 'link.png');
}

//set new image that will be used by the image roulette
function image(url,title,url2){
	imgs[imgs.length] = {url:url,title:title,url2:url2};
}

//start fading random images (that have the .changingImg -class)
function fadeRandomImage(){
	//return false;
	//find all images with the class .changingImg
	var changable = $$('.changingImg');

	//generate a random number 0-[amount of images]
	var i = Math.floor(Math.random()*changable.length);
	//generate an id for the original image
	var orig = Element.identify(changable[i]);
		//changable[i].parentNode.innerHTML += '<img src="'+imgs[Math.floor(Math.random()*imgs.length)]+'" alt="Thumb" class="changingImg" id="fadeIn" />';
	//clone the original image
	var cloned = Element.clone(changable[i]);
	//give the cloned image the id: "fadeIn"
	cloned.id = "fadeIn";
	//change the src of the cloned image
	var im = imgs[Math.floor(Math.random()*imgs.length)];
	cloned.src = im.url;
	cloned.title = im.title;
	cloned.alt = im.title;
	cloned.onclick = function() {
		openImage(this,im.url2,1)
	}
	cloned.onmouseover = function() {
		toolTip(im.title);
	}
	cloned.onmouseout = function() {
		hideTooltip();
	}

	//insert the cloned image to the document
	Element.insert(changable[i].parentNode, cloned);

	//fade out the original image
	Effect.Fade(orig, {duration: 0.75});
	//remove the new image and fade in the new image when the original has been faded out
	setTimeout('Element.remove("'+orig+'");fadeInNewImg();', 800);

	//this function calls itself again and again in 3500 ms interval
	timer = setTimeout('fadeRandomImage();', 3500);
}

//stop the roulette
function stopFade(){
	clearTimeout(timer);
	timer = -1;
}

//fade in a new image and remove the fadeIn -id
function fadeInNewImg(){
	Effect.Appear('fadeIn', {duration: 0.75});
	setTimeout('$("fadeIn").id="";', 100);
}

//open a larger view of an image
function openImage(t, url, enabled){

	if(!parseInt(enabled)) {
		return false;
	}

	//stop the roulette
	//stopFade();
	//hide the text in the bottom of the page
	$('text').style.visibility = 'hidden';
	//show the image on the right or on the left side depending on the pos-parameter

	var images = $$('.thumb2');
	var n = 1;
	for(var i = 0; i < images.length; i++) {
		if($(images[i]) == $(t.parentNode)) {
			pos = n <= 2 ? 'left' : 'right';
			break;
		}
		if(n == 4) n=1;
		else n++;
	}

	$('leftImg').style.display = 'none';
	$('rightImg').style.display = 'none';
	if (pos == 'left'){
		$('bigImgLeft').src = url;
		$('leftImg').style.display = 'block';
	}
	if (pos == 'right'){
		$('bigImgRight').src = url;
		$('rightImg').style.display = 'block';
	}

}

//close the larger view of an image
function closeImage(){
	//if the timer has been started, don't start it again
	if (timer == -1) fadeRandomImage();
	//hide image and show the text in the bottom of the page
	$('leftImg').style.display = 'none';
	$('rightImg').style.display = 'none';
	$('bigImgRight').src = '';
	$('bigImgLeft').src = '';
	$('text').style.visibility = 'visible';
}

function hover(id,client,src) {

	if(typeof($('thumbText').onmouseout) == 'function') {
		$('thumbText').onmouseout();
	}

	t = $('p'+id);
	t2 = $('pd'+id);
	if(!t.orig || typeof(t.orig) == "undefined")
		t.orig = t.src;

	t.src = src;
	t.style.width = "135px";
	t.style.height = "83px";
	/*$('thumbText').innerHTML = client;
	$('thumbText').style.display = 'block';
	Element.clonePosition($('thumbText'), t2,{setWidth:false,setHeight:false});
	//$('thumbText').style.height = 'auto';
	$('thumbText').onclick = function(){document.location.href=t.parentNode.href;}
	$('thumbText').onmouseout = function(){ unHover(id); }
	$('thumbText').setStyle({height:'auto',paddingTop:'0px',paddingBottom:'0px'});
	//$('thumbText').style.marginTop = (73 - $('thumbText').getHeight()) +'px';
	//88*/

	var pb = 12;
	var dh = 97-pb;
	var pt = (dh - $('thumbText').getHeight());
	var h = (dh-pt);
	$('thumbText').setStyle({
		height:h+'px',
		paddingTop:pt+'px',
		paddingBottom:pb+'px'
	});
}

//mouseout for the thumbs
function unHover(id){
	t = $('p'+id);
	t.style.width = "124px";
	t.style.height = "76px";
	t.src = t.orig;
	$('thumbText').style.display = 'none';
}

//set result of ajax request to a div
function load(url, div){
	$(div).innerHTML = '<div id="ajaxLoading"></div>';
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(transport) {
	    $(div).innerHTML = transport.responseText;
	  }
	});
}

//rot 13 function
function rot13(str){ //v1.0
	return str.replace(/[a-zA-Z]/g, function(c){
		return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
	});
};

function changeProductImage(link,src) {
	var image = $('productImage').select('img');

	if(image && image.length) {

  		obj = image[0];
		new Effect.Parallel([
		  new Effect.Move(obj, { sync: true, x: 800, y: 0, mode: 'relative' })
		  //new Effect.Opacity(obj, { sync: true, from: 1, to: 1 })
		], {
		  duration: 1.2,
		  delay: 0.0,
		  afterFinish: function() {

		  		obj.src=src;
			  obj.setStyle({'left':'-800px'});
		  },
		  queue: 'end'
		});

		new Effect.Parallel([
		  new Effect.Move(obj, { sync: true, x: 800, y: 0, mode: 'relative' }),
		  new Effect.Opacity(obj, { sync: true, from: 0, to: 1 })
		], {
		  duration: 1.2,
		  delay: 0.0,
		  queue: 'end'
		});
	}

	var links = $('imageLinks').select('a');
	for(i=0;i<links.length;i++)
		$(links[i]).removeClassName('current');

	$(link).addClassName('current');
}

function toolTip(text){
	if(text.length > 0 && text != " " && $('toolTip').style.display != 'block' ){
		clearTimeout(toolTipTimer);
		toolTipTimer = setTimeout("$('toolTip').style.display='block';", 250);
	}
	$('toolTip').innerHTML = text;
	$('toolTip').style.left = mouseX+"px";
	$('toolTip').style.top = mouseY-25+"px";
	if (text.length == 0) hideTooltip();
}

function hideTooltip(){
	$('toolTip').style.display='none';
	clearTimeout(toolTipTimer);
	toolTipTimer = setTimeout("$('toolTip').style.display='none';", 400);
}