/*

BASE Functions

==================================================================================================================*/

var target;

Cufon.replace('.tagline, .widget-title, .portfolio-card h2', { fontFamily: 'Vista Sans' });

// Detect if device is iOS (iPhone, iPod, iPad)
function isApple() {
	var deviceAgent = navigator.userAgent.toLowerCase();
	var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
	if (agentID) {
		return true;	
	}
	else{
		return false;	
	}
}

// Detect if device is Android
function isDroid() {
	var ua = navigator.userAgent.toLowerCase();
	var isAndroid = ua.indexOf("android") > -1;
	if(isAndroid) {
		return true;	
	}
	else{
		return false;	
	}
}

// Animate Content
function animateContentOpen() {
	$('.main-area').css('height', '0').delay(200).animate({ 'height' : '305px' }, 200);
	
	$('.close-button a').live('click', function() {	
		$('.main-area').stop().animate({ 'height' : '0' }, 200);
		return false;
	});
}

// Slide Navigation
function slideNav() {
	var apple = isApple();
	var android = isDroid();
	
	// remove CSS defaults
	$('ul.main-nav > li > a').css({ 'margin-top' : '0', 'padding-top' : '0', 'background-image' : 'url(/wp-content/themes/eandv/images/bg_nav-item.gif)' });
	// add fake sub-menu's for all nav items just for slide up hover effect
	$('ul.main-nav > li:not(:has(ul.sub-menu))').append('<ul class="sub-menu"></ul>');
	
	$('#navigation .main-nav > li').each(function (i) {
		if ( $(this).hasClass('current-menu-item') || $(this).hasClass('current-page-ancestor') ) {
			// leave open	
			$(this).find('.sub-menu').css('height', '0').hide();		
		} else {
			$(this).find('.sub-menu').css('height', '0').hide();
			
			// Mobile/Tablet Navigation Correction
			if (apple || android){
				
				$(this).click(function(e) {
					var subMenu = $(this).find('ul.sub-menu');
					var hasSubMenu = subMenu.children().length > 0;
					var isActive = subMenu.height() > 0;
										
					//if it has a sub-menu and is not active then open the popup
					//else, navigate to the link
					if(hasSubMenu && !isActive) {
						//show the menu
						$(this).siblings('li').find('.sub-menu').stop().animate({ 'height' : '0' }, 500);
						$(this).find('.sub-menu').stop().show().animate({ 'height' : '90px' }, 500);
						
						//stop the link from being navigated to
						return false;
					}
				});
				
				
								
			}
			
			else { // add hover to these items
				
				$(this).hover(function() {
					$(this).siblings('li').find('.sub-menu').stop().animate({ 'height' : '0' }, 500);
					$(this).find('.sub-menu').stop().show().animate({ 'height' : '90px' }, 500);
				}, function() {
					$(this).find('.sub-menu').stop().animate({ 'height' : '0' }, 500, function() { $(this).hide(); });
					$(this).siblings('li.current-menu-item, li.current-page-ancestor').find('.sub-menu').stop().animate({ 'height' : '90px' }, 500);
				});
				
			}
		}		
		
		$parent = $(this).parent();
		$(this).hover(function() {
			if ( $parent.hasClass('sub-menu') ) {
				$parent.find('li').fadeIn('fast');
			} else {
				$(this).find('.sub-menu li').fadeIn('fast');
			}
		}, function() {
			if ( $parent.hasClass('sub-menu') ) {
				$parent.find('li').fadeOut('slow');
			} else {
				$(this).find('.sub-menu li').fadeOut('slow');
			}
		});
		
	});	
}

//jQuery Load
$(document).ready(function() {
	
	slideNav();
	
	/* fake ajax like action between pages */
	/*$('#main-area').css('height', '0').delay(200).animate({ 'height' : '305px' }, 1000);
	
	$('.close-button a, #navigation a, .logo a').click(function() {
		target = $(this).attr('href');	
		$('.main-area').stop().animate({ 'height' : '0' }, 1000, function () {
			window.location = target;
		});
	});*/
	
	$('#content').scrollbar();
	
	$('.primary-sample a, .portfolio-card a, .other-sample a').fancybox({
		'overlayOpacity' : 0.6,
		'overlayColor' : '#000'
	});
	if ( $('#the_work > li').length > 3 ) {
		$('#the_work').jcarousel();	
	}
	
	if ($('#blog-page').length == 0) {
		$('ul.main-nav > li:not(#menu-item-19) a').live('click', function() {
			$('#main-area').stop().animate({ 'height' : '0' }, 200);
			$('#wrap').load($(this).attr('href') + ' #container', function() {
				Cufon.replace('.tagline, .widget-title, .portfolio-card h2', { fontFamily: 'Vista Sans' });
				slideNav();
				$('#content').scrollbar();
				$('.primary-sample a, .portfolio-card a, .other-sample a').fancybox({
					'overlayOpacity' : 0.6,
					'overlayColor' : '#000'
				});
				if ( $('#the_work > li').length > 3 ) {
					$('#the_work').jcarousel();	
				}
				animateContentOpen();
				$('#menu-item-19 a').unbind('click').click(function() {
					window.location = $(this).attr('href');
				});
			});
			
			return false;
		});
	}
	
	
});

// Print Page
function printThis() {
	(window.print) ? window.print() : alert('To print his page press Ctrl-P on your keyboard \nor choose print from your browser or device after clicking OK');
}
