function toggleAll(element) {
	var form = document.forms.openinviter, z = 0;
	for(z=0; z<form.length;z++) {
		if(form[z].type == 'checkbox')
		form[z].checked = element.checked;
	}
}

function hide_content () {
	$('.tab-content').each(function (i) {
		$(this).hide();
	});
}

function show_anchor_tab (tab) {
	var myFile = document.location.toString();
	if (myFile.match('#')) {
		var myAnchor = myFile.split('#')[1];
		
		if ($('#flashMessage')) {
			$("#flashMessage").clone().prependTo('#' + myAnchor + 'Message');
		}
		
		$('.tab[content="' + myAnchor + '"]').click();
	} else {
		$(tab).show();
	}
}

function resetpage () {
	$('.fancyhelp').hide();
	$('.nid').hide();
	
	// Handling with tables (adding first and last classes for borders and adding alternate bgs)
		$('tbody tr:even').addClass('even');
		$('table.grid tbody tr:last-child').addClass('last');
		$('tr th:first-child, tr td:first-child').addClass('first');
		$('tr th:empty, th:has(img), tr td:has(img)').removeClass('first');
		$('tr th:last-child, tr td:last-child').addClass('last');
		$('form.fields fieldset:last-child').addClass('last');
	// Handling with lists (alternate bgs)
		$('ul.simple li:even').addClass('even');
	// Handling with grid views (adding first and last classes for borders and adding alternate bgs)
		$('.grid .line:even').addClass('even');
		$('.grid .line:first-child').addClass('firstline');
		$('.grid .line:last-child').addClass('lastline');
}

$(document).ready(function() {
	
	
	// #main_nav ul li a.selected {
		// background:transparent url('../img/nav_arrow.png') no-repeat bottom;
	// }
	
	
	
	// $('#features_page img').each(function (index, img) {
	// 	$(img).css({'float' : 'right', 'margin-left' : '30px', 'margin-top' : '-30px'});
	// });
	
	$('#main_nav ul li:has(a.selected)').css('background', 'transparent url(\'http://chooey.com/themed/chooey/img/nav_arrow.png\') no-repeat bottom');
	
	$('#search_field').focus(function () {
		$(this).val('');
	});
	
	$('#search_field').blur(function () {
		if ($(this).val() == '') {
			$(this).val('Search');
		}
	});
	
	resetpage();
	
	$('.status').css({'cursor' : 'pointer'}).click(function () {
		status = $(this).attr('status');
		if (status == 'up') {
			$(this).attr('src', image_path + 'action_down.png');
			$(this).attr('status', 'down');
			$(this).parent().find('.box_content').show();
			
			$(this).parent().parent().find('.box_content').show();
			$(this).parent().parent().find('h2').css({
				'-moz-border-radius': '0px',
				'-webkit-border-radius': '0px',
				'border-radius': '0px',
				'-moz-border-radius-topleft':'5px',
				'-webkit-border-top-left-radius':'5px',
				'-moz-border-radius-topright':'5px',
				'-webkit-border-top-right-radius':'5px'
			});
		} else {
			$(this).attr('src', image_path + 'action_up.png');
			$(this).attr('status', 'up');
			$(this).parent().parent().find('.box_content').hide();
			$(this).parent().parent().find('h2').css({
				'-moz-border-radius': '5px',
				'-webkit-border-radius': '5px',
				'border-radius': '5px'
			});
		}
	});
	
	//On Hover Over
	function megaHoverOver(){
		
		var class_name = $(this).attr('class');
		
		$(this).find('.link').css({
			'-moz-border-radius': '0px',
			'-webkit-border-radius': '0px',
			'border-radius': '0px',
			'-moz-border-radius-topleft':'10px',
			'-webkit-border-top-left-radius':'10px',
			'-moz-border-radius-topright':'10px',
			'-webkit-border-top-right-radius':'10px',
			'border': '2px solid white',
			'margin': '0px -1px',
			'background-color' : '#2b1057',
			'border-bottom':'none'
		});
		
		if (class_name != 'active') {
			$(this).find('.link').css({
				'margin': '0 -2px',
				'color': 'white'
			});
		}
		
	    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
	}
	//On Hover Out
	function megaHoverOut(){
		
		var class_name = $(this).attr('class');
		
		if (class_name != 'active') {
			$(this).find('.link').css({
				'border':'none',
				'background-color': 'transparent',
				'margin': '0',
				'color': '#9d7cd2'
			});
		} else {
			$(this).find('.link').css({
				'-moz-border-radius': '10px',
				'-webkit-border-radius': '10px',
				'border-radius': '10px',
				'border': '1px solid #7858ac',
				'margin': '0',
				'background-color' : '#3b1b70'
			});
		}
		
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
	      $(this).hide();  //after fading, hide it
	  });
	}
	
	//Set custom configurations
	var config = {
	     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
	     interval: 100, // number = milliseconds for onMouseOver polling interval
	     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 500, // number = milliseconds delay before onMouseOut
	     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	$(".sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("#main_nav li").hoverIntent(config); //Trigger Hover intent with custom configurations
	
	
	// Range Big Menu
	
	//On Hover Over
	function rangeMegaHoverOver(){
		
		var class_name = $(this).attr('class');
		
		$(this).find('.range_link').css({
			'-moz-border-radius': '0px',
			'-webkit-border-radius': '0px',
			'border-radius': '0px',
			'-moz-border-radius-topleft':'10px',
			'-webkit-border-top-left-radius':'10px',
			'-moz-border-radius-topright':'10px',
			'-webkit-border-top-right-radius':'10px',
			'border': '2px solid #2b1057',
			'margin': '0px -1px',
			// 'background-color' : '#2b1057',
			'border-bottom':'none'
		});
		
		if (class_name != 'active') {
			$(this).find('.link').css({
				'margin': '0 -2px',
				'color': 'white'
			});
		}
		
	    $(this).find(".range_sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
	}
	//On Hover Out
	function rangeMegaHoverOut(){
		
		var class_name = $(this).attr('class');
		
		$(this).find('.range_link').css({
			'-moz-border-radius': '10px',
			'-webkit-border-radius': '10px',
			'border-radius': '10px',
			'border': '1px solid #ddcff4',
			'margin': '0',
			'background-color' : '#fff'
		});
		
	  $(this).find(".range_sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
	      $(this).hide();  //after fading, hide it
	  });
	}
	
	//Set custom configurations
	var range_config = {
	     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
	     interval: 100, // number = milliseconds for onMouseOver polling interval
	     over: rangeMegaHoverOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 500, // number = milliseconds delay before onMouseOut
	     out: rangeMegaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	$(".sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$(".range").hoverIntent(range_config); //Trigger Hover intent with custom configurations
	
	
	hide_content();
	$('.tab').click(function () {
		$('.tab').each(function () {
			$(this).removeClass('active');
		});
		$(this).addClass('active');
		hide_content();
		content = $(this).attr('content');
		$('#' + content).show();
		
		return false;
	});
});