Page= {
	height: 0,
	
	init: function() {
		
		var dimensions= Util.getWindowSize();
		Page.height= dimensions[1];
		
		Page.setHeight();
		Page.setLanguageMenu();
		
		Util.externalLinks();
		Search.init();
		Video.init();		
		
		if($('#news_image_thumbs').length>0){
			News.init();
		}	
		if($('#print_retailers, #print_retailers_right').length>0){
			Retailers.init();
		}
		
		if(!$("#userdata").attr("class").match("set")) {
			if(google.loader.ClientLocation) {
		        latitude = google.loader.ClientLocation.latitude;
		        longitude = google.loader.ClientLocation.longitude;
				country = google.loader.ClientLocation.address.country_code;
				city = google.loader.ClientLocation.address.city;

				$.ajax({
					url: '/helper/geo',
					data: 'latitude='+latitude+"&longitude="+longitude+"&country="+country+"&city="+city,
					type: 'GET',
					dataType: 'html',
					timeout: 5000,
					success: function(html) {
						//window.location.reload();
					}
				});
	   	    }
		}
		
		
		/*if($("#page .content").length>0 && $("#page .page").length>0) {
			var content = $("#page .content").height();
			var page_left= $("#page .page .left").height();

			if(page_left>0 && page_left<content) {
				var paddingtop= parseInt((content-page_left)/2)-20;
				$("#page .page .left").css("padding-top", paddingtop);
				$("#page .page .right").height(content-30);
		    }
		}*/
		
		if($("#form_retailer #form_country").length>0) {
			$("#form_retailer #form_country").change(function() {
				$("#form_retailer #form_country option:selected").each(function () {
					switch($(this).attr("value")) {
						case 'GR':
						case 'IR':
					    case 'IL':
						case 'LU':
						case 'SK':
						case 'SI':
						case 'CZ':
							url= location.href;
							url = url.substr(0, url.lastIndexOf("/")+1)+$(this).attr("value");
							location.href= url;
							break;
					}
				});
			});
		}
		
		$(".colorbox").colorbox();
	},
	setHeight: function() {
		$("#body").css("height", Page.height).css("background-image", "url(/_img/bg/footer.png)");
	},
	setLanguageMenu: function() {
		$("#languageselect .active").hover(function() {
			$("#languageselect li").show();
		}, function() {
			$("#languageselect li").hide();
			$("#languageselect li.active").show();
		});
		$("#languageselect li").hover(function() {
			$("#languageselect li").show();
		}, function() {
			$("#languageselect li").hide();
			$("#languageselect li.active").show();
		});
	}
}


Retailers={
	init: function(){
		$("#print_retailers .btn, #print_retailers_right .btn").click(function(){
			var data = "";
			var count=1;
			var first = true;
			$(".make_notice input:checked").each(function(){
				if(!first){
					data += "&";
				}				
				data += "retailer"+count+"="+$(this).attr("name");
				count++;
				first=false;
			});		
			var current_lang = $("#languageselect li.active a").attr("href");
			//window.open(current_lang+"/print/retailers?"+data,"","menubar=0,location=0,scrollbar=1,status=0,directories=0,width=680");		
			window.open(current_lang+"/print/retailers?"+data,"","menubar=0,scrollbars=1,width=680");
		});
	}
}
Configurator= {
	show: function(url) {
		var dimensions= Util.getWindowSize();		
		$("#shadow").show().css("height", dimensions[1]);
		//.css("top", parseInt(dimensions[1]-$("#configurator").height())/2)
		$("#configurator").css("left", parseInt(dimensions[0]-$("#configurator").width())/2).html('<iframe src="'+url+'" width="999" height="460" scrolling="no" frameborder="0"></iframe>').show();
		$("#configurator_close").css("left", (parseInt(dimensions[0]-$("#configurator").width())/2)+parseInt($("#configurator").width()-12)).show();
	}
}

News={
	init: function(){
		$('.news_image_thumb').hover(function(){
			$('#news_gallery .news_image_big').hide();
			$('#news_gallery .'+$(this).attr('id')).show();			
		},function(){
			$('#news_gallery .news_image_big').hide();
			$('#news_gallery .news_image_big.news_image').show();
			$('#news_gallery #news_image').addClass('active');
			
		});
	}
}

Video= {
	init: function() {
        $("#videoplayer .close").click(function() {
			$("#videoplayer .iframe").html("");
			$("#videoplayer").hide();
			$(".video").removeClass("active");
		});
		
		$(".video").click(function() {
			$("#videoplayer .iframe").html('<iframe src="'+$(this).attr("rel")+'" width="300" height="168" scrolling="no" frameborder="0"></iframe>');
			$("#videoplayer").show();
			$(".video").removeClass("active");
			$(this).addClass("active");
		});
	}
}

Servicevideos= {
	init: function() {
		$("#servicevideos1 .servicevideo, #servicevideos2 .servicevideo").click(function() {
			$("#servicevideos1 .servicevideo, #servicevideos2 .servicevideo").removeClass("active");
			$(this).addClass("active");
			$("#serviceplayer").html($(".video", $(this)).html());
		})
	}
}

Search= {
	active: false,
	
	init: function() {
		$("#search").hover(function() {
			$(this).get(0).focus();
		});
		$("#search").keyup(function() {
			Search.go();
		}).click(function() {
			Search.go();
		});
		$("#searchresults .close").click(function() {
			Search.hideResults();
		}); 
    },
	go: function() {
		Search.showLoader();
		if($("#search").val().length>=3) {
			$.ajax({
				url: '/helper/search',
				data: 'search='+$("#search").val(),
				type: 'POST',
				dataType: 'html',
				timeout: 5000,
				success: function(html) {
					$("#searchresults .list").html(html);
					Search.showResults();
				}
			});
		} else {
			Search.hideResults();
		}
	},
	showResults: function() {
		if(!Search.active) {
			$("#searchresults").css("top", '-'+$("#searchresults").height()+41);
			$("#searchresults").animate({
				top: "41px"
			}, 500, 'swing', function() {
				Search.active= true;
			});
		}
		Search.hideLoader();
	},
	hideResults: function() {
		if(Search.active) {
			postop= ($("#searchresults").height()+41)*-1;
			$("#searchresults").animate({
				top: postop
			}, 500, 'swing', function() {
				Search.active= false;
			});
		}
		Search.hideLoader();
	},
	showLoader: function() {
		$("#form_search .loader").show();
	},
	hideLoader: function() {
		$("#form_search .loader").hide();
	}   

}

Start= {
	switchPages: null,
	
	init: function() {	
		$(".start .box").css("visibility","hidden");		
		$(".subnavigation li").click(function() {
			clearTimeout(Start.switchPages);
			$(".start").hide();
			$("#start_"+$(this).attr("class").replace(/ active/, '')).show();
			$("#start_"+$(this).attr("class").replace(/ active/, '') +" .box").css("visibility","visible");
			$(".subnavigation li").removeClass("active");
			$(this).addClass("active");
		});	
		Start.switchPages= window.setTimeout(function() {
			Start.next();
		}, 7000);		
	},
	next: function() {
		$("#start_models").hide();
		$(".start").fadeOut("slow", function() {
			$("#start_models").fadeIn("slow");

			if($('#christmas').length == 0)
				$("#start_models .box").css("visibility","visible");
		});
		$(".subnavigation li").removeClass("active");		
		$(".subnavigation .models").addClass("active");
	}
}


Kitchen= {
	current: 0,
	
	init: function() {
		$("#focus img").load(function() {
			$(this).fadeIn("fast");
		});
		
		$("#focus").hover(function() {
			$(".hover", $(this)).show();
		}, function() {
			$(".hover", $(this)).hide();
		}).click(function() {
			image= $("#zoom"+Kitchen.current).attr("rel");
			$("#zoom img").attr("src", image);
		});
		
		$("#gallery .image").click(function() {
			Kitchen.current= $(this).attr("id").replace(/gallery/, '');
			image= $("#focus"+Kitchen.current).attr("rel");
			
			$("#focus img").fadeOut("fast", function() {
				$(this).attr("src", image);
			});
			
			$("#gallery .image .hover").hide();
			$("#gallery"+Kitchen.current+" .hover").css("display", "block");
		});
        $("#kitchen_slide, #focus").click(function() {
			Kitchen.toggleInfo();
		});
		$("#gallery .image:first .hover").css("display", "block");
	},
	toggleInfo: function() {
		if($("#kitchen_slide").hasClass("active")) {
			$("#kitchen_slide").removeClass("active");
			$("#info").animate({
				'right' : '0'
			}, 800, 'swing');
		} else {
			$("#kitchen_slide").addClass("active");
			$("#info").animate({
				'right' : '-313px'
			}, 800, 'swing');
		} 
	}
}

Gallery= {
	init: function() {
		$(".galleries .gallery").each(function() {
			var gallery= $(this).attr("id");
			$(".image", $(this)).click(function() { 
				$("#zoom_"+gallery+" img").attr("src", $(this).attr("id"));
			});
		});
	}
}

Util= {
	getWindowSize: function(){
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) {
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else if(document.body) { 
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		if(yScroll < windowHeight) {
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		if(xScroll < windowWidth) {
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}

		var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	},
	externalLinks: function() {
		//Source: http://www.sitepoint.com/print/standards-compliant-world/
		if (!document.getElementsByTagName) {
			return;
		}
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
				anchor.target = "_blank";
			}
		}
	},
	validateForm: function(id) {
		var result= true;
		$("#"+id + " input, #"+id + " textarea").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
			  // trim input field
			  $(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
				var itemresult= Util.validate($(this).attr("id"));
				result= result && itemresult;
			}
		});
		if(result) {
			$("#form_error").removeClass("error");	
		} else {
			$("#form_error").addClass("error");
		}
		return result;
	},
	validate: function(id) {
		var result= true;
		var obj= $("#"+id);
		var commands= $("#"+id).attr("class").split(" ");
		for(var i=0; i<commands.length; i++) {
			var command= commands[i].replace(/[0-9]/g, "");
			switch(command) {
				case("minlength"):
					var length= parseInt(commands[i].replace(/minlength/g, ""));
					result= result && (obj.val().length>=length);
					break;
				case("maxlength"):
					var length= parseInt(commands[i]);
					result= result && (obj.val().length<=length);
					break;
				case("email"):
					result = result && ( obj.get(0).value.match(/\S@\S.\S{2,}/)!=null )
					break;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}
		if(!result) {
			obj.addClass("error");
		} else {
			obj.removeClass("error");
		}
		return result;
	}      
}
