Page= {
	height: 0,
	
	init: function() {
		$('a.news.grey').css({ opacity: 0.6 });
		var dimensions= Util.getWindowSize();
		Page.height= dimensions[1];

		Languages.init();
		
		Util.externalLinks();
		Search.init();
		Video.init();		
		if($('.print_retailers').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($("#form_country").length>0) {
			$("#form_country").change(function() {
				/*$("#form_country option:selected").each(function () {
					switch($(this).attr("value")) {
						case 'GR':
						case 'IR':
					    case 'IL':
						case 'IN':
						case 'LU':
						case 'SK':
						case 'SI':
						case 'US':
						case 'CZ':
							url= location.href;
							url = url.substr(0, url.lastIndexOf("/")+1)+$(this).attr("value")+'#scrolltocontent';
							location.href= url;
							break;
					}
				});*/
			});
		}
		
		Page.setHeight();
		window.setTimeout(Page.setHeight, 2000);
		
		$("#footer .press").hover(function() {
			$("#presssub").show();
			$("#presssub ul").animate({
				bottom: "0"
			}, 400, 'swing', function() {
				Search.active= true;
			});
		}, function() {
			
		}); 
		$("#presssub").hover(function() {
			$(this).show();
		}, function() {
			$("#presssub ul").animate({
				bottom: -74
			}, 400, 'swing', function() {
				$("#presssub").hide();
			});
		});
		
		$("#footer .news").hover(function() {
			$("#newssub").show();
			$("#newssub ul").animate({
				bottom: "0"
			}, 400, 'swing', function() {
				Search.active= true;
			});
		}, function() {
			
		}); 
		$("#newssub").hover(function() {
			$(this).show();
		}, function() {
			$("#newssub ul").animate({
				bottom: -74
			}, 400, 'swing', function() {
				$("#newssub").hide();
			});
		});
		
		$(".colorbox").colorbox();
	},
	setHeight: function() {
		var menu= $("#page .menu").height();
		var main= $("#page .main").height();
		if(menu<main) {
			$("#page .menu").css("height", main);
		}
	}
}

Retailers={
	init: function(){
		$(".print_retailers .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 = $("a.language.active").attr("href");
			window.open(current_lang+"/print/retailers?"+data,"","menubar=0,scrollbars=1,width=680");
			//window.open("/de/print/retailers?"+data,"","menubar=0,scrollbars=1,width=640,height=auto");
		});
	}
}

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() {
		$("#searchresults_frame").show();
		if(!Search.active) {
			$("#searchresults").css("bottom", '-'+$("#searchresults").height());
			$("#searchresults").animate({
				bottom: "0"
			}, 500, 'swing', function() {
				Search.active= true;
			});
		}
		Search.hideLoader();
	},
	hideResults: function() {
		if(Search.active) {
			posbottom= ($("#searchresults").height())*-1;
			$("#searchresults").animate({
				bottom: posbottom
			}, 500, 'swing', function() {
				Search.active= false;
				$("#searchresults_frame").hide();
			});
		}
		Search.hideLoader();
	},
	showLoader: function() {
		$("#form_search .loader").show();
	},
	hideLoader: function() {
		$("#form_search .loader").hide();
	}   

}

Languages= {
	init: function() {
		$(".languages .language").hover(function() {
			$(this).animate({
				'margin-left' : '82px'
			}, 100, 'swing', function() {
				$('.full', $(this)).show();
				$('.initial', $(this)).hide();
			});
		}, function() {
			$(this).animate({
				'margin-left' : '0'
			}, 100, 'swing'	, function() {
					$('.full', $(this)).hide();
					$('.initial', $(this)).show();
				});
		});
	}
}

Kitchen= {
	current: 0,
	focus: false,
	
	init: function() {
		$("#focus img").load(function() {
			$(this).fadeIn("fast");
		});
		
		$("#gallery .image").click(function() {
			Kitchen.current= $(this).attr("id").replace(/gallery/, '');
			image= $("#focus"+Kitchen.current).attr("rel");
			
			$("#gallery .image").removeClass("active");
			$(this).addClass("active");
			
			$("#focus img.focus").fadeOut("fast", function() {
				$(this).attr("src", image);
			});
		});
		
		$("#focus").click(function() {
			//nothing yet
		});
	}
}

Video= {
	posbottom: 0,
	
	init: function() {
		Video.posbottom= ($("#videos").height()-$("#videos h2").height()-10)*-1;
		$("#videos").css("bottom", Video.posbottom).show();
		
		$("#videos h2").click(function() {
			$("#videos").animate({
				bottom: 0
			}, 500, 'swing', function() {
				$("#videos .close").show();
			});
		});
		
        $("#videos .close").click(function() {
			$("#videoplayer .iframe").html("");
			$(".video").removeClass("active");
			$("#videos .close").hide();
			
			$("#videos").animate({
				bottom: Video.posbottom
			}, 500, 'swing', function() {
				
			});
		});
		
		$("#videos .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());
		})
	}
}

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;
	}      
}
Gallery= {
	init: function() {
		$(".galleries .gallery").each(function() {
			var gallery= $(this).attr("id");
			$(".image", $(this)).click(function() { 
				$("#zoom_"+gallery+" img").attr("src", $(this).attr("id"));
			});
		});
	}
}

