/* site wide */       
$(document).ready(function(){
	
	/* Random Background */
	var randomBG = Math.floor(Math.random()*3)+1;
	$('#main').css('background', 'url(/images/generic/background-' + randomBG + '.gif) 0 0 repeat');	
	
	/* Homepage quickLinks
	-------------------------------------------------------------------------------------------------- */	
	$('#quickLinks div.quickLink').hover(function(e){
		if($('.link', this).size() > 0){
			$(this).addClass('hover');
		}
	}, function(){
		$(this).removeClass('hover');
	});
	
	/* Business quickLinks
	-------------------------------------------------------------------------------------------------- */	
	$('.business div.quickLink').hover(function(e){
		if($('.link', this).size() > 0){
			$(this).addClass('hover');
		}
	}, function(){
		$(this).removeClass('hover');
	});
	
	/* Main Navigation
	-------------------------------------------------------------------------------------------------- */	
	$('#nav li').hover(function(){
	    $('#nav li.on ul').hide();
		$('ul', this).show();
	}, function(){
		$('ul', this).hide();
		$('#nav li.on ul').show();
	});
	$('#nav li.on ul').show();
	
	/* define easing
	-------------------------------------------------------------------------------------------------- */	
	$.easing.easeOutQuint = function(x,t,b,c,d){
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	};
	
	/* Homepage script
	-------------------------------------------------------------------------------------------------- */	
	if($('body').hasClass('home')){ //so it doesn't return javascript error if not homepage
		// images slider
		$('#caseStudy div.imageSlider').jcarousel({scroll:1, wrap: 'both'});
		$('#feature div.imageSlider').jcarousel({scroll:1, wrap: 'both'});
		// What's new scrollpane
		$('#whatsNew ol').jScrollPane({scrollbarWidth:11});
	}
	
	/* Assign link to promo
	-------------------------------------------------------------------------------------------------- */	
	$('div.promo').each(function(){
		var _promoContent = $(this).find('div.content');
		if(_promoContent.hasClass('link')){
			createLinks($(this));
		}
		else if(_promoContent.hasClass('scroll')){
			_promoContent.jScrollPane({scrollbarWidth:7});
		}
	});
	
	// imageGallery
	if($('#imageGalleryNav').size() > 0){
		$('#imageGalleryNav').jcarousel(
		    {scroll:1});
	}
	
	$('.jcarousel-item img').click(function(){
        var src = $(this).attr('src').replace('thumb_','');
        var desc = $(this).attr('alt');
        $('#mainImage').attr('src',src);
        $('#mainDescription').html(desc);
        var currentNumber = $(this).parent().parent().attr('jcarouselindex');
        $('#currentImage').html(parseInt(currentNumber));
        return false;
    });
	
	// case studies hover
	pushUp('body.home #caseStudy li.image-slider-item',-124,-7.83,true);
	pushUp('body.home #feature li.image-slider-item',-100,-7.83,true);
	pushUp('body.home #news div.news',-35,-3,false);
	pushUp('body.home #events div.events',-35,-3,false);
	
	// business concept page
	pushUp('body.business #item3',-108,-7.83,false);
	pushUp('body.business #item6',-90,-7.83,false);
	pushUp('body.business #item7',-90,-7.83,false);
	pushUp('body.business #item8',-90,-7.83,false);
	
	// features concept page
	pushUp('body.features #item1',-35,-11.55,false);
	pushUp('body.features #item2',-108,-6.7,false);
	pushUp('body.features #item3',-90,-6.2,false);
	pushUp('body.features #item4',-35,-5.5,false);
	pushUp('body.features #item5',-35,-7.25,false);
	pushUp('body.features #item6',-108,-6.7,false);
	pushUp('body.features #item7',-90,-6.2,false);
	pushUp('body.features #item8',-90,-6.2,false);
	pushUp('body.features #item9',-90,-6.2,false);
	pushUp('body.features #item10',-90,-6.2,false);
	pushUp('body.features #item11',-90,-6.2,false);
	pushUp('body.features #item12',-90,-6.2,false);
	
	/*pushUp('body.features #item1',-35,-5.5,false);
	pushUp('body.features #item2',-108,-6.7,false);
	pushUp('body.features #item3',-90,-6.2,false);
	pushUp('body.features #item4',-35,-5.5,false);
	pushUp('body.features #item5',-35,-5.5,false);
	pushUp('body.features #item6',-108,-6.7,false);
	pushUp('body.features #item7',-108,-6.7,false);
	pushUp('body.features #item8',-90,-6.2,false);
	pushUp('body.features #item9',-90,-6.2,false);
	pushUp('body.features #item10',-35,-5.5,false);
	pushUp('body.features #item11',-35,-5.5,false);
	pushUp('body.features #item12',-35,-5.5,false);*/
	
	pushUp('body.features .repeater1',-108,-6.7,false);
	pushUp('body.features .repeater2',-35,-5.5,false);
	pushUp('body.features .repeater3',-35,-5.5,false);
	pushUp('body.features .repeater4',-35,-5.5,false);
	pushUp('body.features .repeater5',-35,-5.5,false);
	pushUp('body.features .repeater6',-35,-5.5,false);
	pushUp('body.features .repeater7',-90,-6.2,false);
	pushUp('body.features .repeater8',-90,-6.2,false);
	pushUp('body.features .repeater9',-35,-5.5,false);
	pushUp('body.features .repeater10',-35,-5.5,false);
	
	// Community concept page
	pushUp('body.community #item3',-108,-7.83,false);
	pushUp('body.community #item6',-90,-7.83,false);
	pushUp('body.community #item7',-90,-7.83,false);
	pushUp('body.community #item8',-90,-7.83,false);
	
	// About concept page
	pushUp('body.about #item3',-108,-7.83,false);
	pushUp('body.about #item6',-90,-7.83,false);
	pushUp('body.about #item7',-90,-7.83,false);
	pushUp('body.about #item8',-90,-7.83,false);
	
	/* Adding shadow + resource listing
	-------------------------------------------------------------------------------------------------- */	
	// loop through all lower page content blocks and set it's shadow width and height
	$('#layout div.block').each(function(){
		var _block = $(this);
		if(!_block.hasClass('resourceListing')){
			addShadow(_block);
			}
		else{
			var _listing = $(this).find('.listing');
			_listing.find('ul').hide(); //while we're here, hide content.
		}
	});
	$('#layout div.resourceListing').each(function(){
		var _block = $(this);
		_block.find('h2').toggle(function(){
			_block.addClass('rlExpand');
			//_block.find('ul').slideDown({duration:300, easing:'easeOutQuint', complete:function(){ addShadow(_block); }});
		    _block.find('ul').animate({ height: 'toggle', easing:'easeOutQuint'}, 300, function() { addShadow(_block); });

		}, function(){
		    _block.removeClass('rlExpand');
		    //_block.find('ul').slideUp({duration:300, easing:'easeOutQuint', complete:function(){ _block.css('margin-bottom':'50px'); }});
		    _block.find('ul').animate({height:'toggle', easing:'easeOutQuint'},300);
			_block.find('.shr').height(0);
			_block.find('.shb').width(0);
			_block.css({'min-height':0});
		});
	});
	
});

/* ...................................................
	
	adding shadows
	
................................................... */
function addShadow(block){
	var _this = block;
	var gbh = _this.innerHeight();
	var gbw = _this.innerWidth();
	_this.find('.shr').height(gbh);
	_this.find('.shb').width(gbw-5);
	_this.css({'min-height':gbh+5});
}

/* ...................................................
	
	push up function
	
................................................... */
function pushUp(elem, imagePushUp, descPushUp, headingTweak){
	$(elem).hover(function(){
		var t = $(this);
		var im = $(t).find('.image');
		$(im).find('img').css({'position':'relative'}).stop().animate({'top':imagePushUp}, {easing:'easeOutQuint', duration: 700});
		$(t).find('.desc').stop().animate({'bottom':0+'em'}, {easing:'easeOutQuint',duration: 700});
		if(headingTweak == true){
			$(t).find('.desc h2').css({'padding-top':16});
		}
	}, function(){
		var t = $(this);
		var im = $(t).find('.image');
		$(im).find('img').css({'position':'relative'}).stop().animate({'top':0}, {easing:'easeOutQuint',duration: 700});
		$(t).find('.desc').stop().animate({'bottom':descPushUp+'em'}, {easing:'easeOutQuint',duration: 700});
		if(headingTweak == true){
			$(t).find('.desc h2').css({'padding-top':0});	
		}
	});
}

/* ...................................................
	
	Block level clicking
	
................................................... */
function createLinks(items) {
	items.css('cursor', 'pointer');
	items.bind("click", function () {
		var link = $(this).find('a');
		if (link.length > 0) {
			location.href = link.attr('href');
		}
		return false;
	});
	items.hover(function() {
		$(this).addClass('scriptHover');
	}, function(){
		$(this).removeClass('scriptHover');
	});
} 
