function thisMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function javascript_to_flash() {
	var textfromflash = thisMovie("logoflash").asFunc();
}

Page= {
	dimensions: null,
	
	init: function() {
		var flashvars = {};
		var params = {};
		var attributes = {};
		swfobject.embedSWF("_flash/holter_sound.swf", "logoflash", "0", "1", "9.0.0", "/swfobject/expressInstall.swf", flashvars, params, attributes); 

		$("#logoSound").hover(
			function () {
				javascript_to_flash();
			},
			function () {
				javascript_to_flash();
			}
		);		
		
		Press.init();		
		Page.set();
		//Shops.init();
		
		$(".lightbox").lightBox();
		Util.externalLinks();
		Search.init();
		Newsletter.init();
		
		if($("#message").length>0) {
			$("#overlay, #message").click(function() {
				$("#overlay, #message").hide();
			});
			$("#overlay").show().css("height", parseInt($("#page").height())+parseInt($("#footer").height()));
		}
		
		if($("#contentslider_hide").length>0) {
			$("#contentslider_hide").click(function() { 
				$(this).fadeOut("fast");
				var marginLeft= parseInt($("#content .box").width());
				$("#content .box").animate({
					marginLeft: marginLeft
				}, 500);
				$("#contentslider_show").fadeIn("fast");
			});
		}
		if($("#contentslider_show").length>0) {
			$("#contentslider_show").click(function() {
				$("#contentslider_show").fadeOut("fast");
				$("#content .box").animate({
					marginLeft: 0
				}, 500);
				$("#contentslider_hide").fadeIn("fast");
			});
		}
	},
	set: function() {
		Page.dimensions= Util.getWindowSize();
		
		Page.setDimensions();
		//fallback

		if(Page.dimensions[0]<=1024) {
			$("#page").css("width", "1000px").css("margin-left", "-20px");
			$("#page .frame").css("padding-left", 360)
		}
	},
	setDimensions: function() {
		pageHeight= parseInt($("#page").height());
		manipulators= parseInt($("#page").css("padding-bottom"))+parseInt($("#page").css("padding-top"))+parseInt($("#page").css("margin-top"))+parseInt($("#page").css("margin-top"));
		if(Page.dimensions[1]-manipulators>pageHeight) {
			$("#page").css("height", Page.dimensions[1]-manipulators);
		}
		$("#content div.content").css("min-height", Page.dimensions[1]-manipulators-118);

		$("#shopcontrolframe").css("height", Page.dimensions[1]-manipulators-118-$("#shops").height());
		$("#footerend").css("top", parseInt($("#page").height())-18).show();
		$("#spare").css("height", Page.dimensions[1]-manipulators-118-$("#start").height());
		                                              
		//special for shops
		if($("#directionsPanel").css("display")=="block") {
			$("#shopcontrolframe").css("height", "auto");
		}
	}
}

Newsletter= {
	init: function() {
		
	},
	show: function() {
		dimensions= Util.getWindowSize();
		width= dimensions[0];
		height= parseInt($("#page").height())+parseInt($("#footer").height());
		$("#overlay").css("height", height);
		$("#newsletterform").css("top", (height-490)/2).css("left", (width-320)/2);
		$("#overlay, #newsletterform").show();
		$("#newsletterform .close").click(function() {
			Newsletter.hide();
		});
	},
	hide: function() {
		$("#overlay, #newsletterform").hide();
	}
}

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()+23);
			$("#searchresults").animate({
				top: "23px"
			}, 500, 'swing', function() {
				Search.active= true;
			});
		}
		Search.hideLoader();
	},
	hideResults: function() {
		if(Search.active) {
			postop= ($("#searchresults").height()+23)*-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();
	}
}

Press= {
	init: function() {
		Press.reset();
		$("#press .preview:first").hide();
		$("#press .article:first").show();
		$("#press .preview").click(function() {
			Press.reset();
			$(this).hide();
			$("#article"+$(this).attr("id").replace(/preview/, '')).show();
		});
	},
	reset: function() {
		$("#press .article").hide();
		$("#press .preview").show();
	}
}

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";
			}
		}
	}

}
