// Color
var objBG = new Object(); 
objBG.index = '#262626';
objBG.sitemap = '#262626';
objBG.socialmediamarketing = '#0054A6';
objBG.searchenginemarketing = '#80C100';
objBG.paidmediamarketing = '#E84500';
objBG.designtechnology = '#FF9F00';
objBG.aboutzogmedia = '#535353';
objBG.subpage = '#eee';

// Speed
var colorSpeed = 500;
var contentSpeed = 500;
var backgroundImageSpeed = 500;
var tempTitle = '';

function bgColor(backgroundColor) {
	$('body').css({backgroundColor : backgroundColor});
		
	if($('.contentBlockBG').length > 0 && $('.fiestabowl').length === 0) {
		$('.contentBlockBG').fadeIn(backgroundImageSpeed);
	}
	
	if($('.contentBlockBGShell').length === 1) {
		window.setTimeout(function() {  
			fadeBG();  
		}, backgroundImageSpeed);
	}
}

function fadeBG(opacityVal) {
	$('.contentBlockBGShell').animate({
		opacity : '1'
	}, backgroundImageSpeed, function() {
		// complete
	});
}

function changeView(contentHeight, contentShellHeight, backgroundColor, location, linkName, parentLink) {
	var pageTitle;
	var fullWidth = $(window).width();
	var fullHeight = $(window).height();
	scroll(0,0);
	$('body').css({'overflow' : 'hidden'});
	
	if($('.contentBlockBG').length > 0) {
		$('.contentBlockBG').fadeOut();
	}
	
	if($('.contentBlockBGShell').length === 1) {
		$('.contentBlockBGShell').fadeOut();
	}

	$('.contentBlock').animate({
		top : '-'+contentHeight
	}, contentSpeed, function() {
		
		$("#content").load('/inc/'+location+'.php .contentBlock', function(response, status, xhr) {
			_gaq.push(['_trackPageview', '/'+location+'.php']);
			bgColor(backgroundColor);
			$('.contentBlock').css({'top' : contentShellHeight});
			$('.contentBlock').show();
			$('.contentBlock').animate({
				top : '0px'
			}, contentSpeed, function() {
				$('.active').removeClass('active');
				$('a[name="'+linkName+'"]').addClass('active');
				var parentLink = $('.contentBlock').attr('rel');
				if(parentLink) {
					$('.header a[name="'+parentLink+'"]').addClass('active');
				}
				$('body').css({'overflow' : 'auto'});
				
				if($('.fiestabowl').length > 0) {
					$('.fiestabowl').fadeIn(backgroundImageSpeed);
				}
				
				if(fullWidth >= 1400 && fullHeight >= 1000) {
					$('#zog-work .contentBlockBG').css({backgroundPosition : '50% 160px'});
				}
				
				// Tooltip
				$('.peopleTip').tooltip();
				
				// Masonry
				if(location === 'index') {
					var container = $('.container_box');
					container.imagesLoaded( function(){
						container.masonry({
					    	itemSelector : '.box',
					    	columnWidth: 160,
					    	isAnimated: true
					  	});
					});
				}
			});
			
			document.title = $('.contentBlock').attr('title');
		});		
	});
	
}

$(document).ready(function() {	
	var contentShellHeight = $(document).height() - 295;
	var contentHeight = $('.contentBlock').height() + 35;
	var currentPage = window.location.href;
	var currentLocation = currentPage.split('#!/');
	var fullWidth = $(window).width();
	var fullHeight = $(window).height();
	
	

	if(currentLocation[1]) {
		$('a[name="'+currentLocation[1]+'"]').addClass('active');
		var sanitizedLocation = currentLocation[1].replace(/-/g,'');
		var initBGColor = objBG[sanitizedLocation];
				
		if(!initBGColor) {
			initBGColor = objBG.subpage;
		}
		
		$('body').css({backgroundColor : initBGColor});
		
		$("#content").load('/inc/' + currentLocation[1] + '.php .contentBlock', function(response, status, xhr) {
			contentHeight = $('.contentBlock').height() + 35;
			$('a[name="'+currentLocation[1]+'"]').addClass('active');
			
			var parentLink = $('.contentBlock').attr('rel');
			if(parentLink) {
				$('.header a[name="'+parentLink+'"]').addClass('active');
			}
			
			document.title = $('.contentBlock').attr('title');
			if($('.contentBlockBG').length === 1) {
				$('.contentBlockBG').fadeIn(backgroundImageSpeed);
			}
			
			if($('.contentBlockBGShell').length === 1) {
				window.setTimeout(function() {  
				    fadeBG();  
				}, backgroundImageSpeed);
			}
			
			if($('.fiestabowl').length === 1) {
				$('.fiestabowl').fadeIn(backgroundImageSpeed);
			}
			
			if(fullWidth >= 1400 && fullHeight >= 1000) {
				$('#zog-work .contentBlockBG').css({backgroundPosition : '50% 160px'});
			}
			
			// Tooltip
			$('.peopleTip').tooltip();
			
			// Masonry
			if(currentLocation[1] === 'index') {
				var container = $('.container_box');
				container.imagesLoaded( function(){
					container.masonry({
				    	itemSelector : '.box',
					    columnWidth: 160,
				    	isAnimated: true
				  	});
				});
			}
			
		});
	} else {
		var pathName = window.location.pathname
		var filePath = pathName.replace('/', '');
		
		var sanitizedLocation = filePath.replace(/-/g,'');
		sanitizedLocation = sanitizedLocation.replace(/.php/g,'');
		var initBGColor = objBG[sanitizedLocation];
		
		if(!initBGColor && sanitizedLocation !== '') {
			initBGColor = objBG.subpage;
		}
		
		$('body').css({backgroundColor : initBGColor});

		$("#content").load('/inc/'+filePath+' .contentBlock', function(response, status, xhr) {
			contentHeight = $('.contentBlock').height() + 35;
			$('a[name="index"]').addClass('active');
			if($('.contentBlockBG').length === 1) {
				$('.contentBlockBG').fadeIn(backgroundImageSpeed);
			}
			
			if($('.contentBlockBGShell').length === 1) {
				window.setTimeout(function() {  
				    fadeBG();  
				}, backgroundImageSpeed);
			}
			
			if($('.fiestabowl').length === 1) {
				$('.fiestabowl').fadeIn(backgroundImageSpeed);
			}
			
			if(fullWidth >= 1400 && fullHeight >= 1000) {
				$('#zog-work .contentBlockBG').css({backgroundPosition : '50% 160px'});
			}
			// Tooltip
			$('.peopleTip').tooltip();
			
			// Masonry
			if(!filePath || filePath === 'index.php') {
				var container = $('.container_box');
				container.imagesLoaded( function(){
					container.masonry({
				    	itemSelector : '.box',
				    	columnWidth: 160,
				    	isAnimated: true
				  	});
				});
			}
		});
	}
	
	// Remove Title Attribute
	$('.contentBlock').live('mouseover', function() {
		tempTitle = $(this).attr('title');
		$(this).attr('title', '');
	});
	
	$('.contentBlock').live('mouseout', function() {
		$(this).attr('title', tempTitle);
	});
	
	
	jQuery(window).hashchange(function(e) {
		var currentPage = window.location.href;
		var currentLocationSplit = currentPage.split('#!/');
		var currentLocation = (currentLocationSplit[1]) ? currentLocationSplit[1] : 'index';
		var sanitizedName = currentLocation.replace(/-/g,'');
		var linkName = $('#pageBody a[name="'+currentLocation+'"]').attr('name');
		contentHeight = $('.contentBlock').height() + 35;
		
		if(objBG[sanitizedName]) {
			var backgroundColor = objBG[sanitizedName];
		} else {
			var backgroundColor = objBG.subpage;
		}
		
		changeView(contentHeight, contentShellHeight, backgroundColor, currentLocation, linkName);
	});
	
	$('#pageBody a').live('click', function(e) {
		var currentPage = window.location.href;
		var currentLocationSplit = currentPage.split('#!/');
		var currentLocation = (currentLocationSplit[1]) ? currentLocationSplit[1] : 'index';
	
		if(!$(this).hasClass('external')) {
			e.preventDefault();
			var linkName = $(this).attr('name');
			
			if(currentLocation !== $(this).attr('name')) {
				$('.active').removeClass('active');
				$(this).addClass('active');
				window.location = '/#!/'+$(this).attr('name');
			}
		}
	});
	
	// WORK
	$('.clientThumb').live('click', function() {
		if(!$(this).hasClass('activeThumb')) {
			$('.fullContLft .activeThumb').removeClass('activeThumb');
			$(this).addClass('activeThumb');
			var clientName = $(this).attr('name');
			var prevClientRaw = $('.contentBlockBG:visible').attr('class');
			var prevClient = prevClientRaw.split('contentBlockBG ');
			
			$('#'+prevClient[1]).hide();
			$('#'+clientName).fadeIn(400);
			$('.'+prevClient[1]).hide();
			$('.'+clientName).fadeIn(400);
		}			
	});
	
	// Work Window Resize Listener
	window.onresize = function(e) {
	    fullWidth = $(window).width();
	    fullHeight = $(window).height();
	    
	    if(fullWidth >= 1400 && fullHeight >= 1000) {
	    	$('#zog-work .contentBlockBG').css({backgroundPosition : '50% 160px'});
	    } else if(fullWidth <= 1400 && fullHeight <= 1000) {
	    	$('#zog-work .contentBlockBG').css({backgroundPosition : '210px 150px'});
	    }
	    //alert(fullWidth);
	}
	
	
});
