/* ------------------------------------------------------------------------
 * 	Content Controller
 * 	Use: Loading and displaying content
 * 	Author: Elevations Marketing (http://www.elevations.ca)
 * 	Version: 1.0.0
 ------------------------------------------------------------------------- */

$(function() {

	/*********** GLOBAL VARIABLES **************/
	var contentOpen = false;
	var contentPos = 40;  //Set The Final Content window position when it animates in '.products, .products a'
	var btmContentPos = 0;  //Set The Final Content window position when it animates in '.products, .products a'
	var pagePath = "/ontario";  //Location of files to load;
	var navItemID = "";
	var targetDivID = "";
		
	var pageLoaded = ['showroom', 'news']; //adding 'showroom' is a hack - couldn't get the lightbox to work because content was being injected

	/********** USEFUL FUNCTIONS ***********/
	posContent = function (i) {
		//console.log ("posContent function called - targeting: " +i);
		var contentWin = $(i).outerHeight();								//Set Height of content
		
		if ((i == "#newsLoad") || (i == "#clearanceLoad") ) {				//would like to make this more dynamic but what ever.
			$(i).css({
				bottom: "-" + contentWin + "px", 
				height: contentWin + "px"
			});
		} else {
			$(i).css({
				top: "-" + contentWin + "px", 
				height: contentWin + "px"
			});
		};
		//console.log(":::Content Position has been set");
		
		return contentWin;
	};
	
	showMe = function (i) {
		//console.log ("showMe function called");
		
		if ((targetDivID == "#newsLoad") || (targetDivID == "#clearanceLoad") ) {
			$((navItemID + " a")).attr({
				rel: 'address:'		
			});
			$("#page-wrap").css("overflow", "hidden"); //prevent scroll bars from appearing while it moves into place
			
			$(targetDivID).animate({
				"bottom": btmContentPos +"px"
			},{ 
				duration: 1000, 
				easing: 'easeOutCubic'
			});
				
			$(navItemID).animate({
				"bottom": i + btmContentPos +"px"
			}, 1000, 'easeOutCubic' );
			
			
		} else {
			$((navItemID + " a")).attr({
				id: "close",
				rel: 'address:'		
			});
			
			$(targetDivID).animate({
				"top": contentPos +"px"
			},{ 
				duration: 1000, 
				easing: 'easeOutCubic'
			});
				
			$(navItemID).animate({
				"top": i + contentPos +"px"
			}, 1000, 'easeOutCubic' );
		};
		contentOpen = true;
	}
	
	closeMe = function () {
		//console.log ("closeMe function called");
	
		if ((targetDivID == "#newsLoad") || (targetDivID == "#clearanceLoad") ) {
			$((navItemID + " a")).attr({
				rel: "address:"+navItemID.replace("#","")
			});
			
			$(targetDivID).animate({
				"bottom": - $(targetDivID).outerHeight() + "px"
			}, {
				duration: 700, 
				easing: 'easeOutCubic'
			});
			
			$(navItemID).animate({
				"bottom": "0px"
			}, {
				duration: 700,
				easing: 'easeOutCubic'
			});
			
		} else {
			$((navItemID + " a")).attr({
				id: "",
				rel: "address:"+navItemID.replace("#","")
			});
			
			$(targetDivID).animate({
				"top": - $(targetDivID).outerHeight() + "px"
			}, 700, 'easeOutCubic');
			
			$(navItemID).animate({
				"top": "0px"
			},{
				duration: 700,
				easing: 'easeOutCubic'
				//complete: 
			});	
		}
			
	};
	
	/********** CONTENT LOADING ***********/	
	function showContent (page) {
		//console.log("Been asked to show content " + page);
		var path = pagePath + page  + '.html';				//HTML page path
		var cleaned = page.replace("/","");					//Removes "/" from address variable pass
		
		navItemID = "#"+ cleaned;						//what class should be applied
		targetDivID = "#" + cleaned + "Load";  			// Should target #productsLoad / #showroomLoad / #historyLoad / #contactLoad
		
		//console.log("TargetDiv = " + targetDivID );
		
		if ($.inArray(cleaned, pageLoaded) == -1 ) {
			//console.log("Loading: " + page);
			
			pageLoaded.push(cleaned);							//Content has been injected into the DOM - Don't do it again - update the array
			
			$(targetDivID).load(path + ' ' + navItemID + 'Content', function (){
				showMe(posContent(targetDivID));
			});
		} else {
			//console.log("NOT LOADING ANYTHING");
			//console.log("pageLoaded array Dump");
			//console.log("---------------------");
			//$.each(pageLoaded, function(key, value){ 
				//console.log(key + ":" + value);
			//});
			//console.log("---------------------");
			
			showMe(posContent(targetDivID));
		};
		
	};
	
	/* ADDRESS CHANGE */
	$.address.change(function(event) {
		//console.log("**CHANGE ADDRESS**");
		
		// do something depending on the event.value property, e.g.
		if (event.path == "/") {
			
			if (contentOpen) {
				closeMe();
				contentOpen = false;
			}
		} else {
			if (contentOpen) {
				closeMe();
				contentOpen = false;
				showContent (event.value);
			} else {
				showContent (event.value);
			};
			
		};
		
	});
	
});
