var QueryLoader = {
	/*
	 * QueryLoader		Preload your site before displaying it!
	 * Author:			Gaya Kessler
	 * Date:			23-09-09
	 * URL:				http://www.gayadesign.com
	 * Version:			1.0
	 * 
	 * A simple jQuery powered preloader to load every image on the page and in the CSS
	 * before displaying the page to the user.
	 */
	
	overlay: "",
	loadBar: "",
	preloader: "",
	items: new Array(),
	doneStatus: 0,
	doneNow: 0,
	selectorPreload: "body",
	ieLoadFixTime: 2000,
	ieTimeout: "",
		
	init: function() {
		if (navigator.userAgent.match(/MSIE (\d+(?:\.\d+)+(?:b\d*)?)/) == "MSIE 6.0,6.0") {
			//break if IE6			
			return false;
		}
		if (QueryLoader.selectorPreload == "body") {
			QueryLoader.spawnLoader();
			QueryLoader.getImages(QueryLoader.selectorPreload);
			QueryLoader.createPreloading();
		} else {
			$(document).ready(function() {
				QueryLoader.spawnLoader();
				QueryLoader.getImages(QueryLoader.selectorPreload);
				QueryLoader.createPreloading();
			});
		}
		
		//help IE drown if it is trying to die :)
		QueryLoader.ieTimeout = setTimeout("QueryLoader.ieLoadFix()", QueryLoader.ieLoadFixTime);
	},
	
	ieLoadFix: function() {
		var ie = navigator.userAgent.match(/MSIE (\d+(?:\.\d+)+(?:b\d*)?)/);
		if (ie[0].match("MSIE")) {
			while ((100 / QueryLoader.doneStatus) * QueryLoader.doneNow < 100) {
				QueryLoader.imgCallback();
			}
		}
	},
	
	imgCallback: function() {
		QueryLoader.doneNow ++;
		QueryLoader.animateLoader();
	},
	
	getImages: function(selector) {
		var everything = $(selector).find("*:not(script)").each(function() {
			var url = "";
			
			if ($(this).css("background-image") != "none") {
				var url = $(this).css("background-image");
			} else if (typeof($(this).attr("src")) != "undefined" && $(this).attr("tagName").toLowerCase() == "img") {
				var url = $(this).attr("src");
			}
			
			url = url.replace("url(\"", "");
			url = url.replace("url(", "");
			url = url.replace("\")", "");
			url = url.replace(")", "");
			
			if (url.length > 0) {
				QueryLoader.items.push(url);
			}
		});
	},
	
	createPreloading: function() {
		QueryLoader.preloader = $("<div></div>").appendTo(QueryLoader.selectorPreload);
		$(QueryLoader.preloader).css({
			height: 	"0px",
			width:		"0px",
			overflow:	"hidden"
		});
		
		var length = QueryLoader.items.length; 
		QueryLoader.doneStatus = length;
		
		for (var i = 0; i < length; i++) {
			var imgLoad = $("<img></img>");
			$(imgLoad).attr("src", QueryLoader.items[i]);
			$(imgLoad).unbind("load");
			$(imgLoad).bind("load", function() {
				QueryLoader.imgCallback();
			});
			$(imgLoad).appendTo($(QueryLoader.preloader));
		}
	},

	spawnLoader: function() {
		if (QueryLoader.selectorPreload == "body") {
			var height = $(window).height();
			var width = $(window).width();
			var position = "fixed";
		} else {
			var height = $(QueryLoader.selectorPreload).outerHeight();
			var width = $(QueryLoader.selectorPreload).outerWidth();
			var position = "absolute";
		}
		var left = $(QueryLoader.selectorPreload).offset()['left'];
		var top = $(QueryLoader.selectorPreload).offset()['top'];
		
		QueryLoader.overlay = $("<div></div>").appendTo($(QueryLoader.selectorPreload));
		$(QueryLoader.overlay).addClass("QOverlay");
		$(QueryLoader.overlay).css({
			position: position,
			top: top,
			left: left,
			width: width + "px",
			height: height + "px"
		});
		
		QueryLoader.loadBar = $("<div></div>").appendTo($(QueryLoader.overlay));
		$(QueryLoader.loadBar).addClass("QLoader");
		
		$(QueryLoader.loadBar).css({
			position: "relative",
			top: "50%",
			width: "0%"
		});
		
		QueryLoader.loadAmt = $("<div><p><span>0%</span> Pieni hetki ja Gloria Housesi on valmis.</p></div>").appendTo($(QueryLoader.overlay));
		$(QueryLoader.loadAmt).addClass("QAmt");
		
		$(QueryLoader.loadAmt).css({
			position: "relative",
			top: "45%",
			left: "0%",
			width: "100%",
			margin: "0px auto 0px auto"
		});
	},
	
	animateLoader: function() {
		var perc = (100 / QueryLoader.doneStatus) * QueryLoader.doneNow;
		if (perc > 99) {
			$('.QAmt span').html("100%");
			$(QueryLoader.loadBar).stop().animate({
				width: perc + "%"
			}, 500, "linear", function() { 
				QueryLoader.doneLoad();
			});
		} else {
			$(QueryLoader.loadBar).stop().animate({
				width: perc + "%"
			}, 500, "linear", function() { });
			$('.QAmt span').html(Math.floor(perc)+"%");
		}
	},
	
	doneLoad: function() {
		//prevent IE from calling the fix
		clearTimeout(QueryLoader.ieTimeout);
		
		//determine the height of the preloader for the effect
		if (QueryLoader.selectorPreload == "body") {
			var height = $(window).height();
		} else {
			var height = $(QueryLoader.selectorPreload).outerHeight();
		}
		
		//The end animation, adjust to your likings
		$(QueryLoader.loadAmt).hide();
		$(QueryLoader.loadBar).animate({
			height: height + "px",
			top: 0
		}, 500, "linear", function() {
			$(QueryLoader.overlay).fadeOut(500);
			$(QueryLoader.preloader).remove();
		});
	}
}


$(function() {

	/* INITIAL BACKGROUND */

	$.backstretch('img/bg09.jpg');


	/* BOX POSITION CALCULATION */

	$('#box4').css('left', ($(window).width() - 960) / 2 + 396 +'px');
	$('#box4-1').css('left', ($(window).width() - 960) / 2 + 281 +'px');
	$('#box4-2').css('left', ($(window).width() - 960) / 2 +'px');
	$('#box4-3').css('left', ($(window).width() - 960) / 2 +'px');
	$('#box4-4').css('left', ($(window).width() - 960) / 2 + 525 +'px');
	$('#box4-5').css('left', ($(window).width() - 960) / 2 +'px');
	$('#newsletterBox').css('left', ($(window).width() - 960) / 2 + 725 +'px');

	$(window).resize(function() {
		$('#box4').css('left', ($(window).width() - 960) / 2 + 396 +'px');
		$('#box4-1').css('left', ($(window).width() - 960) / 2 + 281 +'px');
		$('#box4-2').css('left', ($(window).width() - 960) / 2 +'px');
		$('#box4-3').css('left', ($(window).width() - 960) / 2 +'px');
		$('#box4-4').css('left', ($(window).width() - 960) / 2 + 525 +'px');
		$('#box4-5').css('left', ($(window).width() - 960) / 2 +'px');
		$('#newsletterBox').css('left', ($(window).width() - 960) / 2 + 725 +'px');
	});


	/* FRONTPAGE BOXES */

	$('#box1').show();

	$('#box1 .next').click(function(e) {
		e.preventDefault();
		$('#box1').hide();
		$('#box2').fadeIn(500);
	});

	$('#box2 .next').click(function(e) {
		e.preventDefault();
		if($('#landscape1').is(':checked') || $('#landscape2').is(':checked') || $('#landscape3').is(':checked')) {
			$('#box2').hide();
			$('#box3').fadeIn(500);
		}
	});

	$('#box3 .next').click(function(e) {
		e.preventDefault();
		if($('#house1').is(':checked') || $('#house2').is(':checked') || $('#house3').is(':checked')) {
			$('#box3').hide();
			$('#box4').fadeIn(500);
		}
	});

	$('#box4 .next').click(function(e) {
		e.preventDefault();
		$('#box4').hide();
		$('#box4-1').hide();
		$('#box4-2').hide();
		$('#box4-3').hide();
		$('#box4-4').hide();
		$('#box4-5').hide();
		$('#box5').fadeIn(500);
	});

	$('#box5 .next').click(function(e) {
		e.preventDefault();
		var landscape = $('#landscape').val();
		var house = $('#house').val();
		var more1 = $('#more1').is(":checked") ? 1 : 0;
		var more2 = $('#more2').is(":checked") ? 1 : 0;
		var more3 = $('#more3').is(":checked") ? 1 : 0;
		var more4 = $('#more4').is(":checked") ? 1 : 0;
		var more5 = $('#more5').is(":checked") ? 1 : 0;
		var name = $('#name').val();
		var email = $('#email').val();
		var phone = $('#phone').val();
		var info = $('#info ').val();

		// Sending request
		$.ajax({
			type: 'POST',
			data: 'cmd=newOrder&landscape='+ landscape +'&house='+ house +'&more1='+ more1 +'&more2='+ more2 +'&more3='+ more3 +'&more4='+ more4 +'&more5='+ more5 +'&name='+ name +'&email='+ email +'&phone='+ phone +'&info='+ info,
			url: 'services/receiver.php',
			success: function(msg) {
				$('#box5').hide();
				$('#box6').fadeIn(500);
			}
		});
	});

	$('#box3 .prev').click(function(e) {
		e.preventDefault();
		$('#box3').hide();
		$('#box2').fadeIn(500);
	});

	$('#box4 .prev').click(function(e) {
		e.preventDefault();
		$('#box4').hide();
		$('#box4-1').hide();
		$('#box4-2').hide();
		$('#box4-3').hide();
		$('#box4-4').hide();
		$('#box4-5').hide();
		$('#box3').fadeIn(500);
	});

	$('#box2 #link2-1').click(function(e) {
		e.preventDefault();
		$('#landscape').val(1);
		$('input[name=landscape1]').attr('checked', true);
		$('input[name=landscape2]').attr('checked', false);
		$('input[name=landscape3]').attr('checked', false);

		$('#backstretch img').fadeOut(250, function() {
			$('#backstretch img').attr('src', 'img/bg10.jpg');
			$('#backstretch img').fadeIn(250);
		});
	});

	$('#box2 #landscape1').click(function(e) {
		$('#landscape').val(1);
		$('input[name=landscape1]').attr('checked', true);
		$('input[name=landscape2]').attr('checked', false);
		$('input[name=landscape3]').attr('checked', false);

		$('#backstretch img').fadeOut(250, function() {
			$('#backstretch img').attr('src', 'img/bg10.jpg');
			$('#backstretch img').fadeIn(250);
		});
	});

	$('#box2 #link2-2').click(function(e) {
		e.preventDefault();
		$('#landscape').val(2);
		$('input[name=landscape1]').attr('checked', false);
		$('input[name=landscape2]').attr('checked', true);
		$('input[name=landscape3]').attr('checked', false);

		$('#backstretch img').fadeOut(250, function() {
			$('#backstretch img').attr('src', 'img/bg20.jpg');
			$('#backstretch img').fadeIn(250);
		});
	});

	$('#box2 #landscape2').click(function(e) {
		$('#landscape').val(2);
		$('input[name=landscape1]').attr('checked', false);
		$('input[name=landscape2]').attr('checked', true);
		$('input[name=landscape3]').attr('checked', false);

		$('#backstretch img').fadeOut(250, function() {
			$('#backstretch img').attr('src', 'img/bg20.jpg');
			$('#backstretch img').fadeIn(250);
		});
	});

	$('#box2 #link2-3').click(function(e) {
		e.preventDefault();
		$('#landscape').val(3);
		$('input[name=landscape1]').attr('checked', false);
		$('input[name=landscape2]').attr('checked', false);
		$('input[name=landscape3]').attr('checked', true);

		$('#backstretch img').fadeOut(250, function() {
			$('#backstretch img').attr('src', 'img/bg30.jpg');
			$('#backstretch img').fadeIn(250);
		});
	});

	$('#box2 #landscape3').click(function(e) {
		$('#landscape').val(3);
		$('input[name=landscape1]').attr('checked', false);
		$('input[name=landscape2]').attr('checked', false);
		$('input[name=landscape3]').attr('checked', true);

		$('#backstretch img').fadeOut(250, function() {
			$('#backstretch img').attr('src', 'img/bg30.jpg');
			$('#backstretch img').fadeIn(250);
		});
	});

	$('#box3 #link3-1').click(function(e) {
		e.preventDefault();
		$('#house').val(1);
		var landscape = $('#landscape').val();
		$('input[name=house1]').attr('checked', true);
		$('input[name=house2]').attr('checked', false);
		$('input[name=house3]').attr('checked', false);

		$('#backstretch img').fadeOut(0, function() {
			$('#backstretch img').attr('src', 'img/bg'+ landscape +'1.jpg');
			$('#backstretch img').fadeIn(0);
		});
	});

	$('#box3 #house1').click(function(e) {
		$('#house').val(1);
		var landscape = $('#landscape').val();
		$('input[name=house1]').attr('checked', true);
		$('input[name=house2]').attr('checked', false);
		$('input[name=house3]').attr('checked', false);

		$('#backstretch img').fadeOut(0, function() {
			$('#backstretch img').attr('src', 'img/bg'+ landscape +'1.jpg');
			$('#backstretch img').fadeIn(0);
		});
	});

	$('#box3 #link3-2').click(function(e) {
		e.preventDefault();
		$('#house').val(2);
		var landscape = $('#landscape').val();
		$('input[name=house1]').attr('checked', false);
		$('input[name=house2]').attr('checked', true);
		$('input[name=house3]').attr('checked', false);

		$('#backstretch img').fadeOut(0, function() {
			$('#backstretch img').attr('src', 'img/bg'+ landscape +'2.jpg');
			$('#backstretch img').fadeIn(0);
		});
	});

	$('#box3 #house2').click(function(e) {
		$('#house').val(2);
		var landscape = $('#landscape').val();
		$('input[name=house1]').attr('checked', false);
		$('input[name=house2]').attr('checked', true);
		$('input[name=house3]').attr('checked', false);

		$('#backstretch img').fadeOut(0, function() {
			$('#backstretch img').attr('src', 'img/bg'+ landscape +'2.jpg');
			$('#backstretch img').fadeIn(0);
		});
	});

	$('#box3 #link3-3').click(function(e) {
		e.preventDefault();
		$('#house').val(3);
		var landscape = $('#landscape').val();
		$('input[name=house1]').attr('checked', false);
		$('input[name=house2]').attr('checked', false);
		$('input[name=house3]').attr('checked', true);

		$('#backstretch img').fadeOut(0, function() {
			$('#backstretch img').attr('src', 'img/bg'+ landscape +'3.jpg');
			$('#backstretch img').fadeIn(0);
		});
	});

	$('#box3 #house3').click(function(e) {
		$('#house').val(3);
		var landscape = $('#landscape').val();
		$('input[name=house1]').attr('checked', false);
		$('input[name=house2]').attr('checked', false);
		$('input[name=house3]').attr('checked', true);

		$('#backstretch img').fadeOut(0, function() {
			$('#backstretch img').attr('src', 'img/bg'+ landscape +'3.jpg');
			$('#backstretch img').fadeIn(0);
		});
	});

	$('#box4 #link4-1').click(function(e) {
		e.preventDefault();
		if($('input[name=more1]').attr('checked') === false) {
			$('input[name=more1]').attr('checked', true);
		}
		else {
			$('input[name=more1]').attr('checked', false);
		}
		$('#box4-1').toggle();
	});

	$('#box4 #more1').click(function(e) {
		$('#box4-1').toggle();
	});

	$('#box4 #link4-2').click(function(e) {
		e.preventDefault();
		if($('input[name=more2]').attr('checked') === false) {
			$('input[name=more2]').attr('checked', true);
		}
		else {
			$('input[name=more2]').attr('checked', false);
		}
		$('#box4-2').toggle();
	});

	$('#box4 #more2').click(function(e) {
		$('#box4-2').toggle();
	});

	$('#box4 #link4-3').click(function(e) {
		e.preventDefault();
		if($('input[name=more3]').attr('checked') === false) {
			$('input[name=more3]').attr('checked', true);
		}
		else {
			$('input[name=more3]').attr('checked', false);
		}
		$('#box4-3').toggle();
	});

	$('#box4 #more3').click(function(e) {
		$('#box4-3').toggle();
	});

	$('#box4 #link4-4').click(function(e) {
		e.preventDefault();
		if($('input[name=more4]').attr('checked') === false) {
			$('input[name=more4]').attr('checked', true);
		}
		else {
			$('input[name=more4]').attr('checked', false);
		}
		$('#box4-4').toggle();
	});

	$('#box4 #more4').click(function(e) {
		$('#box4-4').toggle();
	});

	$('#box4 #link4-5').click(function(e) {
		e.preventDefault();
		if($('input[name=more5]').attr('checked') === false) {
			$('input[name=more5]').attr('checked', true);
		}
		else {
			$('input[name=more5]').attr('checked', false);
		}
		$('#box4-5').toggle();
	});

	$('#box4 #more5').click(function(e) {
		$('#box4-5').toggle();
	});


	/* NEWSLETTER */

	$('#newsletter').click(function(e) {
		e.preventDefault();
		$('#newsletterBox').toggle();
	});

	$('#newsletterOrder').click(function(e) {
		e.preventDefault();
		var email = $('#newsletterEmail').val();

		// Sending request
		$.ajax({
			type: 'POST',
			data: 'cmd=newSubscription&email='+ email,
			url: 'services/receiver.php',
			success: function(msg) {
				$('#newsletterContent').fadeOut(function() {
					$('#newsletterContent').html('<span>Kiitos tilauksestasi</span>');
					$('#newsletterContent').fadeIn();
				});
			}
		});
	});

});


(function($) {

	/*
	* jQuery Backstretch 1.0
	* http://srobbin.com/jquery-plugins/jquery-backstretch/
	*
	* Add a dynamically-resized background image to the page
	*
	* Copyright (c) 2009 Scott Robbin (srobbin.com)
	* Dual licensed under the MIT and GPL licenses.
	*/

	$.backstretch = function(src, options, callback) {
		var settings = {
			hideUntilReady: true, // Hide the image until it's finished loading
			speed: 0 // fadeIn speed for background after image loads (e.g. "fast" or 500)
		},
		imgRatio;

		// Extend the settings with those the user has provided
		if(options && typeof options == "object") $.extend(settings, options);

		// Initialize
		$(document).ready(_init);

		// For chaining
		return this;

		function _init() {
			// Prepend image, wrapped in a DIV, with some positioning and zIndex voodoo
			if(src) {
				var commonCSS = {left: 0, top: 0},
				wrap = $("<div />").attr("id", "backstretch-wrap")
				.css( $.extend(commonCSS, {position: "absolute", zIndex: 1}) ),
				container = $("<div />").attr("id", "backstretch")
				.css( $.extend(commonCSS, {position: "fixed", overflow: "hidden", zIndex: 1}) )
				.appendTo(wrap),
				img = $("<img />").attr("src", src)

				.bind("load", function() {
					var self = $(this);
					imgRatio = self.width() / self.height();
					_adjustBG(function() {
						if( settings.hideUntilReady )
						self.fadeIn(settings.speed, function(){
							// Callback, if necessary
							if(typeof callback == "function") callback();
						});
					});
				});

				if(settings.hideUntilReady) img.hide();
				img.appendTo(container);

				$("body").prepend(wrap);

				// Adjust the background size when the window is resized
				$(window).resize(_adjustBG);
			}
		}

		function _adjustBG(callback) {
			var bgWidth = $(window).width(),
			bgHeight = bgWidth / imgRatio;

			if(bgHeight < $(window).height()) {
				bgHeight = $(window).height();
				bgWidth = bgHeight * imgRatio;
			}

			$("#backstretch img").width( bgWidth ).height( bgHeight );

			if (typeof callback == "function") callback();
		}
	};

})(jQuery);

function clearMe(formfield){
	if (formfield.defaultValue==formfield.value) {
		formfield.value = "";
	}
}

