var J = jQuery.noConflict();

//strpos function
function strpos (haystack, needle, offset) {
	var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
	return i === -1 ? false : i;
}

//url parser addon
J.url=function(){var segments={};var parsed={};var options={url:window.location,strictMode:false,key:["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:{strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}};var parseUri=function(){str=decodeURI(options.url);var m=options.parser[options.strictMode?"strict":"loose"].exec(str);var uri={};var i=14;while(i--){uri[options.key[i]]=m[i]||""}uri[options.q.name]={};uri[options.key[12]].replace(options.q.parser,function($0,$1,$2){if($1){uri[options.q.name][$1]=$2}});return uri};var key=function(key){if(!parsed.length){setUp()}if(key=="base"){if(parsed.port!==null&&parsed.port!==""){return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/"}else{return parsed.protocol+"://"+parsed.host+"/"}}return(parsed[key]==="")?null:parsed[key]};var param=function(item){if(!parsed.length){setUp()}return(parsed.queryKey[item]===null)?null:parsed.queryKey[item]};var setUp=function(){parsed=parseUri();getSegments()};var getSegments=function(){var p=parsed.path;segments=[];segments=parsed.path.length==1?{}:(p.charAt(p.length-1)=="/"?p.substring(1,p.length-1):path=p.substring(1)).split("/")};return{setMode:function(mode){strictMode=mode=="strict"?true:false;return this},setUrl:function(newUri){options.url=newUri===undefined?window.location:newUri;setUp();return this},segment:function(pos){if(!parsed.length){setUp()}if(pos===undefined){return segments.length}return(segments[pos]===""||segments[pos]===undefined)?null:segments[pos]},attr:key,param:param}}();
//end url parser addon


function initObjects()
{
		swfobject.embedSWF("/public/swf/slideshow.swf", "testimonials", "848", "276", "9.0.0", false, {}, {wmode: "transparent"});
}

function initObjectsLeft_Post()
{
	if (navigator.appName == 'Microsoft Internet Explorer' || (navigator.appVersion.indexOf('Chrome') < 1 && navigator.appVersion.indexOf('Safari') < 1))
	{
		document.getElementById('LeftPaneDiv').className = 'LeftPaneDivIE' ;
	}
	else
	{
		document.getElementById('LeftPaneDiv').className = 'LeftPaneDiv' ;		
	}

}

function initObjectsMid_Post()
{
	if (navigator.appName == 'Microsoft Internet Explorer' || (navigator.appVersion.indexOf('Chrome') < 1 && navigator.appVersion.indexOf('Safari') < 1))
	{
		document.getElementById('MidPaneDiv').className = 'MidPaneDivIE' ;
	}
}

function initObjectsRight_Post()
{
	if (navigator.appName == 'Microsoft Internet Explorer' || (navigator.appVersion.indexOf('Chrome') < 1 && navigator.appVersion.indexOf('Safari') < 1))
	{
		document.getElementById('RightPaneDiv').className = 'RightPaneDivIE' ;
	}

}


var mlShown = false; //track if box is shown or not
var mlTimer; //var to hold timer
var mlTrans = 500; //var to hold delay for fade
var mlDelay = 3000; //how long after mouse out to close window
var mlFocus = false;

function mlShow(){
	if(!mlShown){
		J('.popup-box').fadeIn(mlTrans);
		window.setTimeout("mlShown = true",mlTrans);
		if (document.getElementById('MidPaneDiv').className == 'MidPaneDiv') 
			document.getElementById('MidPaneDiv').className = 'MidPaneDiv2' ;
		if (document.getElementById('MidPaneDiv').className == 'MidPaneDivIE') 
			document.getElementById('MidPaneDiv').className = 'MidPaneDiv2IE' ;
	}
}

function mlHide(){
	if(mlShown){
		window.clearTimeout(mlTimer);
		J('.popup-box').fadeOut(mlTrans);
		window.setTimeout("mlShown = false",mlTrans);
		if (document.getElementById('MidPaneDiv').className == 'MidPaneDiv2') 
			document.getElementById('MidPaneDiv').className = 'MidPaneDiv' ;
		if (document.getElementById('MidPaneDiv').className == 'MidPaneDiv2IE') 
			document.getElementById('MidPaneDiv').className = 'MidPaneDivIE' ;		
	}
}

function mlToggle(){
	if(mlShown){
		mlHide();
	} else {
		mlShow();
	}
}

function mlRef(){ //refreshes the timer
	if(mlShown){
		window.clearTimeout(mlTimer);
	}
}

//set up the function to use for checking enter
function chkKey(e){
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		//submit the form
		ValidateMemberPopupInfo();
		return false
	}
	else{
		return true
	}
}

J(document).ready(function(){	

	J('a.member-login').click(function(){
		mlToggle();
		return false;
	});
	
	J('.popup-box').hover(function(){
		window.clearTimeout(mlTimer);
	}, function(){
		window.clearTimeout(mlTimer);
		mlTimer = window.setTimeout("mlHide()",mlDelay);
	});
	
	document.body.onkeyup = function(e){
		if(mlShown){//make sure the box is visible... not sure how this could happen that the box wasn't visible... but /shrug
			chkKey(e);
		}
	}
	
	J('#mlClose').click(function(){
		mlHide();
	});
	
	//added to insert enter submission functionality to the login box
	J('.popup-box').find('input').each(function(){
		//
		this.onkeyup = function() {
			mlRef(); //refresh the timer while typing to prevent window from closing
		}
	});
	
	J('#dnn_RightPane').find('object').each(function(){
		var tC = J(this.parentNode).html();
		//get the position of the > in the object tag and add the new param to the string
		var tCStart = strpos(tC,">");
		var tC = tC.substr(0,tCStart) + '><param name="wmode" value="transparent">' + tC.substr(tCStart+1);
		var tCStart = strpos(tC,"<embed");
		var tC = tC.substr(0,tCStart) + '<embed wmode="transparent"' + tC.substr(tCStart+6);
		J(this).replaceWith(tC);
	});
	
	//fix for cool template right side
	J('.side-content .shadow').find('object').each(function(){
		var tC = J(this.parentNode).html();
		//get the position of the > in the object tag and add the new param to the string
		var tCStart = strpos(tC,">");
		var tC = tC.substr(0,tCStart) + '><param name="wmode" value="transparent">' + tC.substr(tCStart+1);
		var tCStart = strpos(tC,"<embed");
		var tC = tC.substr(0,tCStart) + '<embed wmode="transparent"' + tC.substr(tCStart+6);
		J(this).replaceWith(tC);
	});
	//end fix for cool template right side
	
	//check to see if we have a redirect from COOL
	if(J.url.attr("query")){
		if(J.url.param("r") == ""){
			//fade in the box with no timer because we're on the "login" page
			window.setTimeout("mlShow()",50);
			window.setTimeout("checkCookies()",50);
		}
	}
	
	//added to fix map page load error in FF2/3
	window.setTimeout("fixMap()",10000);
	
	//added to loop through form module submit graphics and make them link to the top of the page when clicked
	J('.DNNCentricMultilanguageFormsContent').find('input[type=image]').each(function(){
		//
		this.onclick = function() {
			window.scrollTo(0,0);
		}
	});
});

//added to fix map page load error in FF2/3
function fixMap() {
	var mapCID = J('div[class=NetismMap_Map]').attr('id');
	if(document.getElementById(mapCID) !== null){
		var mapStartCrop = strpos(mapCID,'_') + 1;
		var mID = Number(mapCID.substr(mapStartCrop));
		Netism.Map.Render(mID);
	}
}
