var log = function () {};

if (typeof console == "object" && typeof console.firebug == "string") {
	//log = console.log;
}

function getWindowDimensions(){
	
	var height, width;
	
	if (typeof window.innerWidth != 'undefined'){
		
		width = window.innerWidth;
		height = window.innerHeight;

	} else if ((typeof document.documentElement != 'undefined') && (typeof document.documentElement.clientWidth != 'undefined') && (document.documentElement.clientWidth != 0)){

		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	
	} else {
		
		width = document.getElementsByTagName('body')[0].clientWidth;
		height = document.getElementsByTagName('body')[0].clientHeight;
	}
	
	return {width:width, height:height };
}


function parseName(s) {

	return s.split("/").pop().split(".jpg")[0];
}

var logError = function(error){

	log(error);

	if (error.stack) {

		log("Error stack:");

		if (error.stack) {

			error.stack.match(/\/(\w*)\.js:(\d*)/g).each(function(line){log("   " + line);	});
		}
	}
};

var App = Class.create();

App.prototype = {

	version: "0.04",

	initialize: function(){

		log("Initializing...");

		this.fullImages = $A($("full").childNodes).findAll(function(image){
			return image.tagName && image.tagName.toUpperCase() == "IMG";
		});

		this.links = $A($("preview").childNodes).findAll(function(link){
			return link.tagName && link.tagName.toUpperCase() == "A" && link.className != "buy";
		});

		this.sideLinks = $A($("productlist").childNodes).findAll(function(item){
			return item.tagName && item.tagName.toUpperCase() == "LI";
		});

		this.title = document.title.split(" - ")[0].toLowerCase();
		
		log(this.title);

		this.initFull();
		this.initLinks();
		this.initSideBar();
		this.initForm();
		
		$("preview").className = "";

		log("Ready!");
	},
	
	initForm: function(){
		
		try {
					
			Event.observe("form_close", "click", function(e){
				
				Element.hide("order");
				Event.stop(e);
			});

			Event.observe("form_okay", "click", function(e){
				
				Element.hide("order");
				Event.stop(e);
			});
			
			Event.observe("show_form", "click", function(e){
				
				$("order").style.height = Math.max($("main").clientHeight, getWindowDimensions().height) + "px";
				
				Element.show("order");
				
				Element.show("form_content");
				Element.hide("form_sent");
				
				$("order").className = "";
				
				$("form_name").focus();
				$("form_name").select();
				
				Event.stop(e);
				
				document.location.hash = "#";

			});	
			
			
			
			Event.observe("form_send", "click", (function(e){
				
				Event.stop(e);
				
				var correctInputs = true;
				
				if (!$("form_name").value){
					
					$("form_name").className = "error";
					$("form_name").focus();
					correctInputs = false;
					
				} else {
					
					$("form_name").className = "";
				}
				
				log($("form_name").value + ":: " + $("form_name").value);				
				
				if (!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($("form_email").value))){
					
					$("form_email").className = "error";
					$("form_email").focus();
					correctInputs = false;
					
				} else {
					
					$("form_email").className = "";
				}
				
				if (correctInputs){
				
					var vars = "";
					
					vars += "name=" 	+ encodeURIComponent($("form_name").value);
					vars += "&email=" 	+ encodeURIComponent($("form_email").value);
					vars += "&title=" 	+ encodeURIComponent(document.title.split(" - ")[0]);
					vars += "&url=" 	+ encodeURIComponent(document.location.href);
					
					new Ajax.Request(  
						"http://www.missgeschickladylapsus.de/mail/ajax/", 
						{
							postBody: vars, 
							onComplete: function(r){
																
								$("form_sent_message").innerHTML = r.responseText;

								$("form_content").className = "";
								Element.hide("form_content");
								Element.show("form_sent");
							}
						}
					);
					
					$("form_content").className = "sending";

				}
				
			}).bind(this));	
			
		} catch(e){}
	},

	initFull: function(){

		this.fullImages.each( function(c) {
			c.id = "photo_" + parseName(c.src);
		});
	},

	initLinks: function(){

		var _this = this;

		this.links.each( function(c) {

			log(c);

			Event.observe(c, "click", function(e){
				_this.showImage("photo_" + parseName(c.href));
				Event.stop(e);
			});
		});
	},
	
	initSideBar: function(){
		
		var _this = this;
		
		this.sideLinks.each(function(li) {
				
			var isSelected = li.firstChild.innerHTML.toLowerCase().match(_this.title);
			
			if (isSelected) {
											 
				log("found: " + li);
				li.className = "selected";
				
			} 
										  
			Event.observe(li.firstChild, "mouseover", function(l){
				
				li.className = "hover";
				log(li);
			});
			
			Event.observe(li.firstChild, "mouseout", function(l){
				
				if (isSelected) {
					
					li.className = "selected";	
				} 
				else {
					li.className = "";	
				}
				log(li);
			});

		});
	},

	hideAll: function(){

		this.fullImages.each(function(c){

			log(c);
			c.className = "hidden";
		});
	},

	showImage: function(id){

		this.hideAll();
		
		log($(id).src.split("/").pop() );
		
		if( $(id).src.split("/").pop() == "full.jpg") {
			
			$(id).className = "";
			
		} else {
			
			$(id).className = "preview";
		}
	}
};

var main = null;

var init = function(){
		
	log("init Called");
		
	try {

		if (!main) {
			
			main = new App();
		}

	} catch (err) {

		logError(err);
	}
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	
    document.addEventListener("DOMContentLoaded", init, false);
	
} else if (/MSIE/i.test(navigator.userAgent)) {
	
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
	
} else if (/WebKit/i.test(navigator.userAgent)) { 
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            
			clearInterval(_timer);
			init(); // call the onload handler
        }
    }, 10);
}


Event.observe(window, "load", function() {

	init();
});

if (/MSIE/i.test(navigator.userAgent)){
	
	document.execCommand("BackgroundImageCache",false,true);
}

