// JavaScript Document

//console.debug("Starting");

var Preloader = {

}

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};
var Application = {
	currentSubNav: "none",
	currentTextSize: "size2",
	sn_top_Y: 135,
	sn_width: 160,
	_hasFlash: null,

	hasFlash: function(){
		if(this._hasFlash != null) return this._hasFlash;
		this._hasFlash = DetectFlashVer(8,0,0);
		return this._hasFlash;
	},

	switchSubNavBox: function(id){
		var newSubNav = "subnav"+id;
		var sn = document.getElementById('nav_secondary');
		for(i=1; i<=6; i++){
			var pn = document.getElementById('nav'+i);
			pn.className = "";
		}
		var pn = document.getElementById('nav'+id);
		pn.className = "active";
		
		var my_browser = navigator.appName;
		var b_version = navigator.appVersion;
		var version = parseFloat(b_version);
				
		if ( (my_browser=="Microsoft Internet Explorer") && (version<=4))
		{
			if (typeof document.body.style.maxHeight != "undefined") {
			// IE 7, mozilla, safari, opera 9
				this.sn_top_Y = 135;
			} else {
			// IE6, older browsers
				this.sn_top_Y = 140;
			}
			
		}else{
			this.sn_top_Y = 135;
		}
		
		sn.style.top = this.sn_top_Y+"px";
		if(id == 1)
			sn.style.left = "15px";
		if(id == 2)
			sn.style.left = "180px";
		if(id == 3)
			sn.style.left = "345px";
		if(id == 4)
			sn.style.left = "510px";
		if(id == 5)
			sn.style.left = "675px";
		if(id == 6)
			sn.style.left = "840px";
		if(newSubNav == this.currentSubNav) {
			
		}else{
			if(this.currentSubNav == "none"){
				//this.currentSubNav = ""
			}else{
				$(this.currentSubNav+'_box').hide();
			}
			this.currentSubNav = newSubNav;
			$(newSubNav+'_box').show();
		}
		
		$$('.shadow').each(function(item){					 
			item.show();
	  	});
		return false;
	},
	
	
	closeSubNavBox_byMouseOut: function(subNav){
		for(i=1; i<=6; i++){
			var pn = document.getElementById('nav'+i);
			pn.className = "";
		}
		$(subNav+'').hide();
		this.currentSubNav = "none";
		$$('div#nav_secondary .shadow').each(function(item){					 
			item.hide();
	  	});
	},
	
	closeSubNavBox_byClick: function(subNav){
		for(i=1; i<=6; i++){
			var pn = document.getElementById('nav'+i);
			pn.className = "";
		}
		$(subNav+'_box').hide();
		this.currentSubNav = "none";
		$$('div#nav_secondary .shadow').each(function(item){					 
			item.hide();
	  	});
	},

	changeTextSize: function(size){
		Cookie.set("textSize",size,356);
		if(size == this.currentTextSize){
			//alert("same size");
		}else{
			var oldBtn = document.getElementById(this.currentTextSize);
			oldBtn.className = "";
			var newBtn = document.getElementById(size);
			var wrapper = document.getElementById('page');
			newBtn.className = "active";
			if(size == "size1"){
				wrapper.className = "size1";
			}
			if(size == "size2"){
				wrapper.className = "size2";
			}
			if(size == "size3"){
				wrapper.className = "size3";
			}
			this.currentTextSize = size;
		}
		
	}

	
}

//console.debug("Flash Link");

Event.addBehavior({
  'a.flash_link:click' : function(e) {
		var link = this.href.substring(this.href.indexOf("overview/")+9).replace(/\/$/,"")
		if(!Application.hasFlash()) return; 
	 	if(thisMovie("main_flash")){
			loadContent(link);
			try {
				e.target.blur();
			} catch(e){}
			Application.closeSubNavBox_byMouseOut(Application.currentSubNav+"_box");
			return false;
		} else {
			this.href=Application.relative_root+"#"+link;
		}
  }
});
	
 function loadContent(page) {
     thisMovie("main_flash").loadContent(page);
 }

 function thisMovie(movieName) {
     if (navigator.appName.indexOf("Microsoft") != -1) {
         return window[movieName]
     }
     else {
         return document[movieName]
     }
 }


//console.debug("Main nav");

RowHighlight = Behavior.create({
  initialize: function() {
  },

 onmouseover : function() {
		this.getRowGroupMembers().each(function(el){
			$(el).addClassName("highlight_row");
		});
  },

  onmouseout : function() {
		this.getRowGroupMembers().each(function(el){
			$(el).removeClassName("highlight_row");
		});
  },

	getRowGroupMembers : function() {
		var rowGroup = this.getRowGroup();
		return rowGroup != -1 ? $(this.element.parentNode).select("tr."+rowGroup) : [this.element];
	},
	
	getRowGroup : function() {
		if(this.rowGroup != undefined) 
			return this.rowGroup;
		this.rowGroup = -1;
		for(var i=0; i<10; i++){
			var rowGroup = "row_group_"+i
			if(this.element.hasClassName(rowGroup)){
				this.rowGroup = rowGroup;
				break;
			}
		}
		return this.rowGroup;
	}

});


Event.addBehavior({
  '#nav_primary a.mainnav_btn:mouseover' : function(e) {
	 var id = this.id.split("_")[1]; 
	 Application.switchSubNavBox(id);
	 e.cancel = true;
	 return false;
  }
});


Event.addBehavior({
  '#nav_primary a.mainnav_btn:mouseout' : function(e) {
	var id = this.id.split("_")[1]; 
	ev = e;
	var yMouse = ev.pageY;
	var yScroll = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	if(ev.pageY+yScroll < Application.sn_top_Y){
	 	Application.closeSubNavBox_byMouseOut(Application.currentSubNav+"_box");
	}else if(ev.clientY+yScroll < Application.sn_top_Y){
		Application.closeSubNavBox_byMouseOut(Application.currentSubNav+"_box");
	}
	e.cancel = true;
	 return false;
  }
});

//console.debug("Td highlight");

// Event.addBehavior({
//   'tr.data:mouseover' : function() {
// 		$(this).addClassName("highlight_row");
// 	 	//$(this).ancestors()[0].addClassName("highlight_row");
//   }
// });
// 
// Event.addBehavior({
//   'tr.data:mouseout' : function() {
// 		$(this).removeClassName("highlight_row");
// 	 	//$(this).ancestors()[0].removeClassName("highlight_row");
//   }
// });

// Event.addBehavior({
//   '#nav_primary a.mainnav_btn:click' : function(e) {
// 	//********Fix for Safari***********
// 	var newTarget = this.href.split("#")[1];
// 	var patt1 = new RegExp('%23');
// 	if( patt1.test(newTarget)){
// 		//for safari
// 		newTarget = newTarget.split("%23")[1];
// 	}else{
// 		//for firefox,ie
// 		newTarget = this.href.split("#")[2];
// 	}
// 	//********Fix for Safari***********
// 	document.location = newTarget;
// 	e.cancel = true;
// 	return false;
//   }
// });

//console.debug("secondary nav");

Event.addBehavior({
  '#nav_secondary ul.subnav_box:mouseout' : function(e) {
	var subnav = this.id.split("#");
	var wp_X = document.getElementById('wrapper').offsetLeft;
	var sn_height = document.getElementById('nav_secondary').offsetHeight;
	var sn_bottom_Y = sn_height + Application.sn_top_Y - 1;
	var sn_left_X = document.getElementById('nav_secondary').offsetLeft + wp_X; 
	if(subnav == "subnav5_box"){
		var sn_right_X = sn_left_X + (Application.sn_width*2+5) - 1;
		//alert("nav5 = "+sn_right_X);
	}else{
		var sn_right_X = sn_left_X + Application.sn_width - 1; 	
		//alert("nav..." +sn_right_X);
	}
	ev = e;
	var xMouse = ev.pageX;
	var yMouse = ev.pageY;
	var yScroll = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	//alert("yScroll="+yScroll);
	if(ev.pageX < sn_left_X || ev.pageX > sn_right_X || ev.pageY+yScroll > sn_bottom_Y+yScroll){
		//alert(ev.pageY+"/"+yScroll+"/"+sn_bottom_Y);
	 	Application.closeSubNavBox_byMouseOut(subnav);
	}else if(ev.clientX < sn_left_X || ev.clientX > sn_right_X || ev.clientY+yScroll > sn_bottom_Y){
		//alert(ev.clientY+"*"+yScroll+"*"+sn_bottom_Y);
		Application.closeSubNavBox_byMouseOut(subnav);
	}
	e.cancel = true;
	return false;
  }
});

//console.debug("text size");

Event.addBehavior({
  '#text_size_selector a:click' : function(e) {
	 var newSize = this.href.split("#")[1];
	 //alert("newSize:"+newSize);
	 Application.changeTextSize(newSize);
	 e.cancel = true;
	 return false;
  }
});

Event.addBehavior({
  'tr.data': RowHighlight
});


Event.onReady(function() {
	
	
	//console.debug("On ready");
	
	$$('.subnav_box').each(function(item){					 
		item.hide();
  	});
	$$('.shadow').each(function(item){					 
		item.hide();
  	});
	document.body.style.display = 'block';
	var btn = document.getElementById('size2');
	btn.className = "active";
	var textSize = Cookie.get("textSize");
	////console.debug(textSize);
	if(textSize) {
		Application.changeTextSize(textSize);
	}
	
	// After 1s try to jump to flash content.
	window.setTimeout(function(){
		if(thisMovie("main_flash")){
			try {
			loadContent(document.location.href.split("#")[1]);
			} catch(e){
				//alert(e.toString());
			}
		} else {
			// nothing
		}
	}, 1000);
	

	
	
	
	//console.debug("After On ready");
	
	
});



