/*##############################################################################
	I N F O T E X T
##############################################################################*/

var infoText						= new Array();
infoText['error']					= new Array();
infoText['help']					= new Array();

infoText['error']['captcha']					= '<h1>Achtung</h1>Der Sicherheitscode ist falsch.';
infoText['error']['wrongEcard']					= '<h1>Sorry</h1>Das ausgewählte Bild kann nicht versendet werden!!!';
infoText['error']['competition_Email_Exist']	= '<h1>Sorry</h1>Eine Person mit der angegebenen Email-Adresse, hat bereits am Wettbewerb teilgenommen.';
infoText['error']['emailaddress_exists']		= '<h1>Sorry</h1>Die angegebene Emial-Adresse ist bereits registriert.';

infoText['help']['captcha']						= '<h1>Sicherheitscode</h1><p><img src="/inc/captcha/captcha.info.png" border="0" alt="" /></p>Setze den im Bild enthaltenen Sicherheitscode in das Eingabefeld ein. Achte auf Gross- und Kleinschreibung.';



/*##############################################################################
	setContainerHeight
##############################################################################*/

function setContainerHeight()
{
	var ct = $("mainContainer");
	ct.style.height = "auto";
	var ctHeight = ct.offsetHeight;
	
	if(!$("htmlBody")) {
		var bd = document.getElementsByTagName("body")[0];
		bd.id = "htmlBody";
		bd = $("htmlBody");
	} else { bd = $("htmlBody"); }
	
	var bdHeight = bd.getSize().y;
	if(bdHeight > ctHeight) ct.style.height = bdHeight + "px";
}

window.addEvent("load",setContainerHeight);
window.addEvent("resize",setContainerHeight);

function setSmiley(smiley) { $("comment").value += " " + smiley; }

/*##############################################################################
	setButtonFX
##############################################################################*/

function setButtonFX(bt)
{
	bt.onmouseover = function()	{ this.src = this.src.replace("_normal","_hover"); }
	bt.onmousedown = function()	{ this.src = this.src.replace("_hover","_click"); }
	bt.onmouseout = function()	{ this.src = this.src.replace("_hover","_normal"); }
	bt.onmouseup = function()		{ this.src = this.src.replace("_click","_hover"); }
	if(!setButtonFX.arguments[1]) bt.onmouseover();
}

/*##############################################################################
	initZoomBox
##############################################################################*/

function initZoomBox()
{
	var blackBox = $("blackBox");
	var img = $("zoomBox_Image");
	var btClose = $("zoomBox_btClose");
	var btNext = $("zoomBox_btNext");
	var btPrev = $("zoomBox_btPrev");
	var btEcard = $("zoomBox_btEcard");
	var btFacebook = $("zoomBox_btFacebook");
	var errorBox = $("errorBox");
	
	blackBox.setOpacity(0.7);
	
	setButtonFX(btClose,1);
	setButtonFX(btNext,1);
	setButtonFX(btPrev,1);
	setButtonFX(btEcard,1);
	setButtonFX(btFacebook,1);
	
	blackBox.onclick = function()
	{
		img.style.display = "none";
		this.style.display = "none";
		btClose.style.display = "none";
		btNext.style.display = "none";
		btPrev.style.display = "none";
		btEcard.style.display = "none";
		btFacebook.style.display = "none";
		errorBox.style.display = "none";
		$("mirrorBall").style.display = "block";
	}
	
	btClose.onclick = function() { blackBox.onclick(); }
}

window.addEvent("load",function(){ if($("blackBox")){initZoomBox();}});

/*##############################################################################
	setGalleryControllButtons
##############################################################################*/

function setGalleryControllButtons(img)
{
	
	// Parameter 0 = Bildpfad
	// Parameter 1 = Navibuttons
	// Parameter 2 = Ecardbutton
	// Parameter 3 = Facebookbutton
	var blackBox = $('blackBox');
	var naviButtons = (setGalleryControllButtons.arguments[1]) ? 1 : 0;
	var ecardButton = (setGalleryControllButtons.arguments[2]) ? 1 : 0;
	var facebookButton = (setGalleryControllButtons.arguments[3]) ? 1 : 0;
	
	var btClose = $("zoomBox_btClose");
	var btNext = $("zoomBox_btNext");
	var btPrev = $("zoomBox_btPrev");
	var btEcard = $("zoomBox_btEcard");
	var btFacebook = $("zoomBox_btFacebook");
	
	var imgWidth = img.offsetWidth/2;
	var imgHeight = img.offsetHeight/2;
	var winWidth = window.getSize().x/2;
	var winHeight = window.getSize().y/2;
	
	btClose.style.left = winWidth + imgWidth - 1 + "px";;
	btClose.style.top = winHeight - imgHeight + window.getScroll().y - 20 + "px";
	btClose.onclick = function() { blackBox.onclick(); }
	btClose.style.display = "block";
	
	if(naviButtons)
	{
		btNext.onclick = function() { loadNextGalleryImage(1); }
		btNext.style.left = winWidth + imgWidth + 10 + "px";
		btNext.style.top = winHeight + window.getScroll().y - 16 + "px";
		
		btPrev.onclick = function() { loadNextGalleryImage(0); }
		btPrev.style.left = winWidth - imgWidth - 42 + "px";
		btPrev.style.top = winHeight + window.getScroll().y - 16 + "px";
		
		btNext.style.display = "block";
		btPrev.style.display = "block";
		
		img.style.cursor = "pointer";
		img.onclick = function() { loadNextGalleryImage(1); }
	}
	
	if(ecardButton)
	{
		btEcard.style.left = (facebookButton) ? winWidth - 16 - 21 + "px" : winWidth - 16 + "px";
		btEcard.style.top = winHeight + window.getScroll().y - imgHeight - 42 + "px";
		
		btEcard.onclick = function() {
			var frm = $("zoomBox_ecard_formular");
			$("zoomBox_ecard_imageID").value = img.src;
			frm.method = "POST";
			frm.action = "/page/ecards/index.php";
			frm.submit();
		}
		btEcard.style.display = "block";
	}
	
	if(facebookButton)
	{
		btFacebook.style.left = winWidth - 16 + 21 + "px";
		btFacebook.style.top = winHeight + window.getScroll().y - imgHeight - 42 + "px";
		
		btFacebook.onclick = function() {
			var u = "http://shakeadelic.ch/page/share/?imgID="+img.src;
			var t = "Shakeadelic";
			window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
		}
		btFacebook.style.display = "block";
	}
	
}

/*##############################################################################
	zoomBox
##############################################################################*/

function zoomBox(imgPath)
{
	// Parameter 0 = Bildpfad
	// Parameter 1 = Navibuttons
	// Parameter 2 = Ecardbutton
	// Parameter 3 = Facebookbutton
	var naviButtons = (zoomBox.arguments[1]) ? 1 : 0;
	var ecardButton = (zoomBox.arguments[2]) ? 1 : 0;
	var facebookButton = (zoomBox.arguments[3]) ? 1 : 0;
	
	var blackBox = $("blackBox");
	var img = $("zoomBox_Image");
	var btClose = $("zoomBox_btClose");
	var btNext = $("zoomBox_btNext");
	var btPrev = $("zoomBox_btPrev");
	
	img.style.display = "none";
	
	blackBox.style.width = window.getSize().x + "px";
	blackBox.style.height = $("mainContainer").getSize().y + "px";
	
	hgb_x = (window.getSize().x/2) - 16 + "px";
	hgb_y = (window.getSize().y / 2) - 16 + window.getScroll().y + "px";
	blackBox.style.backgroundPosition = hgb_x + " " + hgb_y;
	
	$("mirrorBall").style.display = "none";
	
	blackBox.style.display = "block";
	img.src = imgPath;
	img.onload = function()
	{
		this.setOpacity(0);
		this.style.display = "block";
		this.style.left = (window.getSize().x/2) - (this.offsetWidth/2) + "px";
		this.style.top = (window.getSize().y / 2) - (this.offsetHeight/2) + window.getScroll().y + "px";
		setGalleryControllButtons(this,naviButtons,ecardButton,facebookButton);
		this.setOpacity(1);
	}
}

/*##############################################################################
	mp3Player
##############################################################################*/

function mp3Player(src)
{
	console.log(src);
	var flashvars = false;
	var width = 250;
	var height = 250;
	var params = {
		movie: src,
		bgcolor:"#000000",
		allowscriptaccess:"always"
	};
	var attributes = {};
	
	var player = $('mp3PlayerContainer');
	var blackBox = $("blackBox");
	var btClose = $("zoomBox_btClose");
	
	blackBox.style.width = window.getSize().x + "px";
	blackBox.style.height = $("mainContainer").getSize().y + "px";
	
	hgb_x = (window.getSize().x/2) - 16 + "px";
	hgb_y = (window.getSize().y / 2) - 16 + window.getScroll().y + "px";
	blackBox.style.backgroundPosition = hgb_x + " " + hgb_y;
	
	blackBox.style.display = "block";
	swfobject.embedSWF(src, "mp3Player", width, height, "8.0.0", "expressInstall.swf", flashvars, params, attributes);
	player.style.display = "block";
	player.style.left = (window.getSize().x/2) - (player.offsetWidth/2) + "px";
	player.style.top = (window.getSize().y / 2) - (player.offsetHeight/2) + window.getScroll().y + "px";
	
	blackBox.onClickBackup = blackBox.onclick;
	blackBox.onclick = function()
	{
		this.onClickBackup();
		player.style.display = "none";
		$('mp3Player').innerHTML = "";
		//initZoomBox();
	}
	setGalleryControllButtons(player);
}

/*##############################################################################
	errorBox
##############################################################################*/

function errorBox(msg)
{
	var blackBox = $("blackBox");
	var eb = $("errorBox");
	var btClose = $("zoomBox_btClose");
	
	blackBox.style.width = window.getSize().x + "px";
	blackBox.style.height = $("mainContainer").getSize().y + "px";
	
	hgb_x = (window.getSize().x/2) - 16 + "px";
	hgb_y = (window.getSize().y / 2) - 16 + window.getScroll().y + "px";
	blackBox.style.backgroundPosition = hgb_x + " " + hgb_y;
	
	$("mirrorBall").style.display = "none";
	
	blackBox.style.display = "block";
	eb.innerHTML = msg;
	eb.style.display = "block";
	eb.style.left = (window.getSize().x/2) - (eb.offsetWidth/2) + "px";
	eb.style.top = (window.getSize().y / 2) - (eb.offsetHeight/2) + window.getScroll().y + "px";
	setGalleryControllButtons(eb);
}

/*##############################################################################
	initGalleryThumbnail
##############################################################################*/

var activeGalleryImage = null;

function initGalleryThumbnail(ct,id)
{
	ct.imgID = id;
	
	for(i=0;i<ct.childNodes.length;i++)
	{
		if(ct.childNodes[i].className == "gallery_thumb_img") {
			ct.thumb = ct.childNodes[i];
			break;
		}
	}
	
	ct.onclick = function()
	{
		activeGalleryImage = this.imgID;
		zoomBox(pics[this.imgID],1,1,1);
	}
	ct.onmouseout = function() { this.style.borderColor = "#2B2B2B"; }
	ct.onmouseover = function() { this.style.borderColor = "#FFFFFF"; }
}

if($$(".gallery_thumb_container")) {
	window.addEvent("load",function(){
		var cts = $$(".gallery_thumb_container");
		var id = 0;
		$each(cts,function(el){
			initGalleryThumbnail(el,id);
			id++;
		});
	});
}

/*##############################################################################
	initGalleryThumbnail
##############################################################################*/

function loadNextGalleryImage(next)
{
	activeGalleryImage = (next) ? activeGalleryImage+1 : activeGalleryImage-1;
	if(activeGalleryImage > pics.length-1) activeGalleryImage = 0;
	if(activeGalleryImage == -1) activeGalleryImage = pics.length-1;
	
	zoomBox(pics[activeGalleryImage],1,1,1);
}

/*##############################################################################
	loadNextGalleryPage
##############################################################################*/

var gallerySiteID = 0;

function loadNextGalleryPage(next)
{
	if(next) { gallerySiteID = ((gallerySiteID+1) == pageCount) ? 0 : gallerySiteID+1; }
	else { gallerySiteID = (gallerySiteID == 0) ? pageCount-1 : gallerySiteID-1; }
	
	var startID = gallerySiteID * maxPixPerPage;
	var cts = $$(".gallery_thumb_container	");
	var idCount = 0;
	$each(cts,function(el){
		el.thumb.style.display = "none";
		el.imgID = startID + idCount;
		el.thumb.src = thumbs[el.imgID];
		if(el.imgID < thumbs.length) {
			el.style.visibility = "visible";
			el.thumb.onload = function() { this.style.display = "block"; }
		} else {
			el.style.visibility = "hidden";
			el.thumb.src = "";
		}
		idCount++;
	});
	$("galleryPageCounter").innerHTML = (gallerySiteID+1) + " / " + pageCount;
}

/*##############################################################################
	formCheck
##############################################################################*/

function formCheck(frm,sendTo)
{
	function checkMail(str)
	{
		var a = false;
		var result = false;
		if(typeof(RegExp) == 'function')
		{
			var b = new RegExp('abc');
			if(b.test('abc') == true) { a = true; }
		}
		if(a == true)
		{
			reg = new RegExp('^([a-zA-Z0-9\-\.\_]+)'+
									'(\@)([a-zA-Z0-9\-\.]+)'+
									'(\.)([a-zA-Z]{2,4})$');
			result = (reg.test(str));
		}
		else
		{
			result = (s.search('@') >= 1 &&
			str.lastIndexOf('.') > str.search('@') &&
			str.lastIndexOf('.') >= str.length-5)
		}
		return(result);
	}
	
	var frm = $(frm);
	var error = new Array();
	var errorMessage = "<h1>Achtung</h1><div class='borderBottom'>Bitte alle mit einem * gekennzeichneten Felder korrekt ausfüllen.</div>";
	
	
	for(i=0;i<frm.elements.length;i++)
	{
		var el = frm.elements[i];
		if(el.className.indexOf("validate_email") > -1 && !checkMail(el.value)) { error.push(el.title); }
		else if(el.className.indexOf("validate") > -1 && el.value.length < 3) { error.push(el.title); }
	}
	
	if(error.length > 0)
	{
		$each(error,function(el)
		{
			errorMessage += "- " + el + " -<br />";
		});
		errorBox(errorMessage);
	} else {
		frm.action = sendTo;
		frm.method = "POST";
		frm.submit();
	}
}

/*##############################################################################
	ecard_setScrollButten
##############################################################################*/

function ecard_setScrollButten(bt,up)
{
	var ct = $("ecard_scrollConatiner")
	var scrl = 10;
	var speed = 50
	bt.onmousedown = function()
	{
		if(up) { this.onScroll = window.setInterval(function(){ct.scrollTop -= scrl;},speed); }
		else { this.onScroll = window.setInterval(function(){ct.scrollTop += scrl;},speed); }
	}
	bt.onmouseup = function() { window.clearInterval(this.onScroll); }
	bt.onmouseover = function() {}
}

/*##############################################################################
	addTextLink
##############################################################################*/

function addTextLink(area)
{
	var url = prompt("Gib die Linkadresse ein","http://");
	if(url)
	{
		var linkText = prompt("Gib den Link-Text ein");
		if(linkText)
		{
			$(area).value += '<a href="' + url + '" target="_blank">' + linkText + '</a>';
		}
	}
}

