// JavaScript Document

$(document).ready(function () {
	
	set_menu();
	set_footer();
	
	$('.frame').not('.frame#foot').hide().delay(500).fadeIn('slow');
	
	// Links
	if( ios() == "iPad" || ios() == "iPhone" ) {
		
		//Stop Animations
		$.fx.off
				
		//Fonts
		$('body').css('overflow', 'auto').css('font-size', 10);
		$('span#titles').css('font-size', 12);
		
		//Structure
		$('.frame#foot').hide();
		$('.frame#content').css('padding-bottom' , 15);
		$('.column, .imageHero').css('width', 210);
		$('.img_reload').css('width', 68);
		
		//Menu Structure
		$('#menu_thinkingways').hide();
		
	} else {
		
		$('.frame#foot').fadeIn();
		
		$('a').not('.sub a').hover(
		function() {
			$(this).animate({color : '#C1D82F'}, 'fast'); 
		},
		function() {
			$(this).animate({color : '#ffffff'}, 'fast'); 
		});
		
		$('.sub a').hover(
		function() {
			$(this).animate({opacity : 0.5}, 'fast'); 
		},
		function() {
			$(this).animate({opacity : 1.0}, 'fast'); 
		});
		
		$('.frame#content').jScrollPane({
			showArrows: false,
			autoReinitialise: true,
			animateScroll: true
		}).css('bottom' , 120);
		
	}
	
	//Images
	$('.footer_detail').hide();
	
	/*
	$('.img_reload').hide().load(function() {
		$(this).fadeIn('slow');
	})
	;*/
	
	//$('.img_reload').hide().show('slow');
	
	
	$('a[rel=gallery]').fancybox({
		'width'				: 750,
		'height'			: 615,
		'autoDimensions'	: false,
		'autoScale'			: false,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe',
		'overlayColor'		: '#000',
		'changeSpeed'		: 0
	});
	
	$('a[rel=newsletter]').fancybox({
		'width'				: 780,
		'height'			: '90%',
		'autoDimensions'	: false,
		'autoScale'			: false,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe',
		'overlayColor'		: '#000',
		
		'showNavArrows'		: false
	});

});

function ios() {
	
	if (navigator.platform.indexOf("iPhone") != -1 || navigator.platform.indexOf("iPod") != -1) {
		return "iPhone"
	} else if (navigator.platform.indexOf("iPad") != -1) {
		return "iPad"
	}
}

function set_footer() {
	
	var t
	var i
	
	if( ios() != "iPad" ) {
		$('.footer_tile').hover(
			function () {
				i = $(this)
				t = setTimeout(function() {
					$(i).find('.footer_detail').show(500);
					$('.footer_tile').not(i).animate({'opacity':0.25}, 500);
				}, 500)
			},
			function () {
				clearTimeout(t);
				$(this).find('.footer_detail').fadeOut(250);
				$('.footer_tile').animate({'opacity':1.00}, 500);
			}
		);
	}
}

function set_menu() {

	function menu_over(){
		$(this).find(".sub").stop().fadeTo('slow', 1).show();
		(function($) {

			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;

				$(this).find("ul").each(function() {
					rowWidth += $(this).width();
				});
			};
		})(jQuery); 
	
		if ( $(this).find(".row").length > 0 ) {
	
			var biggestRow = 0;	
	
			$(this).find(".row").each(function() {
				$(this).calcSubWidth();

				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
	
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
	
		} else {
	
			$(this).calcSubWidth();
			$(this).find(".sub").css({'width' : rowWidth});
	
		}
	}

	function menu_out(){
	  $(this).find(".sub").stop().fadeTo('slow', 0, function() {
		  $(this).hide();
	  });
	}
	
	var config = {
		 sensitivity: 2,
		 interval: 100,
		 over: menu_over,
		 timeout: 500,
		 out: menu_out
	};
	
	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
}
