$(window).unload( function () { toggle_loader(1); } );

$(document).ready( function(){
    
    $('a.main_nav').each(function()
    {
        var height = $(this).children('span').outerHeight();
        //alert(height);
        if(height < 20)
        {
            $(this).children('span').css('margin-top', '8px');
        }
    });
    
    $('a.main_nav').click(function()
    {
        var color = $(this).css('background-color');
        $('#color_bar').css('background-color', color);
    });
    
	toggle_loader(0);

	$(".main_nav").click(function(){
    	main_nav(this.id);
    });

	$.history.init(function(full_location) {processHashLocation(full_location);});
	$("a[@rel='history']").click(function(){
        $.history.load(this.href.replace(/^.*#/, ''));
        return false;
    });


	bind_sort_by_over();

	//roll over function
	$(".roll-image").hover(
		function () {
			var img_orig = $(this).attr("src");
			var img_replace = img_orig.replace("_off.gif", "_on.gif");
			$(this).attr( "src", img_replace )
		},
		function () {
			var img_orig = $(this).attr("src");
			var img_replace = img_orig.replace("_on.gif", "_off.gif");
			$(this).attr( "src", img_replace )
		}
	);

	//Used to clear a form
	//eg $('#FormIdHere').clearForm();
	$.fn.clearForm = function() {
		this.each(function() {
			var type = this.type, tag = this.tagName.toLowerCase();

			if (tag == 'form')
				return $(':input',this).clearForm();
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = '';
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			else if (tag == 'select')
				this.selectedIndex = '';
		});
	};

	$("form input").keypress(function (e) {
		if (((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) && (!$(this).is(".auto_complete"))) {
			 $(this).parents('form').submit();
			return false;
		} else {
			return true;
		}
	});

});

function submitEnter(myfield,e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13){
	   myfield.form.submit();
	   return false;
	}else{
	   return true;
	}
}

/*register functions */
function country_states(id, theform, the_element, prefix,clear){

	if(id == 840){
		toggleTable(prefix+'_el_1',1);
		toggleTable(prefix+'_el_2',0);
		toggleTable(prefix+'_el_3',0);

		toggleTable(prefix+'_el_not_man',0);
		toggleTable(prefix+'_el_man',1);

		theform.elements[prefix+"_state_province"][0].selected = true;
	}else if(id == 124){
		toggleTable(prefix+'_el_1',0);
		toggleTable(prefix+'_el_2',1);
		toggleTable(prefix+'_el_3',0);

		toggleTable(prefix+'_el_not_man',0);
		toggleTable(prefix+'_el_man',1);

		theform.elements[prefix+"_canada_province"][0].selected = true;
	}else{
		toggleTable(prefix+'_el_1',0);
		toggleTable(prefix+'_el_2',0);
		toggleTable(prefix+'_el_3',1);

		toggleTable(prefix+'_el_not_man',1);
		toggleTable(prefix+'_el_man',0);

		if(clear)
		theform.elements[the_element].value = "";
	}
}

function toggleTable(id,state) {
	if(state == 1){
		$("#"+id).show();
	}else if(state == 0){
		$("#"+id).hide();
	}else{
		$("#"+id).toggle();
	}
}

function set_state(theform,str,the_element){
	theform.elements[the_element].value = str;
}

function submitForm(theform, theaction){
	theform.action = theaction;
	theform.submit();
}


function bind_sort_by_over(){
	 if($.browser.msie) {
		if(document.getElementById("sort_by_nav")){

			category_nav_over = function() {
				var sfEls = document.getElementById("sort_by_nav").getElementsByTagName("li");
				for (var i=0; i<sfEls.length; i++) {
					sfEls[i].onmouseover=function() {
						this.className+=" over";
					}
					sfEls[i].onmouseout=function() {
						this.className=this.className.replace(new RegExp(" over\\b"), "");
					}
				}
			}

			category_nav_over();
			//if (window.attachEvent) window.attachEvent("onload", category_nav_over);
		}
	}
}

function main_nav(id){

	var id_orig = id;
	var id_num = id_orig.replace("mn-", "");

	var on_nav_class = $("#nav-"+id_num).attr( "class" );
	var on_nav_class_no_first = on_nav_class.replace("nav_td_first", "*first*");
	var on_nav_class_not_on = on_nav_class_no_first.replace("nav_td_on", "nav_td");
	var on_nav_class_on = on_nav_class_not_on.replace("nav_td", "nav_td_on");
	var new_on_nav_class = on_nav_class_on.replace("*first*", "nav_td_first");

	$("#nav-"+id_num).attr( "class", new_on_nav_class );

	for(x = 0; x < main_nav_count; x++){
		if(id_num == x){

		}else{

			var nav_class = $("#nav-"+x).attr( "class" );
			var nav_class_no_first = nav_class.replace("nav_td_first", "*first*");
			var nav_class_not_on = nav_class_no_first.replace("nav_td_on", "nav_td");
			var new_nav_class = nav_class_not_on.replace("*first*", "nav_td_first");

			$("#nav-"+x).attr( "class", new_nav_class );
		}
	}
}

function side_nav(id){

	$("#sc-"+id).show(400);
	$("#s-"+id).attr( "class", "side_20_on" );

	for(x = 0; x < side_nav_arr.length; x++){
		if(id == side_nav_arr[x]){

		}else{
			$("#sc-"+side_nav_arr[x]).hide(370);
			$("#s-"+side_nav_arr[x]).attr( "class", "side_20" );
		}
	}
}

////hash controller
function processHashLocation(full_location){

//	var target = location.hash;
//	var full_location = target.replace("#", "");

	if(full_location != ""){
		AjaxCall(full_location, 'main_right');
	}
}


function AjaxCall(url, target){

	if(url != "" && target != ""){
		toggle_loader(1)

		$('#'+target).fadeTo("slow", 0.2, function () {
			$.get(url, { AjaxCall: "1"},
			  function(data){
			  	$("#"+target).html(data);
			  	goUp('#'+target);
			  	toggle_loader(0);
			});
		});
	}
}

function goUp(who) {
	$(who).fadeTo("fast",1);
}
function goDown(who) {
	$(who).fadeTo("slow", 0);
}

function toggle_loader(state){
/* loader positioning */
	var loader = $('#loader');
	var w = ( $(window).width() - 115 );
	loader.css('left', w);
	loader.css('bottom', 0);
/* loader positioning end */

	if(state == 1){
//		$('#loader').css('display', 'block');
		$('#loader').css('zIndex', '1000');
		$('#loader').fadeTo("fast",1);
	}else{
		$('#loader').fadeTo("slow",0);
		$('#loader').css('zIndex', '-1000');
//		$('#loader').css('display', 'none');
	}
}


function ConfirmAjaxCall(url, target, msg){
	if(confirm(msg)){
		AjaxCall(url, target)
	}
}

function getRating(id, rating){
	toggle_loader(1);
	$.get("/sites/", { id: id, action: "rate", rating: rating },
	function(data){
	  	$("#stars_" + id).html(data);
	  	toggle_loader(0);
	});
}

function toolbox_action(tool_action, id){
	toggle_loader(1);

	$.get("/sites/", { id: id, action: tool_action },
	function(data){
	  	$("#toolbox_div").html(data);
	  	toggle_loader(0);
	});
}

function DoSearch(){
	var search_key = $("#sites_search").val();
	var section_id = $("#sites_search_section").val();

	if(search_key != ""){
		var hashvalue = "/sites/?search="+search_key;
		if(section_id != ""){
			hashvalue += "&s="+section_id;
		}

		location.hash = hashvalue;
	}
}


function show_hide_toggle(state, id){

	var tmp = $('#site_comment_count-'+id).html();

	var count = tmp*1;

	if(count > 0){
		if(state == 1){
			$('#hide_comment-'+id).show();
		}else{
			$('#hide_comment-'+id).hide();
			$('#site_comment-'+id).html(' ');
		}
	}
}


function bio_man_toggle(){
	if($('#contribute_chk').is(':checked')){
		$('#bio_man').show();
	}else{
		$('#bio_man').hide();
	}
}
