var docloc = window.location.pathname;
var base = window.location.protocol + "//" + window.location.hostname;
var url = base + docloc;
//var investment = new RegExp("^"+base+"/investment/?$|^"+base+"/investment/principles/?.*$", "i");
var investment = new RegExp("^"+base+"/investment/?$|^"+base+"/investment/principles/?.*$|^"+base+"whatsnew/?.*$", "i");

var pma = new RegExp("^"+base+"/company/privately_managed_account/?.*","i");

var leavesite = function() { if(!confirm('You are now leaving the Muhlenkamp web site.')) { return false; } }
var enterfund = function() { if(!confirm('You are entering the Muhlenkamp Fund section of the site.')) { return false; } }
var leavefund = function() { if(!confirm('You are leaving the Muhlenkamp Fund section of the site.')) { return false; } }
var leavefundurl = function(url) { if(!confirm('You are leaving the Muhlenkamp Fund section of the site.')) { return false; } else { window.location = url; } }

$(document).ready(function() {
	$(document).pngFix();
	
//search box
	$('input.search-input').focus(function(ev) {
		if($(this).val() == "Search") { $(this).val(""); }
	});
	$('input.search-input').blur(function(ev) {
		if($(this).val() == "") { $(this).val("Search"); }
	});	
	
//corners
	$('#header-box').corner({ tl: { radius: 0 }, tr: { radius: 0 }, bl: { radius: 7 }, br: { radius: 7 }});

	$('.show-contact').click( function() {
		$(this).next(".contact-form").show();
		$(this).next("#tellafriend_form").show();
		$(this).next("#freeform").show();	
		return false;
	});
	
	$('.close').click( function() {
		$(this).parent(".contact-form").hide();
		$(this).parent("#tellafriend_form").hide();
		$(this).parent("#freeform").hide();
		return false;		
	});
	 
	/**
	 * Invoke the Glossary
	 * Make sure to load glossary.js first
	 *
	 * terms are now loaded inline
	 *
	 */

    // load the glossary definitions
    //this json request rewrites the page and loses link event handlers
    //$.getJSON('/glossary/terms.json',function(json) {
    
        // glossify anything machting the jquery selector
        $(".glossify").each(function (idx, el) {
            $(this).html(Glossary.process($(this).html(),glossaryterms))
        });
        
        // if we have glossary links then install the dialog template
        if ($('.glossary-term').size() > 0 ) {
            var tmpl = 
                '<div id="glossy-dialog" class="dialog-box" title="Move" style="text-align: center">' +
                    '<img id="glossy-loading-throbber" src="/images/glossy-throbber.gif" style="margin-top:30px" />' +
                '</div>';
        
            $('body').append(tmpl);                
            $('#glossy-dialog').dialog({
		          autoOpen: false,
		          width: 400
	         }).parent().css('overflow','visible');
	       // Dialog Tweaks     	         
	        $(".ui-dialog-titlebar-close").removeClass("ui-corner-all");
        	$('.close-this').insertBefore('.ui-dialog-title');
        	$('.close-this').click(function(){
        		$(this).parents('.ui-dialog').find('.dialog-box').dialog('close');
        		return  false;
        	});

        }
        
        // install a hook on every glossary link
        $('.glossary-term').click(function(ev){
            ev.preventDefault();
            
            // Hide all loaded glossary entries
            $('#glossy-dialog dl').hide();
                    
            // Check if we have this entry loaded an show it
            var definition = $(this).attr('rel');
            if (definition!='' && !document.getElementById(definition)) {   
                // show the loading indicator
                $('#glossy-loading-throbber').show();
                            
                // load the entry
                $.get('/glossary/definition/'+ definition.replace('glossy-definition-',''), function(data){
                    // hide the ajax indicator
                     $('#glossy-loading-throbber').hide();
                     $('#glossy-dialog').append(data);
                });
            } else {
                $('#'+definition).parent().show();
            }
            
            //offset to body relative to document and show the dialog
			var dtop=$(document).scrollTop();
			var dleft=$(document).scrollLeft();
			var offset=$(this).offset();
	
			var mco = $('#main-col').offset();
			var mcw = $('#main-col').width();
			
			$('#glossy-dialog').dialog('option','position', [((mcw-400)/2)+mco.left-dleft, offset.top-dtop+16]);									 
			$('#glossy-dialog').dialog('open');
            return false;	
        });
    //});
        
 //POST-GLOSSARY specific page PROCESSING

	//link modifiers
	$('a').each(function(index) {
		var link = "";
		//don't handle this
		if(typeof($(this).attr('href')) == "undefined") { return true; }
		link = this.href;
		//don't handle this
		if($(this).attr('href') == "" || $(this).attr('href') == "#" || $(this).attr('href') == " ") { return true; }
		if(link.search(/upload/) > -1) { return true; }
		
		//set all external links to provide warning
		$("a.leave-fund").bind('click', leavefund);
		
		if($(this).attr('href').search('^http?.') == 0 && link.search(window.location.host) == -1) {
			$(this).bind('click', leavesite);
		}
	
		//set warnings on entering/exiting investment principles
		else if( url.search(investment) > -1 || url.search(pma) > -1 ) {
			if(link.search(investment) == -1 && link.search(pma) == -1) {
				$(this).bind('click', enterfund);
			}
		} else if( url.search(investment) == -1 && link.search(investment) > -1 ) {
			$(this).bind('click', leavefund);
		} else if(url.search(pma) == -1 && link.search(pma) > -1) {
			$(this).bind('click', leavefund);
		}
		return true;
	});
	if ((window.location.pathname=="/fund/request-info")|(window.location.pathname=="/fund/request-info/")) {
		$("a.prospectus-msg").attr("onclick","alert('Please read the Muhlenkamp Fund Prospectus and Privacy Policy carefully before you invest.');");
	}

});

var check_a_friend = function(fid) {
	alert(fid);
}
