/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

		
function getRandomInt(min,max)
{ 
	return Math.round(min + Math.random()*(max-min));
} 
		
var ieVer;

$(function() {
	ieVer = $.browser.msie && parseInt($.browser.version);
	var cols = $('.col').not($('#nav'));
	
	if($('div#home').length>0)
	{
	/* randomize positioning of homepage box elements */
	cols.each(function()
	{
		//var s = getRandomInt(75, 195);
		var s = getRandomInt(75, 210);
		$(this).css({marginTop:s+'px'});
	});
	
	/* move footer to second column */
	var f = $('#footer').remove();

	var n = cols.get(1);
	f.appendTo(n);
	//.append(f);
	
	// remove top margin spacing on .break p's
	$('.col .break p').each(function() {
		if(!$(this).prev().is('h2'))
		{
			$(this).css({marginTop:'0px'});
		}
	});
	

		$('.item')
			.find('h1, h2, h3, h4, p, a')
			.each(function()
			{
				$(this).width($(this).width());
				$(this).attr('origColor',$(this).css('color'));
			});

			hoverOver();
	}
	
	if($('#case_studies').length>0)
	{
		/* move footer to second column */
		var f = $('#footer').remove();
		f.find('p:first-child').css({marginTop:'2px'});
		var n = cols.get(0);
		f.appendTo(n);
	}
	
	if($('#our_story, #we_will, #news').length>0)
	{
		/* move footer to second column */
		var f = $('#footer').remove();
		f.find('p:first-child').css({marginTop:'2px'});
		var n = cols.get(0);
		f.appendTo(n);
	}
	
	if($('#contact').length>0)
	{
		/* move footer to second column */
		var f = $('#footer').css({paddingLeft: '20px'}).remove();
		f.find('p:first-child').css({marginTop:'2px'});
		var n = cols.get(0);
		f.appendTo(n);
	}
	else
	{
		// remove everything but the 'copyright Nation1'
		var f = $('#footer');
		f.find('p').not(':first').remove();
		
		if($('#careers').length>0)
		{
			f.appendTo(cols.get(0));
		}
	}
	
	if($('#we_are_selling').length>0)
	{
		/* move footer to second column */
		var f = $('#footer').remove();
		f.find('p:first-child').css({marginTop:'2px'});
		var n = cols.get(0);
		f.appendTo(n).css({paddingLeft: '18px'});
	}
	
	if($('div#clients').length>0)
	{
		$('#footer').appendTo($('#nav'));
		
		if($.browser.msie)
		{
			var block = $('<li />').css({width:'148px', height:'93px', display:'inline', float:'left', marginTop:'8px', marginLeft:'8px'});
			var b = $('#clients').find('.universal');
			block.insertAfter(b);
		}
	}
	if($('#error_page').length>0)
	{
		var f = $('#footer');
		var n = cols.get(0);
		f.appendTo(n);
	}
	
	// remove subscribe submit button
	//$('#home #subscribe p.submit').remove();
	
	// homepage subscribe input field click
	var subscribe_text = 'type your email here...';
	$('#home p.email input').focus(function() {
		if($(this).val()=='type your email here...')
		{
			$(this).val('');
		}
	});
	$('#home p.email input').blur(function() {
		if($(this).val()=='')
		{
			$(this).val(subscribe_text);
		}
	});
	
	// add comments slidedown stuff
	$('#news.events #comments').hide();
	
	var text;
	$('#news.events div.details div.comments a').click(function(e) {
		e.preventDefault();
		var comm = $('#news.events #comments');
		var label = $('#news.events div.details div.comments span.text a');
		
		 
		
		if(!$(comm).is(':visible'))
		{
			text = label.text();
			label.text('hide comments');
			$(comm).slideDown(500, function() {
			$.scrollTo('#comments', 600, { easing:'easeOutCubic', queue:true, axis:'y' } );	
			});
		}
		else
		{
			label.text(text);
			$(comm).slideUp(500);
		}
	});
});

function hoverOver()
{
	$('.item, .item div').not($('.break')).each(
		function()
		{
			if($(this).parent().attr('id')=='nav' || $(this).parent().parent().attr('id')=='nav') return;
			
			var h1 = $(this).not($('.item')) ? true : false;
			var margin_lr_px = 15;
			var margin_tb_px = 10;
			var over = false;
			
			if(ieVer < 7 && ieVer!==false)
			{
				var config = {    
					 sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
					 interval: 10, // number = milliseconds for onMouseOver polling interval    
					 over:function()
					{
						if(over===false)
						{
							$(this).stop();
							over = true;
							$('h1, h2, h3, h4, p, a', this).each(fadeTextIn);
							$(this).animate({
								backgroundColor:'#eb008b'
							}, 200);
						}
					},
					timeout: 10, // number = milliseconds delay before onMouseOut    
					out:function()
					{
						if(over===true)
						{
							over = false;
							$('h1, h2, h3, h4, p, a', this).each(fadeTextOut);
							$(this).animate({
								backgroundColor:'#ffffff'
							}, 200);
						}
					}
				}

				$(this).hoverIntent(config);
			}
			else
			{
				var config = {    
				 sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
				 interval: 50, // number = milliseconds for onMouseOver polling interval    
				 over:function()
				{
					$('h1, h2, h3, h4, p, a', this).each(fadeTextIn);
					$(this).css({position:'relative',zIndex:100})
					.animate({
						backgroundColor:'#eb008b',
						marginLeft:'-='+margin_lr_px+'px',
						marginRight:'-='+margin_lr_px+'px',
						paddingLeft:'+='+margin_lr_px+'px',
						paddingRight:'+='+margin_lr_px+'px',
						marginTop:'-='+margin_tb_px+'px',
						marginBottom:'-='+margin_tb_px+'px',
						paddingTop:'+='+margin_tb_px+'px',
						paddingBottom:'+='+margin_tb_px+'px'
					}, 200);
				},
				timeout: 50, // number = milliseconds delay before onMouseOut    
				out:function()
				{
					$('h1, h2, h3, h4, p, a', this).each(fadeTextOut);
					$(this).css({position:'static'})
					.animate({
						backgroundColor:'#ffffff',
						marginLeft:'+='+margin_lr_px+'px',
						marginRight:'+='+margin_lr_px+'px',
						paddingLeft:'-='+margin_lr_px+'px',
						paddingRight:'-='+margin_lr_px+'px',
						marginTop:'+='+margin_tb_px+'px',
						marginBottom:'+='+margin_tb_px+'px',
						paddingTop:'-='+margin_tb_px+'px',
						paddingBottom:'-='+margin_tb_px+'px'
					}, 200);
				}
				}
				
				$(this).hoverIntent(config);
			}
		});
}

function fadeTextIn()
{
	$(this).animate({color:'#ffffff'}, 300);
}
function fadeTextOut()
{
	$(this).animate({color:$(this).attr('origColor')}, 300);
}

// allow object reversal through jquery function
$.fn.reverse = Array.prototype.reverse;

var bg_frame_width = 1650;
var new_c;

// set the background frame left positions - we'll calculate actual positioning later
var backgrounds = new Array();

$(function()
{
	// uk backgrounds
	backgrounds['news'] = 3;
	backgrounds['events'] = 3;
	backgrounds['contact'] = 0;
	backgrounds['case-studies'] = 1;
	backgrounds['our-story'] = 4;
	backgrounds['we-will-make'] = 4;
	backgrounds['404'] = 2;
	backgrounds[''] = 2;
	backgrounds['careers'] = 2;
	
	// india background image overrides   
	if($('body').hasClass('india'))
	{
		backgrounds['contact'] = 0;
		backgrounds['clients'] = 1;
		backgrounds['case-studies'] = 1;
		backgrounds[''] = 2;
		backgrounds['404'] = 2;
		backgrounds['news'] = 3;
		backgrounds['events'] = 3;
		backgrounds['we-are-selling'] = 3;
		backgrounds['our-story'] = 4;
		backgrounds['we-will-make'] = 4;
	}
	
	$(window).resize();

	$('#nav a').click(function(e)
	{
		e.preventDefault();
		
		var _this = $(this);
		
		// fix the wrapper height before fading stuff out
		$('#wrapper').height($('#wrapper').height());
		
		_this.parent().parent().find('li').removeClass('loading');
		_this.parent().addClass('loading');
		
		$('#nav').next().fadeOut(300, function() {
			//window.setTimeout(function (a) {
			  moveBackground(_this);
			//},300,_this);
		});
	});
});

function moveBackground(anchor)
{
	if(typeof anchor!=='undefined')
	{
	var path = anchor.attr('href');
	}
	else
	{
		var path = location.pathname;
	}
	path_s = path.split('/')[1];
	
	var n = (bg_frame_width * backgrounds[path_s]);
	var move_c = new_c - parseInt(n);

	if(typeof anchor!=='undefined')
	{
		// added this 'stop' to prevent queing
		$('body').stop().animate(
		{
			backgroundPosition:"("+move_c+"px 0)"
		},
		2000,
		"easeOutCubic",
		function()
		{
			window.location = anchor.attr('href');
		});
	}
	else
	{
		$('body').css({backgroundPosition: move_c+"px 0"});
	}
}

$(window).resize(function() {
	
	var c_pos = (bg_frame_width / 2);
	
	var win_c = $(window).width() / 2;
	
	new_c = Math.floor(win_c-c_pos);
	
	moveBackground();
});

$(document).ready(function(){ 
	$(document).pngFix({blankgif:'/images/blank.gif'}); 
});

$(function()
{
	
	$('#nav.col li a').each(function() {
		$(this).not($('.active')).hover(function() {   
			$(this).animate({color:'#eb008b'}, 300);	
		},
		function() {
			$(this).animate({color:'#000'}, 300);
		});
	});
	
	$('#news.events div.rsvp div').hide();
	
	$('#news.events div.rsvp h4 a').click(function(e) {
		e.preventDefault();

		$(this).parent().next().slideDown(500);
	});
});

$(function() {
	/*$('#home .col #subscribe form').submit(function(e) {
	e.preventDefault();
	
	var f = $(this).serialize();
	$.ajax({
			type: "POST",
			url: "./",
			data: "ajax=true&" + f,
			success: function(html)
			{	
				//alert(html);
				
				var v = $('#subscribe').parent();
				$('#subscribe').remove();
				v.prepend(html);
				$('#subscribe').find('p.submit').hide();
				hoverOver();
							over = false;
							$('h1, h2, h3, h4, p, a').each(fadeTextOut);
							$('#subscribe').animate({
								backgroundColor:'#ffffff'
							}, 200);
				
				//$('.item, .item div').not($('.break')).unbind('hoverIntent');
				//hoverOver();
				//alert( "Data Saved: " + msg );
			}
		});
	});   */
});

$(function() {
	$('#home #subscribe').hover(function() {
		$(this).find('.arrow').attr('src', '/images/email-submit-invert.gif');	
	},
	function() {
		$(this).find('.arrow').attr('src', '/images/email-submit.gif');
	});
});