$(document).ready(function(){
	
	var a = document.location.href.split("#");
	a = a[0].split("/");
	var page = a[a.length - 1];
	if(page.substr(-4) != ".php" && page.substr(-4) != "html"){
		page = "index.php";
	}
	
	$(".sf-menu li a").each(function(el, i){
		if($(this).attr("href") == page){
			$(this).addClass("active");
		}
	});
	
	// for Facebook iFrame embedding
	var inFrame = (window.location != window.parent.location);
	if(inFrame){
		$("html").css("overflow", "hidden");
	} 
	
	$("body")
		.addClass(
				$(window).width() < 767 ? $(window).width() < 480 ? "view-portrait" : "view-landscape" : "view-large" 
		)
	
	if($(window).width() < 767){
		$(".newsletter-menu").parent().prepend($(".newsletter-menu"));
	}
	
	// reposition aside
	if($(window).width() < 767){
		$(".aside").parent().parent().append($(".aside").parent());		
	}
	
	$(window).resize(function() {
		
		var windowWidth = $(window).width();
		var changedView = false;
		var viewClass = $("body").hasClass("view-portrait") ? "view-portrait" : $("body").hasClass("view-landscape") ? "view-landscape"  : "view-large" 
		
		if(windowWidth < 767){
			$(".newsletter-menu").parent().prepend($(".newsletter-menu"));
			$(".aside").parent().parent().append($(".aside").parent());		
			if(windowWidth < 480){
				if(viewClass != "view-portrait"){
					changedView = true;
					$("body")
						.addClass("view-portrait")
						.removeClass("view-landscape")
						.removeClass("view-large")
				}
			}else{
				if(viewClass != "view-landscape"){
					changedView = true;
					$("body")
						.addClass("view-landscape")
						.removeClass("view-portrait")
						.removeClass("view-large")
				}
			}
		}else{
			$(".newsletter-menu").parent().append($(".newsletter-menu"));
			$(".aside").parent().parent().prepend($(".aside").parent());
			if(viewClass != "view-large"){
				changedView = true;
				$("body")
					.addClass("view-large")
					.removeClass("view-portrait")
					.removeClass("view-landscape")
			}
		};

		if(changedView){
			closeBetaPopUp();
		}
		
	});

	function closeBetaPopUp(){
		$(".trigger")
			.unbind();
			
		$("#section-tophat")
			.animate({
				"margin-top":"0px",
				"padding-top":"10px"
			}, function(){
				$(this)
					.find(".trigger")
					.unbind()
					.click(openBetaPopUp)
					.find(".explanation")
					.fadeOut()
			})

		
	}

	function openBetaPopUp(){
		if($("body").hasClass("view-large")){
			$(this)
				.unbind()
				.find(".explanation")
				.css({
					"display":"block",
					"margin-top":"-150px"
				})
				.animate({"margin-top":"40px"}, function(){ $(this).parent().unbind().click(closeBetaPopUp); })
		}
		
		if($("body").hasClass("view-landscape")){
			$(this)
				.unbind()
				.find(".explanation")
				.css({
					"display":"block",
					"margin-top":"-200px"
				})
				
			$("#section-tophat")
				.css({
					"margin-top":"-30px",
					"padding-top":"30px"
				})
				.animate({"margin-top":"200px","padding-top":"30px"}, function(){$(this).find(".trigger").unbind().click(closeBetaPopUp);})

		}
		
		if($("body").hasClass("view-portrait")){
			$(this)
				.unbind()
				.find(".explanation")
				.css({
					"display":"block",
					"margin-top":"-248px"
				})
			
			$("#section-tophat")
				.css({
					"margin-top":"-30px",
					"padding-top":"30px"
				})
				.animate({"margin-top":"245px","padding-top":"30px"}, function(){$(this).find(".trigger").unbind().click(closeBetaPopUp);})
		}

		
	}
	
	$(".trigger")
		.click(openBetaPopUp)
	
	$(".roundPreview")
		.each(function(){
			$(this)
				.css({
					"background-image" : "url("+$(this).find("img.roundPreviewImage").attr("src")+")"
				})
				.addClass("round")
				.empty()
				.next()
				.css({
					"margin-top" : 250
				})
				
		})

}
)
