$(function () {

	// Random Background
	var randomBG = Math.floor(Math.random() * 3) + 1;
	$('#main').addClass('bg' + randomBG);

	$('input.keepInTouchInput').focus(function () {
		if ($(this).val() == 'enter email')
			$(this).val('');
	});
	$('input.keepInTouchInput').blur(function () {
		if ($(this).val() == '')
			$(this).val('enter email');
	});
	/* 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', '#imageGalleryNav').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;
	});

	// home page boxes hover
	pushUp('body.home #caseStudy li.image-slider-item', -8, -8, false);
	pushUp('body.home #feature li.image-slider-item', -8, -8, false);
	pushUp('body.home #news div.news', -6, -6, false);
	pushUp('body.home #events div.events', -6, -6, false);

	// landing page, featureBoxes
	pushUp('.tile', -6.4, -6.4, false);
	pushUp('.tilex2', -7.4, -7.4, false);
	pushUp('.tilex3', -7.4, -7.4, 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 () {
	if (!$(this).hasClass('resourceListing'))
	addShadow($(this));
	});*/

	$('.listing ul', '.resourceListing').hide();
	$('#layout div.resourceListing').each(function () {
		var $$ = $(this);
		$$.find('h2').toggle(function () {
			$$.addClass('rlExpand');
			$('ul', $$).animate({
				height: 'toggle',
				easing: 'easeOutQuint'
			}, 300, function () {
				$('.shr', $$).show();
				$('.shb', $$).show();
				$('.shc', $$).show();
				//addShadow($$);
				$('.content', $$).height($$.height());
			});

		}, function () {
			$$.removeClass('rlExpand');
			$('.content', $$).height('auto');
			$$.find('ul').animate({
				height: 'toggle',
				easing: 'easeOutQuint'
			}, 300, function () {
				if ($.browser.msie) {
					$($$).next('.block').css('padding-top', '6px');
				}
			});
			$('.shr', $$).hide();
			$('.shb', $$).hide();
			$('.shc', $$).hide();
			$$.css({
				'min-height': 0
			});
		});
	});

});

/* adding shadows */

function addShadow(_block) {
    return;
    var gbh = _block.innerHeight();
    var gbw = _block.innerWidth();
    _block.find('.shr').height(gbh);
    _block.find('.shb').width(gbw - 5);
    _block.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 + 'em'
        }, {
            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');
    });
}
