var map = null;

function is_valid_email(email) {
  var regex = /^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/;
  return regex.test(email); 
}

function get_selected_value(elem_id) {
	var fobj = document.getElementById(elem_id);
	var val = '';
	
	if (fobj.selectedIndex >= 0) {
		var fsel = fobj[fobj.selectedIndex];
		val = (fsel.value.length > 0) ? fsel.value : fsel.text;
	}
	return val;
}

function toggle_elem(link, elem_id) {
	var elem = document.getElementById(elem_id);
	
	if (elem.style.display == 'none') {
		elem.style.display = 'block';
		link.innerHTML = '-';
	} else {
		elem.style.display = 'none';
		link.innerHTML = '+';
	}
	return false;
}

function format_currency(str) {
	str += '';
	x = str.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function set_selected_topnav_menuitem(start) {
	if ($('#topnav_selected').length != 0) {
		return;
	}

	// Set currently selected top menu item
	var page = window.location.pathname.replace(/.*\//i, '');
    if (page=='')
    {
        page = window.location.pathname.slice(0, -1);
        page = page.replace(/.*\//i, '');
    }   
    var selected_page, curr_page, bc_link;

	$('#topnav').find('#topnav > li > a').each(function() {
		curr_page = $(this).attr('href').replace(/.*\//i, '');
        if (curr_page=='')
        {
            curr_page = $(this).attr('href').slice(0, -1);
            curr_page = curr_page.replace(/.*\//i, '');
        }   

		if (curr_page == page) {
			selected_page = this;
		}
	});

	var set_breadcrumb = false;
	if (selected_page) {
		$(selected_page).attr('id', 'topnav_selected');
		append_breadcrumb($(selected_page).text(), window.location);
	} else {
		$('#topnav').find('.topnav_sub a').each(function() {
			curr_page = $(this).attr('href').replace(/.*\//i, '');
            if (curr_page=='')
            {
                curr_page = $(this).attr('href').slice(0, -1);
                curr_page = curr_page.replace(/.*\//i, '');
            }               
			if (curr_page == page) {
				if (!set_breadcrumb) {
					selected_page = $(this).parent().parents('li').find('a:first');
					append_breadcrumb($(selected_page).text(), '');
                    bc_link = window.location.protocol + '//' + window.location.host + window.location.pathname;
                    append_breadcrumb($(this).text(), bc_link);
					set_breadcrumb = true;
				}
			}
		});
		if (selected_page) {
			$(selected_page).attr('id', 'topnav_selected');
		}
	}	
}

function append_breadcrumb(name,link) {
	var bc = document.createElement('li');
	if (link.length > 0) {
		var lnk = document.createElement('a');
		lnk.setAttribute('href', link);
		$(lnk).append(document.createTextNode(name));
		$(bc).append(lnk);
	} else {
		$(bc).append(document.createTextNode(name));
	}
	$('#site_nav_line ul').append(bc);
	//alert(name);
}

//On Hover Over
function megaHoverOver(){
    $(this).find(".topnav_sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...
        var biggestRow = 0;	

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".topnav_sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
    } else { //If row does not exist...
        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".topnav_sub").css({'width' : rowWidth}); //Set Width
    }
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".topnav_sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}

// Toggle hover
/*function megaHoverToggle() {
	var has_menu = ($(this).find('a:first').attr('href') == '#');
	
	if (has_menu) {
		//$(this).find(".topnav_sub").stop().fadeTo('fast', 1).show();
	}
	return !has_menu;
}*/
$(document).unload(megaHoverOut);

$(document).ready(function(){ 
	//Set custom configurations for top menu mega dropdown
	var config = {
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		 interval: 50, // number = milliseconds for onMouseOver polling interval
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		 timeout: 300, // number = milliseconds delay before onMouseOut
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	$('ul#topnav li .topnav_sub').css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$('ul#topnav li').hoverIntent(config); //Trigger Hover intent with custom configurations
	//$('ul#topnav > li').toggle(megaHoverOver, megaHoverOut);
	//$('#topnav > li').megaHoverOut();
	
	set_selected_topnav_menuitem();
	$('#site_nav_line').jBreadCrumb({easing:'linear'});
	
	// Set form validation
	$('form').submit(function() {
		$('.ee_table_form_hp2').attr('value','');
		return true;
	});
	
	// Buttons
	if (jQuery.fn.button) {
		$('button, input:submit, input:button, a.button').button();
	}
});

