// JavaScript Document


// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func) {
	//alert('adding '+func);
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
function addResizeEvent(func) {
	//alert('adding '+func);
	var oldonresize = window.onresize;
	if (typeof window.onresize != 'function'){
    	window.onresize = func;
	} else {
		window.onresize = function() {
			oldonresize();
			func();
		}
	}
}
addResizeEvent(page_size);
addLoadEvent(startRedirect);
addLoadEvent(page_size);
addLoadEvent(resetLinks);
// ------------------------
var f = document.location.href;
filestr = f.substr(f.lastIndexOf("/")+1);
if (filestr == "press.php") {
	addLoadEvent(init_slider);
}
// ------------------------
addLoadEvent(startList);
addLoadEvent(update_location);



// NB: using hash value for permanent links 
// ... lets us change content without refreshing the page and still keeping link correct for bookmarks
function startRedirect() {
	//alert("start redirect");
	if (document.location.hash.length > 0) {
		hashFilter = document.location.hash.split(".");
		//alert(hashFilter[0] + " / "+hashFilter[1]);
		// redirect
		redirect = hashFilter[0].slice(1)+".php";
		if (hashFilter[0].slice(1) == 'section' && hashFilter.length == 3) redirect = "show.php";
		if (hashFilter.length >= 2 && hashFilter[0].slice(1) == 'section') redirect += "?section="+hashFilter[1];
		if (hashFilter.length >= 2 && hashFilter[0].slice(1) == 'show') redirect += "?product="+hashFilter[1];
		//location.href = redirect;
		//alert(redirect);
		nextObj = new Object();
		pagehref = document.location.href;
		pageparts = pagehref.split("/");
		nextObj.href = "/normal/"+pageparts[4]+"/"+redirect;
		Effect.Fade('modules',{duration:0.5,afterFinish:loadNewPage});
	} else {
		// should be looking at the home page
		addLoadEvent(menu_open); // open the menu by default
	}
}
var pHeight;
function page_size() {
	if (self.innerHeight) {// all except Explorer
		pHeight = self.innerHeight - 20;
	} else if (document.documentElement && document.documentElement.clientHeight){ // Explorer 6 Strict Mode
		pHeight = document.documentElement.clientHeight;
	} else if (document.body) {// other Explorers
		pHeight = document.body.clientHeight;
	}
	$('stage').style.height = (pHeight-1);
	$('stage').setAttribute('style', 'height:'+(pHeight-1)+'px;');
}

function thumb_out(obj) {
	fademe = obj.firstChild;
	new Effect.Fade(fademe,{ from:1.0, to:0.5, duration:1.0});
}
function thumb_over(obj) {
	fademe = obj.firstChild;
	new Effect.Fade(fademe,{ from:0.5, to:1.0, duration:0.1});
}

function menu_open() {
	new Effect.Appear('sub_menu', {duration:0.3, limit:1, scope:'sub_menu',queue:'front'});
	pe = new PeriodicalExecuter(menu_checkClose, 2);
}
function menu_checkClose() {
	new Effect.Fade('sub_menu',{delay:1,duration:0.3, limit:1, scope:'sub_menu',queue:'front'});
	pe.stop();
}
function resetLinks() {
	//alert('resetting links');
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	//alert("found "+anchors.length+" links");
	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "fade")){
			anchor.onclick = function () { fadePageOut(this); return false;}
		}
	}
}
function fadePageOut(obj) {
	hrefstr = obj.getAttribute("href");
	filestr = hrefstr.substr(hrefstr.lastIndexOf("/")+1);
	re = /(\?|\.|\=)/;
	f = filestr.split(re);
	nextObj = new Object();
	nextObj.href = hrefstr;
	nextObj.page = f[0];
	nextObj.section = (f.length >= 7 && f[4] == 'section') ? f[6] : '';
	nextObj.product = (f.length >= 7 && f[4] == 'product') ? f[6] : '';
	Effect.Fade('modules',{duration:0.5,afterFinish:loadNewPage});
	rewrite_hash = "#"+nextObj.page;
	if (nextObj.section) rewrite_hash += "." + nextObj.section;
	if (nextObj.product) rewrite_hash += "." + nextObj.product;
	location.href = rewrite_hash;
	// clear section active states
	sections = document.getElementsByClassName("section");
	for (var i=0; i<sections.length; i++) {
		if (Element.hasClassName(sections[i],'active')) Element.removeClassName(sections[i],'active');
	}
	if (nextObj.section) Element.addClassName("menu_"+nextObj.section, "active");// set active section
	update_location();
}
function loadNewPage() {
	var url = nextObj.href;
	var params = 'page=part';
	var div = 'modules';
    var myAjax = new Ajax.Updater(div, url, { parameters: params, method: 'post',onComplete:fadePageIn});
	// switch page title
	var anchors = document.getElementsByTagName("title");
	page_title = anchors[0];
	new_title = 'new';
	Element.update(page_title,new_title);
	// urchin stats
	urchinTracker(nextObj.href);
}
function fadePageIn() {
	Effect.Appear('modules',{duration:0.5, afterFinish:checkSlider});
	resetLinks();
	//alert(nexturl);
}
function checkSlider() {
	if (nextObj.href == '/normal/en/press.php' || nextObj.href == '/normal/jp/press.php') init_slider();
}
function switchLang(str) {
	if (str.indexOf("/en/") >= 0) {
		return str.replace(/\/en\//,'/jp/');
	} else {
		return str.replace(/\/jp\//,'/en/');
	}
}

function init_slider() {
	//alert('start slider');
	// from the author's first demo of a vertical slider.
	var mySlider = new Control.Slider('press_slider','press_track',{ axis:'vertical'});
	// Setting the callbacks later on
	mySlider.options.onSlide = function(v) {
		h = Element.getHeight('press_inner') - 350;
		Element.setStyle('press_inner', { 'margin-top': (v*-h) + 'px'});
	}
}
function gallery_show_image(image_file) {
	html = '<img class="main" src="../img/upload/products/'+image_file+'" alt="">';
	Element.update('product_images',html); 	
}
function startList() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("info_menu");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}
function update_location() {
	Element.update('print_location',document.location.href);	
	// set lang switch
	switched_url = switchLang(document.location.href);
	switched_url = switched_url.slice(0,switched_url.lastIndexOf("/")+1) + document.location.hash;
	$('lang_switch').setAttribute('href',switched_url);
}
//

