widthThreshold = 1250;

function url_remap() {
	var curUrl = document.location.href;
	var curUrlHash = document.location.hash;
	var newUrl = document.location.protocol + "//" + document.location.host + "/mbc/";
	
	var urlMap = 	[		["/mbc/morrisoncampus/parents.aspx", "#/morrisoncampus/parents"],
												["/mbc/morrisoncampus/multiply.aspx", "#/morrisoncampus/multiply"],
												["/mbc/morrisoncampus/afterhours.aspx", "#/morrisoncampus/afterhours"],
												["/mbc/morrisoncampus/superquick.aspx", "#/morrisoncampus/superquick"],
												["/mbc/morrisoncampus/springbreak.aspx", "#/morrisoncampus/springbreak"],
												["/mbc/morrisoncampus/carepackage.aspx", "#/morrisoncampus/carepackage"],
												["/mbc/morrisoncampus/atmguy.aspx", "#/morrisoncampus/atmguy"],
												["/mbc/ontheroad/farnoosheconomy.aspx","#/ontheroad/farnoosheconomy"],
												["/mbc/ontheroad/farnooshsavings.aspx","#/ontheroad/farnooshsavings"],
												["/mbc/ontheroad/farnooshcollege.aspx","#/ontheroad/farnooshcollege"],
												["/mbc/ontheroad/instantpricegame.aspx","#/ontheroad/instantpricegame"],
												["/mbc/ontheroad/instanttalentgame.aspx","#/ontheroad/instanttalentgame"],
												["/mbc/ontheroad/atmguyinlove.aspx","#/ontheroad/atmguyinlove"],
												["/mbc/ontheroad/bigatmoncampus.aspx","#/ontheroad/bigatmoncampus"],
												["/mbc/ontheroad/snowcones.aspx","#/ontheroad/snowcones"],
												["/mbc/themorriscode/","#/morriscode"]
										];
										
	if (curUrlHash == "") { //No SWF Address hash present
		for (urlItr = 0; urlItr < urlMap.length; urlItr++) {
			if (curUrl.indexOf(urlMap[urlItr][0]) != -1) { //match found
				return newUrl + urlMap[urlItr][1];
			}
		}
	} else { //SWF Address has found
		return escape(newUrl + curUrlHash)
	}
		
	return escape(curUrl);
}

function callShare(shareType,title) {
	switch (shareType) {
		case "digg":		launchCenter("http://digg.com/submit?phase=2&url="+url_remap()+"&title="+escape(title), "digg");
													break;
		case "delicious":	launchCenter("http://del.icio.us/post?url="+url_remap()+"&title="+escape(title), "delicious");
													break;  
		case "twitter": 	launchCenter("http://twitter.com/home?status="+url_remap(),"twitter");
													break;
		case "stumble":		launchCenter("http://www.stumbleupon.com/submit?url="+url_remap()+"&title="+escape(title), "stumble");
													break;
		case "facebook": 	launchCenter("http://www.facebook.com/sharer.php?u="+url_remap()+"&t="+escape(title), "facebook");
													break;
		case "myspace":		launchCenter("http://www.myspace.com/Modules/PostTo/Pages/?u="+url_remap()+"&t="+escape(title), "myspace");
													break;
		default:			alert('bad value');
													break;
	}
}

function launchCenter(url, name)
{
	  var winName = Math.round(9999*Math.random()) + new Date().getTime();
	  var winNew = window.open(url, name);

	  if(!winNew) {
			getSwf(swfId).openWindowFromSwf(url);
	  } else {
			winNew.focus();
	  }
}

function modLegal(content) {
	document.getElementById("pageLegal").innerHTML = content;
}

function showTranscript(xmlFile)
{
    fancyOpen("/mbc/transcript.aspx?file="+xmlFile, '_blank', 500, 600, 'scrollbars=yes,toolbar=no,location=no,menubar=no,resizable=yes')
}

var swfId = "mbc";

function fancyOpen(url, name, width, height, winstyle)
{
    var str = "height=" + height + ",innerHeight=" + height;
    str += ",width=" + width + ",innerWidth=" + width;  if (window.screen) {
    var ah = screen.availHeight - 30;    var aw = screen.availWidth - 10;
    var xc = (aw - width) / 2;    var yc = (ah - height) / 2;
    str += ",left=" + xc + ",screenX=" + xc;
    str += ",top=" + yc + ",screenY=" + yc;  }
	  var winName = Math.round(9999*Math.random()) + new Date().getTime();
	  var winNew = window.open(url, name, str + "," + winstyle);

	  if(!winNew) {
			getSwf(swfId).openWindowFromSwf(url);
	  } else {
			winNew.focus();
	  }
}


 function getSwf(id) {
	  if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[id];
	  } else {
			return document[id];
	  }
 }    


function swapImgRestore() { //v3.0
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function swapImage() { //v3.0
  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function checkSwffit() {
	if(getWidth() < widthThreshold ) {
		swffit.stopFit();
		swffit.configure({maxWid: 1000, maxHei: 599,minWid: 1000, minHei: 599});
		swffit.startFit();
		document.getElementById("footer").style.width="680px";
		document.getElementById("footer").style.paddingLeft="160px";
		document.getElementById("footer").style.paddingRight="160px";
	} else {
		swffit.stopFit();
		swffit.configure({maxWid: 1250, maxHei: 748, minWid: 1250, minHei: 748});
		swffit.startFit();
		document.getElementById("footer").style.width="850px";
		document.getElementById("footer").style.paddingLeft="200px";
		document.getElementById("footer").style.paddingRight="200px";
	}
}

function getWidth() {
 var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth  ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  
  return myWidth;
}

function getHeight() {
 var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  
  return myHeight;
}

function scrollTo(objId){
    try {
		 var obj = document.getElementById(objId);
        var objpos = ObjectPosition(obj);
    } catch(e) {}
    try {
        scroll(0,objpos[1]);
    } catch(e) {}
    try {
        window.scrollTo(0,objpos[1]);
    } catch(e) {}
}

function ObjectPosition(obj) {
    var curleft = 0;
      var curtop = 0;
      if (obj.offsetParent) {
            do {
                  curleft += obj.offsetLeft;
                  curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
      }
      return [curleft,curtop];
}

function getInitialHeight() {
	return (getWidth() < widthThreshold) ? 1000 : 1250;
}

function getInitialWidth() {
	return (getWidth() < widthThreshold) ? 599 : 748;	
}

function setCookie(c_name, c_value, c_path, c_expiredays, c_domain, c_secure){
	if(c_expiredays){
     var exdate=new Date();
     exdate.setDate(exdate.getDate()+c_expiredays);
	 }
	 
   document.cookie=c_name+ "=" +escape(c_value)+ 
   ((c_expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
 ( ( c_domain ) ? ";domain=" + c_domain : "" ) +
  ((c_path == null) ? "" : ";path=" + c_path)+
   ((c_secure==null) ? "" : ";secure");
   ;
}

//function to get cookie by name
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

// state selection form
function drawStateSelector() {
	var submitURL = "http://" + document.domain + "/mbc/img/submit_state.gif";
	var mbc = document.getElementById("mbc");
	// show div
	mbc.style.display = "";
	var selectorString = '<div id="mbc_state"><form><select id="stateSel" class="select_state"><option value="">Select a state</option>' +
		'<option value="AK">Alaska</option>' +
		'<option value="AL">Alabama</option>' +
		'<option value="AR">Arkansas</option>' +
		'<option value="AZ">Arizona</option>' +
		'<option value="CA">California</option>' +
		'<option value="CO">Colorado</option>' +
		'<option value="CT">Connecticut</option>' +
		'<option value="DC">District of Columbia</option>' +
		'<option value="DE">Delaware</option>' +
		'<option value="FL">Florida</option>' +
		'<option value="GA">Georgia</option>' +
		'<option value="HI">Hawaii</option>' +
		'<option value="IA">Iowa</option>' +
		'<option value="ID">Idaho</option>' +
		'<option value="IL">Illinois</option>' +
		'<option value="IN">Indiana</option>' +
		'<option value="KS">Kansas</option> '+
		'<option value="KY">Kentucky</option>' +
		'<option value="LA">Louisiana</option>' +
		'<option value="MA">Massachusetts</option>' +
		'<option value="MD">Maryland</option>' +
		'<option value="ME">Maine</option>' +
		'<option value="MI">Michigan</option>' +
		'<option value="MN">Minnesota</option>' +
		'<option value="MO">Missouri</option>' +
		'<option value="MS">Mississippi</option>' +
		'<option value="MT">Montana</option>' +
		'<option value="NC">North Carolina</option>' +
		'<option value="ND">North Dakota</option>' +
		'<option value="NE">Nebraska</option>' +
		'<option value="NH">New Hampshire</option>' +
		'<option value="NJ">New Jersey</option>' +
		'<option value="NM">New Mexico</option>' +
		'<option value="NV">Nevada</option>' +
		'<option value="NY">New York</option>' +
		'<option value="OH">Ohio</option>' +
		'<option value="OK">Oklahoma</option>' +
		'<option value="OR">Oregon</option>' +
		'<option value="PA">Pennsylvania</option>' +
		'<option value="PR">Puerto Rico</option>' +
		'<option value="RI">Rhode Island</option>' +
		'<option value="SC">South Carolina</option>' +
		'<option value="SD">South Dakota</option>' +
		'<option value="TN">Tennessee</option>' +
		'<option value="TX">Texas</option>' +
		'<option value="UT">Utah</option>' +
		'<option value="VA">Virginia</option>' +
		'<option value="VT">Vermont</option>' +
		'<option value="WA">Washington</option>' +
		'<option value="WI">Wisconsin</option>' +
		'<option value="WV">West Virginia</option>' +
		'<option value="WY">Wyoming</option></select>' +
		'<input type="image" name="select" onclick="setStateCookieSWF()" src="' + submitURL + '" width="73" height="35" class="submit_state"/></form></div>';
	mbc.innerHTML = selectorString;
}

function setStateCookieSWF() {
	var stateVal = document.getElementById('stateSel').value;
	if (stateVal.length != 0) {
		// set cookie (refreshes page)
		setStateCookie(stateVal);
	} else {
		// no state selected - redraw state selector
		drawStateSelector();
	}
}