var Site = {
	
	Init : function() {
		
		// Animate the home page logos
		Site.AnimateHomeLogos();
		
		// Load tweets
		Site.LoadTweets();
		
		// Load case studies
		Site.LoadCaseStudies();
		
		// When hash changes
		$.address.change(function(event) {  
			var hashValue = $.address.value();
			var hash = (hashValue!=='/') ? hashValue.substring(1) : "work";
			Site.PageChange(hash);
		});
		
		$('.office-email a').attr('href','mailt'+'o:stu'+'dio@w'+'iseguy'+'digit'+'al.com').html('stu'+'dio@w'+'iseguy'+'digit'+'al.com');

		// Address
		$('header#header header h1 a').click(function(e) {
			e.preventDefault();
			$.address.value('work');
		});
		
		$("header#header ul").delegate("li a", "click tapstart", function(e){
			e.preventDefault();
			$.address.value($(this).attr('href'));
			clearInterval(Anim.timer);
		});
		
		// Resize event
		$(window).resize(function() {
			Site.Resize();
		});
		
	},
	
	PageChange : function(hash) {
		if (hash !== 'home' && hash !== 'work' && hash !== 'about' && hash !== 'social' && hash !== 'contact') {
			Site.ShowCaseStudy(hash);
			hash = 'work';
		}
		var p = $('section#'+hash).offset();
		var sectionY = p.top;
		
		$('section#main-content').animate({
			top: "-="+sectionY
		}, {
			'complete' : function(){
				timeDelay = window.setTimeout(function(){ Site.InitPage(hash) }, 10);
			}
		});
		Site.UpdateNav(hash);
	},
	
	UpdateNav : function(hash) {
		$('header#header nav .selected').removeClass('selected');
		$('header#header nav a[href$="'+hash+'"]').addClass('selected');
	},
	
	InitPage : function(hash) {

		// Active class
		$('section#main-content section.content-section').removeClass('active-section');
		$('section#'+hash).addClass('active-section');
		// Slide in headers
		$('section#main-content header h1').css({'opacity': '1'});
		// Modal window on work page
		if (hash=='work') {
			Modal.Init();
		}
		
		// Initiate scrolling
		Site.InitScrolling(hash);
		// Move page header on section scroll
		$('section#'+hash+' div.content-area').scroll(function() {
			var header = $('section#'+hash+' div.content-area header');
			var h1 = $('section#'+hash+' div.content-area header h1');
			var pos = header.position().top;
			if (!Site.IsMobile()) {
				if (pos < 50) {
						h1.fadeOut();
				} else {
						h1.fadeIn();
				}
			}
		});
		// Masonry
		var $container = $('div.content-area section');
		$container.masonry({
		  itemSelector : 'article',
	    columnWidth : 245
	  });
		// Resize
		Site.Resize();	
	},
	
	AnimateHomeLogos : function() {
		$('#home h1').css({'left':'-=40', 'opacity':0});
		$('#home h2').css({'left':'+=60', 'opacity':0});
		$('#home a').css({'opacity':'0'});
		setTimeout(function() {
			$('#home h1').animate({
				'left' : '+=40',
				'opacity' : '1'
			}, 500);
		}, 500);
		setTimeout(function() {
			$('#home h2').animate({
				'left' : '-=60',
				'opacity' : '1'
			}, 500);
		}, 800);
		setTimeout(function() {
			$('#home a').animate({
				'opacity' : '1'
			}, 300);
		}, 3000);
	},
	
	LoadCaseStudies : function() {
			var caseStudiesSection = $('#work div.content-area section');
			var newHtml = '';
			var caseStudies;
			caseStudies = $.map(cases, function(caseStudy, index){
				var id = index;
				var titleTxt = caseStudy['titleTxt'];
				return '<article class="image-article"><a href="#/work/'+id+'"><img src="img/work-thumbs/'+id+'.jpg" alt="'+titleTxt+'" /></a></article>';
			});
			newHtml += caseStudies.join("");
			newHtml += '<br class="clear" />';
			caseStudiesSection.html(newHtml);
			Site.ResizeForPhones();
	},
	
	LoadTweets : function() {
		$.ajaxSetup({ cache: true });
		var twitterUrl = 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=f&include_rts=t&screen_name=wiseguydigital&count=20&trim_user=t';
		$.getJSON(twitterUrl+'&callback=?', function(tweets){
			var tweetBox = $('#social div.content-area #tweets');
			var newHtml = '';
			var tweets;			
			tweets = $.map(tweets, function(tweet, index){
				var tweetContent = tweet['text']
				var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
				tweetContent = tweetContent.replace(exp,"<a href='$1' title='$1'>Visit URL</a>");
				tweetContent = tweetContent.replace(/(^|\s)@(\w+)/g, "$1<a href='http://www.twitter.com/$2' target='_blank'>@$2</a>");
				tweetContent = tweetContent.replace(/(^|\s)#(\w+)/g, "$1<a href='http://search.twitter.com/search?q=%23$2' target='_blank'>#$2</a>");
				return ('<article>'+tweetContent+'<br /><span class="date">'+tweet['created_at']+'</span></article>');
			});
			newHtml += tweets.join("");
			newHtml += '<br class="clear" />';
			tweetBox.html(newHtml);
		});
	},
	
	Resize : function() {
		var p = $('section.active-section').offset();
		if (undefined !== p && null !== p) {
			var sectionY = p.top;
			$('section#main-content').animate({
				top: "-="+sectionY
			},0);
		}
		Site.ResizeForPhones();
	},
	
	ResizeForPhones : function() {
		
		// Content needs to overflow on mobile devices
		if (Site.IsMobile()) {
			
			// Work pages
			var articlesPerRow = Math.floor($(window).width() / 235);
			var count = 0;
			for(var prop in cases) {
	        count++;
	    }
			var height = ((count/articlesPerRow)*200) + 100;
			$('#work-content').css({
				'overflow':'auto',
				'height': height
			});
			$('#work #info-box').css({'top':$(window).height()/2});
			
			// About
			$('#about-content').css({
				'overflow':'auto',
				'height': height + 150
			});
			
			// Contact
			$('#contact-content').css({
				'overflow':'auto',
				'height': height + 150
			});
			
			// Social pages
			var tweetsHeight = $('#tweets').height();
			var tweetsPerRow = Math.floor($(window).width() / 195);
			if (tweetsHeight < 400) tweetsHeight = ((20/tweetsPerRow)*200) + 100;
			$('#social-content').css({
				'overflow':'auto',
				'height': tweetsHeight + 150
			});
			
		}
		
		if ($(window).width() < 555) {
			$('div.content-area h1').css({'width':'230px'});
		} else {
			$('div.content-area h1').css({'width':'475px'});	
		}
	},
	
	InitScrolling : function(hash) {
		if (Site.IsMobile()) {
			var myScroll = new iScroll(hash, {
				'bounce' : false
			});
		}
	},
	
	IsMobile : function() {
		if( navigator.userAgent.match(/Android/i) ||
			  navigator.userAgent.match(/webOS/i) ||
			  navigator.userAgent.match(/iPhone/i) ||
			  navigator.userAgent.match(/iPod/i) ||
			  navigator.userAgent.match(/iPad/i)){
		 		return true;
		}
		return false;
	},
	
	IsCanvasSupported : function(){
	  var elem = document.createElement('canvas');
	  return !!(elem.getContext && elem.getContext('2d'));
	},
	
	ShowCaseStudy : function(hash) {
		var caseName = hash.substring(5);
		if (undefined !== cases[caseName]) {
			var titleTxt = cases[caseName].titleTxt;
			var txt = cases[caseName].txt;
			var tags = cases[caseName].tags;
			var videoId = cases[caseName].videoId;
			var url = cases[caseName].url;
			var html = '<a href="#/work" id="close-button"><img src="/img/close-btn.png" alt="Close" /></a>';
			$('#info-bg').css({'height': 2000});
			$('#info-box').offset({
				'top' : ($(window).height() / 2) - $('#info-box').height()/2
			});
			
			if (undefined !== videoId) {
				html += '<iframe src="http://player.vimeo.com/video/'+videoId+'?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1" width="600" height="338" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>';
			} else {
				html += '<section><h2>'+titleTxt+'</h2>'+txt+'<p class="tags">'+tags+'</p>';
				if (undefined !== url) html += '<p><a href="'+url+'" target="_blank">Visit site</a></p>';
				html += '</section>';
				html += '<img class="large-image" src="/img/work-large/'+caseName+'.jpg" alt="'+titleTxt+'" />';
			}
			Modal.Init(html);
		}
	}
	
}

var Modal = {
	introViewed : false,
	Init : function(html) {
		
		if( navigator.userAgent.match(/Android/i) ||
			  navigator.userAgent.match(/webOS/i) ||
			  navigator.userAgent.match(/iPhone/i) ||
			  navigator.userAgent.match(/iPod/i)) {
		 		return;
		}
		
		if (Modal.introViewed) {
			$('#info-box').offset({
				'top' : ($(window).height() / 2) - $('#info-box').height()/2 - 30
			});
		}
		
		// Populate with HTML
		if (undefined !== html) {
			$('#info-box').html(html);
			$('#work-content').css({'overflow':'hidden'});
			$('#info-box').show();
			$('#info-bg').show();
		}
		
		if (undefined == html && !Modal.introViewed) {	
			$('#work-content').css({'overflow':'hidden'});
			$('#info-box').show();
			$('#info-bg').show();
		}
		
		$("#info-box").delegate("#close-button", "click tapstart", function(e){
			e.preventDefault();
			Modal.introViewed = true;
			Modal.Close();
		});
		
		$('#info-box #continue-button').click(function(e){
			e.preventDefault();
			Modal.introViewed = true;
			Modal.Close();
		});

	},
	
	Close : function() {
		$('#work-content').css({'overflow':'auto'});
		$('#info-box').html('');
		$('#info-box').hide();
		$('#info-bg').hide();
		$.address.value('work');
	}
}
