var global = {
	PAGE_HOMEPAGE : "homepage",
	PAGE_COLLECTION : "collection",
	PAGE_STOCKISTS : "stockists",
	PAGE_ABOUT: "about",
	PAGE_CONTACT: "contact",
	PAGE_PRESS : "press",
	PAGE_T_AND_C : "termsandconditions",
	
	MAIN_NAV_TRANSITION_TIME : 500,
	
	MIN_WINDOW_H_TO_SHOW_NAV : 768,
	CANVAS_WIDTH : 1280,
	CANVAS_HEIGHT : 1024,
	
	BG_MAX_HEIGHT : 1200,
	BG_MAX_WIDTH : 1600,
	
	EVENT_NAV_OPEN_CLOSE_CHANGE : "EVENT_NAV_OPEN_CLOSE_CHANGE",
	
	currentPage : "",
	isNavOpen : true,
	
	init : function(currentPage) {
		global.currentPage = currentPage;
		
		
		//if there are no sub links then make the links positioned absolute
		if(!$("#nav #links #subLinks").length > 0){
			$("#nav #links").css("position", "absolute");
		}
		
		
		global.handleShowOrHideMainNav(0);
		
		global.disableSpecialProjectsLink();
		global.disablePageNav();
		global.disableNavSubLinks();
		
		global.resizeBg();
		
		//init the global events
		global.events();
	},
	
	disableSpecialProjectsLink : function() {
		var $specialProjNavBtn = $('#nav #links #mainLinks a:eq(1)');
		
		$specialProjNavBtn.click(function(e) {
			e.preventDefault();  
		});
	},
	
	disablePageNav : function() {
		//turn off functionality of a tags in the main navigation
		$('.pageNav a').click(function(e) {
			e.preventDefault();  
		});
	},
	
	disableNavSubLinks : function() {
		//turn off functionality of a tags in the main navigation
		$('#nav #subLinks a').click(function(e) {
			e.preventDefault();  
		});
	},
	
	handleShowOrHideMainNav : function(transitionTime) {
		var winH = $(window).height();
		if(winH <= global.MIN_WINDOW_H_TO_SHOW_NAV) {
			global.hideMainNav(transitionTime);
		}
		else {
			global.showMainNav(transitionTime);	
		}
	},
	
	hideMainNav : function(transitionTime) {
		//fade out links
		var $links = $("#nav #links");
		
		
		if(jQuery.support.opacity) {
			$links.stop().animate({
				"opacity" : "0",
				"-moz-opacity" : "0",
				"filter" : "alpha(opacity=0)"
			},transitionTime, function(){
				$links.hide();
				global.isNavOpen = false;
				$("#nav").trigger(global.EVENT_NAV_OPEN_CLOSE_CHANGE);
			});
		}
		else {
			$links.hide();
			global.isNavOpen = false;
			$("#nav").trigger(global.EVENT_NAV_OPEN_CLOSE_CHANGE);
		}
		
		/*
		$links.stop().animate({
			"opacity" : "0",
			"-moz-opacity" : "0",
			"filter" : "alpha(opacity=0)"
		},transitionTime, function(){
			$links.hide();
		});
		*/
		
		//$links.fadeOut(transitionTime);
		
		//$links.customFadeOut(transitionTime);
		
		/*
		$links.css({
			display : "none"
		});
		*/
		
		//$links.hide();
			
		//move logo to bottom
		/*
		$("#nav img").css({
			"position" : "relative"
		});
		*/
	},
	
	showMainNav : function(transitionTime) {
		//fade in links
		var $links = $("#nav #links");
		
		
		$links.show();
		global.isNavOpen = true;
		$("#nav").trigger(global.EVENT_NAV_OPEN_CLOSE_CHANGE);
		
		if(jQuery.support.opacity) {
			$links.stop().animate({
				"opacity" : "1",
				"-moz-opacity" : "1"
				//"filter" : "alpha(opacity=100)"
			},transitionTime);
		}
		
		
		
		//$links.fadeIn(transitionTime);
		
		//$links.customFadeIn(transitionTime);
		
		/*
		$links.css({
			display : "block"
		});
		*/
		
		//$links.show();
		
		//move logo back up
		/*
		$("#nav img").css({
			"position" : "absolute"
		});
		*/
	},
	
	hideShareOptionBtns : function() {
		var $shareBtn = $(".share .shareBtn");
		var $shareSubNav = $(".share .shareSubNav");
		
		$shareBtn.show();
		$shareSubNav.hide();
	},
	
	showShareOptionBtns : function() {
		var $shareBtn = $(".share .shareBtn");
		var $shareSubNav = $(".share .shareSubNav");
		
		$shareBtn.hide();
		$shareSubNav.show();
	},
	
	resizeBg : function() {
		var $img = $("#bg img");
		var winW = $(window).width();
		var winH = $(window).height();
		//var currentImgH = $img.height();
		//var currentImgW = $img.width();
		var currentImgH = global.BG_MAX_HEIGHT;
		var currentImgW = global.BG_MAX_WIDTH;
		
		
		//if you are on the homepage, resize verticaly
		//else make sure the resize fills up the entire screen
		var newImgH = currentImgH;
		var newImgW = currentImgW;
		//if(global.currentPage == global.PAGE_HOMEPAGE || global.currentPage == global.PAGE_COLLECTION) {
			newImgH = Math.round(winH);
			/*
			if(newImgH > global.BG_MAX_HEIGHT) {
				newImgH = global.BG_MAX_HEIGHT;	
			}
			*/
			newImgW = (currentImgW*newImgH) / currentImgH;
			/*
			if(currentImgH <=1) {
				newImgW = (global.BG_MAX_WIDTH*newImgH) / global.BG_MAX_HEIGHT;
			}
			*/
			newImgW = Math.round(newImgW);
		//}
		/*
		else {
			var resizeObj = global.getResizeMinMaxObj(
				currentImgW, currentImgH,
				currentImgW, currentImgH,
				winW, winH
			);
			newImgW = resizeObj.width;
			newImgH = resizeObj.height;
		}
		*/

		$img.height(newImgH);
		$img.width(newImgW);
		
		global.positionBg();
	},
	
	//takes in an images height and width values as well as min and max values 
	//and returns an object with width and height values that fall within the min and max values
	getResizeMinMaxObj : function(width, height, maxW, maxH, minW, minH) {
		//console.log('imgW and imgH: ' + width + 'x' + height);
		var newImgW = width;
		var newImgH = height;
		
		var r = maxH/maxW;
		
		//check to see if width is greater than max
		if(newImgW > maxW) {
			newImgW = maxW;
			newImgH = Math.round(newImgW*r);
		}
		//console.log('newImgW and newImgH: ' + newImgW + 'x' + newImgH);
		
		//check to see if height is greater than max
		if(newImgH > maxH) {
			newImgH = maxH;
			newImgW = Math.round(newImgH/r);
		}
		//console.log('newImgW and newImgH: ' + newImgW + 'x' + newImgH);
		
		//check to see if width is smaller than min
		if(newImgW < minW) {
			newImgW = minW;
			newImgH = Math.round(newImgW*r);
		}
		//console.log('newImgW and newImgH: ' + newImgW + 'x' + newImgH);
		
		//check to see if height is smaller than min
		if(newImgH < minH) {
			newImgH = minH;
			newImgW = Math.round(newImgH/r);
		}
		//console.log('newImgW and newImgH: ' + newImgW + 'x' + newImgH);
		
		
		//return a resize object
		var resizeObj = {
			height : newImgH,
			width : newImgW	
		}
		return resizeObj;
	},
	
	getCurrentSubNavOffset : function() {
		var $nav = $("#nav");
		var $logo = $nav.find("#logo img");
		var logoH = $logo.height();
		var navH = $nav.height();
		//TODO should not be hardcoded and should be grabbed from the css
		var navBottomOffset = 42;
		
		var offset = navH - logoH - navBottomOffset;
		
		return offset;
	},
	
	positionBg : function() {
		var $img = $("#bg img");
		var winW = $(window).width();
		var winH = $(window).height();
		var imgW = $img.width();
		var imgH = $img.height();
		
		var marginLeft = (winW/2) - (imgW/2);
		marginLeft = Math.round(marginLeft);
		var marginTop = 0;
		if(imgH > global.BG_MAX_HEIGHT) {
			marginTop = (winH/2) - (imgH/2);	
		}
		$img.css("marginLeft", marginLeft);
		$img.css("marginTop", marginTop);
	},
	
	handleFacebookShare : function() {
		var sharelink = "p[url]="+encodeURIComponent(window.location.href);
		var sharetitle = "shaaareTiiiitle";
		var thumb = "p[images][0]="+"img/global/facebook_share_icon.jpg";
		var fblink = "http://www.facebook.com/share.php?s=100&"+sharelink+"&"+sharetitle+"&"+thumb;
		window.open(fblink);
		
		/*
		var self = $(this);
		var sharelink = "";
		if (self.attr("href")=="") {
			sharelink = "p[url]="+encodeURIComponent(window.location.href);
		}
		else {
			sharelink = "p[url]="+encodeURIComponent(self.attr("href"));
		}
		var sharetitle = "p[title]="+encodeURIComponent(self.attr("title"));
		//var thumb = "p[images][0]="+encodeURIComponent($("#logo img").attr("src"));
		var thumb = "p[images][0]="+"_/images/facebook_share_icon.jpg";
		var link = "http://www.facebook.com/share.php?s=100&"+sharelink+"&"+sharetitle+"&"+thumb;
		self.attr("href",link);
		//http://www.facebook.com/sharer.php?s=100&p[title]=WEB CHANNEL&p[summary]=Web+Design+Company+in+Dubai.&p[url]=http://www.webchannel.ae&p[images][0]=http://www.webchannel.ae/images/logo-home.png
		*/
	},
	
	handleTwitterShare : function() {
		/*
		var self = $(this);
		var sharelink = "";
		if (self.attr("href")=="") {
			sharelink = "'"+encodeURIComponent(window.location.href)+"'";
		}
		else {
			sharelink = "'"+encodeURIComponent(self.attr("href"))+"'";
		}
		var sharetitle = "'"+encodeURIComponent(self.attr("title"))+"'";
		var link = "http://twitter.com/home?status="+sharetitle+"-"+sharelink;
		self.attr("href",link);
		*/
		var sharetitle = "Share Title here";
		var sharelink = encodeURIComponent(window.location.href);
		var twitterlink = "http://twitter.com/home?status="+sharetitle+"-"+sharelink;
		//window.location.href = twitterlink;
		window.open(twitterlink);
	},
	
	handleEmailShare : function() {
		var emailCopy = "I thought you might enjoy this. ";
		var content = "mailto:?body=" + emailCopy;
		content += "\n"+window.location.href;
		//window.open(content);
		window.location.href = content;

		
		/*
		var email = $(".share-menu li").eq(2);
		var content = email.find("a").attr("href");
		content += "\n"+window.location.href;
		email.find("a").attr("href",content);
		*/
	},
	
	events : function() {
		var $nav = $("#nav");
		var $specialProjNavBtn = $('#nav #links #mainLinks a:eq(1)');
		var $shareBtn = $(".share .shareBtn");
		var $shareSubNav = $(".share .shareSubNav");
		//var $mail = $(".share .shareSubNav a:eq(0)");
		//var $facebook = $(".share .shareSubNav a:eq(1)");
		//var $twitter = $(".share .shareSubNav a:eq(2)");
		
		//old
		/*
		$share.click(function() {
			//by default the links within share sub nav are hidden, so show them regardless if its parent(#shareSubNav) are hidden or not)
			$("#shareSubNav a").each(function(index){
				$(this).show();
			});	
			
			//show or hide share options
			var isVisible = $("#shareSubNav").is(':visible');
			$("#shareSubNav").toggle(!isVisible);
		});
		*/
		
		$shareBtn.click(function() {
			global.showShareOptionBtns();
		});
		
		
		$shareSubNav.each(function(index){
			var $mail = $shareSubNav.eq(index).find("a:eq(0)");
			var $facebook = $shareSubNav.eq(index).find("a:eq(1)");
			var $twitter = $shareSubNav.eq(index).find("a:eq(2)");
			
			$mail.click(function(){
				global.handleEmailShare();
			});
			
			$facebook.click(function() {
				global.handleFacebookShare();
			});
			
			$twitter.click(function() {
				global.handleTwitterShare();
			});
		});
		
		/*
		$mail.click(function() {
			global.handleEmailShare();
		});
		
		$facebook.click(function() {
			global.handleFacebookShare();
		});
		
		$twitter.click(function() {
			global.handleTwitterShare();
		});
		*/
		
		$specialProjNavBtn.mouseover(function() {
			$(this).html("Coming Soon");
		});
		
		$specialProjNavBtn.mouseout(function() {
			$(this).html("Special Projects");
		});
		
		$nav.mouseenter(function() {
			var winH = $(window).height();
			if(winH <= global.MIN_WINDOW_H_TO_SHOW_NAV) {
				global.showMainNav(global.MAIN_NAV_TRANSITION_TIME);
			}
		});
		
		$nav.mouseleave(function() {
			global.handleShowOrHideMainNav(global.MAIN_NAV_TRANSITION_TIME);
		});
		
		$(window).resize(function() {
			global.handleShowOrHideMainNav(global.MAIN_NAV_TRANSITION_TIME);
			
			global.resizeBg();
		});
		
	}
};

(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

