var Bipsa = new Object();


/**
 *@autor:Sebastian Romero
 * 
 */
Bipsa = {
	
	str_baseTitle : "Bipsa ",
	
	/**
	 * 
	 */
	initialize : function(){
		void Bipsa.initAddress()
		void Bipsa.adjustPageInstances();
		void Bipsa.setOnResize();
		void Bipsa.setMenu();
	},
	
	/**
	 * 
	 */
	initAddress : function(){
		$.address.change(function(event) {
			Bipsa.gotoSection(event.value);
		}); 
	},
	
	
	/**
	 * 
	 * @param {String} section
	 */
	gotoSection : function (section){
		switch(section.replace("/", "")){
			case "home":
			case "":
				$(".main-menu").hide("slow");
				$(".menu a").removeClass("selected"); 
				$.scrollTo("#home", 1000, {easing:'easeInOutQuad'}); 
				$.address.title(Bipsa.str_baseTitle + "| Interactive Developer" );
			break;
			default :
				$.scrollTo("#" + section.replace("/", ""), 1000, {easing:'easeInOutQuad'});
				$(".main-menu").show("slow");
				$(".menu a").removeClass("selected");
				$("." + section.replace("/", "") + " a").addClass("selected");
				$.address.title(Bipsa.str_baseTitle + "| Interactive Developer | " +  section.replace("/", ""));
			break;
		}
	},
	
	/**
	 * 
	 */
	adjustPageInstances : function(){
		var padding = 50, iter = 0;
		$(".page").each(function(index, page){
			if(index%3 === 0)iter++;
			$(page).css({
				width:Page.getWindowSize().windowWidth + padding,
				height:Page.getWindowSize().windowHeight + padding,
				left:(index * (Page.getWindowSize().windowWidth + padding)),
				top: (iter * (Page.getWindowSize().windowHeight))
			});
		})
	}, 
	
	/**
	 * 
	 */
	setOnResize : function(){
		$(window).resize(function(event){
			void Bipsa.adjustPageInstances();
			Bipsa.gotoSection($.address.value());
		});
	},
	
	/**
	 * 
	 */
	setMenu : function(){
		$(".menu a").each(function(index, link){
			$(link).click(function(event){
			});
		});
		$(".linkPage").click(function(event){
			$.scrollTo("#" + $(this).attr("class").replace(" linkPage", ""), 1000, {easing:'easeInOutQuad'});
			switch($(this).attr("class").replace(" linkPage", "")){
				case "html5" :
					void Bipsa.drawHTML5Page();
				break;
				default:
			}
		});
	},
	
	/**
	 * 
	 */
	drawHTML5Page : function(){
		//$(Bipsa.drawPolaroid($("#html5").get(0), "assets/images/polaroid.gif")).css("top", 100);
		Bipsa.drawPhotos($("#html5").get(0), ["1", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3", "2", "3"])
	},
	
	/**
	 * 
	 * @param {HTMLObject} content
	 * @param {String} imagePath
	 */
	drawPolaroid : function(content, imagePath){
		var canvas = document.createElement("canvas"), contentCanvas, image = new Image();
		contentCanvas = canvas.getContext("2d");
		image.onload = function(){
			canvas.setAttribute("width",image.width + 150);
			canvas.setAttribute("height",image.height + 180);
			contentCanvas.rotate(15 * (Math.PI / 180));
			contentCanvas.fillStyle = "rgb(255,255,255)";
        	contentCanvas.fillRect (130, 0, image.width + 10, image.height + 20);
			contentCanvas.drawImage(image, 135, 5, image.width, image.height);
			
		};
		image.src = imagePath;
		content.appendChild(canvas);
		return canvas;
	},
	
	/**
	 * 
	 * @param {Array} photos
	 * @param {HTMLObject} content
	 */
	drawPhotos : function (content, photos){
		if($(content).find("canvas").size() >0) return;
		var canvas = document.createElement("canvas"), contextCanvas, rotation, xPos, yPos, width = 120, height = 80;
		if(canvas.getContext){
			canvas.setAttribute("width",$(content).width());
			canvas.setAttribute("height",$(content).height());
			contextCanvas = canvas.getContext("2d");
			for (var i = 0; i<photos.length; i++) {
				rotation = (Math.floor(Math.random()*360)) * (Math.PI / 180);
				xPos = Math.floor(Math.random()*$(content).width());
				yPos = Math.floor(Math.random()*$(content).height())
				contextCanvas.fillStyle = "rgb(255,255,255)";
				contextCanvas.fillRect(xPos, yPos, width, height);
				contextCanvas.fillStyle = "rgb(0,130,255)";
				contextCanvas.fillRect(xPos + 3, yPos + 3, width - 6, height -12);
				contextCanvas.rotate(rotation);
			}
			content.appendChild(canvas);
		}
	}
}

/**
 * 
 * @param {JqueryObject} event
 */
$(function(event){
	void Bipsa.initialize();
});