// 
//  main.js
//  js
//  
//  Created by Zoltán Fehér on 2009-01-25.
//  Copyright 2009 Design Candy. All rights reserved.
// 

var postcard_down = false;
var pause_image = new Image(73,74); 
	pause_image.src = "/images/web/pause.png"; 
var play_image_url =  "/images/web/play.png"; 
var previous_url = "";
var next_url = "";
var countdown;

// =================================
// = fancy glass hover link action =
// =================================
function glassLinkHover() {
	obj = document.getElementById('title');
	obj.style.color = "#04363c";
	obj.style.textShadow = "#90a3c0 0 1px 1px";
}
function glassLinkOut () {
	obj = document.getElementById('title');
	obj.style.color = "#c9eeff";
	obj.style.textShadow = "#b9c9e4 0 0 5px";
}

// =============================================================
// = this is for getting the VERTICAL position of the POSTCARD =
// =============================================================
function getPostcardPos() {
	var patt1 = new RegExp("[-0-9]+");
	return patt1.exec(document.getElementById('postcard').style.top);
}

// =====================================================
// = try to slide POSTCARD DOWN if in initial position =
// =====================================================
function slideDownPostcard () {
	$('click_here').hide();
	var pos = getPostcardPos();
	if (pos == -407 || pos === null) {
		new Effect.Move('postcard', {y: 360, mode: 'relative', transition: Effect.Transitions.spring});
		postcard_down = true;
	};
}

// =======================================================
// = try to slide POSTCARD UP if in slided down position =
// =======================================================
function slideBackPostcard () {
	if (getPostcardPos() == -47) {
		new Effect.Move('postcard', {y: -360, mode: 'relative', transition: Effect.Transitions.spring});
		postcard_down = false;
	};
}

// ====================================
// = handle PLAY/PAUSE BUTTON presses =
// ====================================
function togglePlaying () {
	var play_button = document.getElementById('play_button');
	if (! playing) {
		play_button.style.backgroundImage = "url(" + pause_image.src + ")";
		playing = true;
		countdown = setTimeout('slideImage (-1, next_url)', 5000);
	} else {
		play_button.style.backgroundImage = "url(" + play_image_url + ")";
		playing = false;
		clearTimeout(countdown);
	}
}

// ==========================
// = get the DOCUMENT WIDTH =
// ==========================
function getDocumentWidth () {
	if (document.documentElement 
		&& document.documentElement.offsetWidth 
		&& document.documentElement.offsetWidth > 0) 
	{
		return document.documentElement.offsetWidth;
	} else {
		return document.body.offsetWidth;
	}
}

// ===================
// = page navigation =
// ===================
function goToPage(page) { window.location = page;}
function slideImage (direction, link) {
	if (link != "#") {
		var w = ((getDocumentWidth() - content_width)/2 + content_width + 20) * direction;
		new Effect.Move('content', {x: w, mode: 'relative', transition: Effect.Transitions.spring});
		linkk = new String (link);
		if (playing) { linkk += "/slideshow" };
		setTimeout("goToPage(linkk)", 600);
	} else {
		new Effect.Shake('content');
		if (playing) { togglePlaying() };
	}
}

// =========================
// = KEY CATCHING routines =
// =========================
function catchKey(Event) {
	Event.stopPropagation();
	// Note: for IE, you would use this declaration:
	window.cancelBubble = true;
	
	switch (Event.keyCode) {
		case 37:
			slideImage (1, previous_url);
			break;
		case 39:
			slideImage (-1, next_url);
			break;
		case 32:
			togglePlaying();
			new Effect.Appear('play_button', { duration: 1.0 });
		default:
	}
}

function watchForKeys() {
	// document.addEventListener("keydown", catchKey, false);
	document.addEventListener("keyup", catchKey, false);
}

function showClickHere () {
	new Effect.Move('click_here', {y: 120, mode: 'relative', transition: Effect.Transitions.sinoidal, duration: 0.6});
	setTimeout(function() {
		// Effect.DropOut('click_here');
		Effect.Fade('click_here', { duration: 0.5 });
	}, 4500);
}

function checkFirstVisit () {
	if (cookiesAllowed())
		if ( ! getCookie('visited')) {
			showClickHere();
			setCookie('visited','true', 10);
		}
}

// ===================================
// = execute on PAGE LOAD COMPLETION =
// ===================================
function onLoad () {
    jQuery(document).pngFix(); 
	new Effect.Appear('content', { duration: 1.0 });
	watchForKeys();
	// checkForPlaybackEnabled
	if (playing) {
		countdown = setTimeout('slideImage (-1, next_url)', 5000);
		play_button.style.backgroundImage = "url(" + pause_image.src + ")";
	}
	checkFirstVisit();
}

// $(document).ready(function(){ 
// }); 
