/**
 * @author Sebastian Romero
 * Zemoga Inc. This file contains the general scripts for the site
 */
$(function(){
	
	
	/**
	 * 
	 */
	var showMainMenu = function(element){
		if(element.parent().parent().find("li:first-child").is(":visible")){
			$("#site-wrapper").animate({
				"right" : 220
			}, function(){
				$("#mobile-menu").css({
					"left" : "auto",
					"right" : 110
				}).show();
				element.html("Close");
			});
		} else {
			$("#site-wrapper").animate({
				"width" : 220,
				"margin-left" : $(document).width() - 220
			}, function(){
				$("#mobile-menu").css({
					"right" : "auto",
					"left" : 5
				}).show();
				element.html("Close");
			});
		}
	};
	
	/**
	 * 
	 */
	var closeMainMenu = function(element){
		$("#site-wrapper").animate({
			"width" : "100%",
			"margin-left" : 0,
			"right" : 0
		}, function(){
			$("#mobile-menu").hide();
			element.html("Menu");
		});
	};
	
	
	/**
	 * 
	 */
	var createVimeoFrame = function(){
		var frame = null;
		if($("#z-reel-frame-vimeo").size() == 0){
			frame = $(document.createElement("iframe"));
			frame.attr("src", "http://player.vimeo.com/video/31808235?title=0&amp;byline=0&amp;portrait=0");
			frame.attr("id", "z-reel-frame-vimeo");
		}
		return frame;
	};
	
	
	/**
	 * 
	 */
	var createCloseButtonFrame = function(){
		var close = $(document.createElement("div"));
		close.addClass("close-z-reel");
		close.html('<a id="close-z-reel" title="Close the Zreel" href="javascript:;">CLOSE THE ZREEL</a>');
		return close;
	};
	
	
	
	/**
	 * 
	 */
	$(document).ready(function(){
		/**
		 * 
		 */
		$("#show-menu-button").click(function(event){
			event.preventDefault();
			if($(this).html() !== "Close"){
				void showMainMenu($(this));
			} else {
				void closeMainMenu($(this));
			}
		});
		
		
		$("#view-z-reel").click(function(event){
			var frame = createVimeoFrame();
			if(frame){
				var close = createCloseButtonFrame();
				var scope = $(this);
				$("#slide-element").append(frame);
				$("body").append(close);
				close.find("#close-z-reel").click(function(e){
					frame.remove();
					scope.show();
					$(this).remove();
				});
				scope.hide();
			}
		});
		
		/**
		 * 
		 */
		$(window).bind("orientationchange", function(event){
			if((window.orientation == 90) != (window.orientation == - 90)){
				if($("#mobile-menu").is(":visible")){
					$("#show-menu-button").html("Menu");
					void closeMainMenu();
				}
			}
		});
		
	});
});

