﻿function InitDefault() {

	// call other Init functions
	InitNav();

	DoStriping();

	// helpers for style
	$('.two-col-listing li:odd').addClass('no-right-margin');

}

function InitGrid() {
	$('html').keyup(function(event) {
		if (event.keyCode == 27) {
			($('body').hasClass('grid')) ? $('body').removeClass('grid') : $('body').addClass('grid');
		}
	});
}

function InitNav() {
	$('#main-nav li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
		$(this).children('ul').css('left', -9999);
		$(this).children('ul').css('opacity', 1);
	});
}

function InitExpand() {
	$('#hidden-tags').hide();
	$('#expand-tag-listing').toggle(
		function() {
			$('#hidden-tags').slideDown();
			$('#expand-tag-listing').text("Contract list")
			$('#expand-tag-listing').addClass('up');
		}, function() {
			$('#hidden-tags').slideUp();
			$('#expand-tag-listing').text("Expand list")
			$('#expand-tag-listing').removeClass('up');
		}
	)
}


function SetEnterSubmit() {
	// usage: add the class 'submit-on-enter' to any element
	$(".submit-on-enter input[type=text], .submit-on-enter textarea").keydown(function(ev) {
		//$(".submit-on-enter").append(ev.keyCode);
		if (ev.keyCode == 13) {
			ev.preventDefault();
			ev.stopPropagation();
			//$(".submit-on-enter").append("==submitting");    
			$(this).parents(".submit-on-enter").children("input[type=image], input[type=button]").click();
			return false;
		}
	});
}

function SetExternalLinks() {
	/*
	Purpose: checks all <a>'s in the doc, if it has a rel attribute of 'external', sets to open in new window
	usage: <a href="http://www.google.com" rel="external">Google</a>
	*/

	$('a[rel=external]').click(function() { window.open(this.href); return false; });

}

function Equalise(elms) {
	/*
	Purpose: simple EQ for columns / pods / etc
	usage: Equalise($(".eq-what";
	*/
	var highest = 0;
	for (i = 0; i < elms.length; i++) { if ($(elms[i]).height() > highest) highest = $(elms[i]).height(); }
	for (i = 0; i < elms.length; i++) { if ($(elms[i]).height() < highest) $(elms[i]).height(highest); }
}

function DoStriping() {
	$('table tr:even').addClass("alt");
}

jQuery.fn.setEqualHeight=function(o) {
    var maxHeight=0;
    var maxElement=null;
    jQuery(this).each(function(i) {
  		    if((jQuery(this).height()+parseInt(jQuery(this).css("padding-bottom"))+parseInt(jQuery(this).css("padding-top")))>maxHeight) {
		    maxHeight=jQuery(this).height()+parseInt(jQuery(this).css("padding-top"))+parseInt(jQuery(this).css("padding-bottom"));
		    maxElement=this;
	    }
    });
    jQuery(this).not($(maxElement)).each(function() {$(this).height(maxHeight-parseInt(jQuery(this).css("padding-top"))-parseInt(jQuery(this).css("padding-bottom")))})
}

function InitGateway() {

   	$(".gateway-panel-header").setEqualHeight();
   	$(".gateway-panel-header1").setEqualHeight();
   	$(".gateway-panel-header2").setEqualHeight();
   	$(".gateway-panel-header3").setEqualHeight();   

}

function InitHomepage() {
	$('#home-tabs li a').hover(function() {
		$('#home-tabs li').removeClass('selected')
		$(this).parent().addClass('selected')
		$('#home-tabs').attr('class', '');
		$('#home-tabs').addClass($(this).parent().attr('id'));
		$('#home-tabs').addClass('self-clear');

		//$('#homepage-feature .section').fadeOut();
		$('#homepage-feature .section').hide();
		
		var toShow = '#' + $(this).parent().attr('id') + '-data';
		//$(toShow).stop().fadeIn();
		$(toShow).stop().show();
		
		return false;
	});

	$('#proptabs li a').click(function() {
		$('#proptabs li').removeClass('selected')
		$(this).parent().addClass('selected')
		$('#proptabs').attr('class', '');
		$('#proptabs').addClass($(this).parent().attr('id'));
		$('#proptabs').addClass('self-clear');

		//$('#homepage-feature .section').fadeOut();
		$('#who-what-how .section').hide();

		var toShow = '#' + $(this).parent().attr('id') + '-section';
		//$(toShow).stop().fadeIn();
		$(toShow).stop().show();

		return false;
	});

	$('#homepage-story').cycle({
		fx: 'fade',
		timeout: 7000,
		delay: 2000,
		pause: 'true'
	});
}

