/*----------------------------------*/
/*GENERAL SETTINGS & ANIMATION-----*/
/*--------------------------------*/

JAME.Components.TreeMenu.prototype.initPageLoadBreadCrumb = function() {
	return false;
}

JAME.Components.TreeMenu.prototype.handleMainMenuClickEvent = function() {

		var self = this;

		this.mainMenu.onclick  = function(e) {

			//normalize the event
			e = JAME.Events.Normalize(e);
			e.preventDefault();
			e.stopPropagation();

			var target = e.target;
			//check what triggered the event and get the li tag
			if(target.tagName.toLowerCase()==='a') target=target.parentNode;
			if(target.tagName.toLowerCase()!=='li') return;

			if(self.closingTimer) clearTimeout(self.closingTimer);

			self.dispatch('onClick',target);

			if(self.expandedLI) {
				var hostname   = window.location.protocol+'//'+window.location.hostname+'/';

				var clickedURL = JAME.DOM.firstNode(target).href;
				if(clickedURL.indexOf(hostname)==-1)
					clickedURL=hostname+clickedURL;

				var seenURL = hostname+window.location.hash.replace('#/','');
				if(self.expandedLI===target && self.mainMenu.isOpened) {
					self.expandedLI=undefined;
					return self.closeMainMenu();
				}

				if(seenURL==clickedURL && self.expandedLI===target && self.mainMenu.isOpened){
					 return self.closeMainMenu();
				}
			}

			if(target.parentNode===self.root && target.getElementsByTagName('ol')[0]){
				 self.openMainMenu();
			}

			if(self.expandedLI && self.expandedLI.parentNode!==self.root) {

				var ancestors = self.getAncestorTree(self.expandedLI);
				var link=JAME.DOM.firstNode(ancestors[ancestors.length-2].parentNode).href;

				if(link && JAME.DOM.firstNode(target).href===link) {
					return;
				}
			}

			self.hideExpandedTree(target);

			self.expandedLI=target;

			JAME.CSS.setStyles(JAME.DOM.firstNode(target),{color:this.onColor});

			//try to get the target submenu
			var submenu = target.getElementsByTagName('ol')[0];

			self.onClick(target,submenu);
			//if  we have a submenu
			if(submenu) {
				self.moveDot(getElementTop(self.expandedLI),getElementLeft(self.expandedLI));
				var OLLiChildren = self.hideLiElements(submenu);
				//display the submenu
				self.displayOLElement(submenu);
				self.showLiElements(OLLiChildren);
				return;
			}
			//close the menu again in 3.5s
			self.closingTimer = setTimeout(function() {
				self.closeMainMenu();
			},1000);
			//set the url address in the bar
			SWFAddress.setValue(window.Value(e.target.href)[1]);
		}
}



//this event fires just before we make the ajax call
//here you can modify the current displayed content
//like fade out elements,move them,etc.

JAME.ASYNC.Events.addListener('onFullLoad',function() {

	var string=location.href;
	if(string.match(/\/snowboard\/(#)?$/)){
		new JAME.Ajax({
			url:'/snowboard/?ajax=true&output=raw',
			onSuccess:function(resp){
				JAME.$('website').innerHTML=resp;
				createTopFeatureAnimations();
			}
		});
	}
});
var createTopFeatureAnimations = function() {

	if(!window.topFeatures){
		var panels = parse('#features li');

		window.topFeatures = new JAME.Components.Panels({
			container:JAME.$('top-features-area'),
			panels   :parse('#features li'),
			navigationOff:false,
			customId:'feature-panels',
			padding:20,
			to:'top'
		});
	}
	if(window.topFeatures)
	//window.topFeatures.startSlideShow(6000);

	window.crossFader = null;
	window.crossFader = new JAME.Components.CrossFader({imgs:parse('#pic img')});
}

JAME.ASYNC.Events.addListener('onFullLoad',function() {

	if(!JAME.$('top-news')) {

		if(window.crossFader){
			window.crossFader.stop();
			window.crossFader = null;
		}
		if(window.topFeatures) {
			//window.topFeatures.stopSlideShow();
			window.topFeatures = null;
		}
	}
});


//EVENTS ARE WRITTEN IN DISPATCHING ORDER

//onload fading and menu settings

window.getDepth = function(elm,root) {
		var	depth=0;
		do {
			if(elm===root) return depth;
			elm=elm.parentNode;
			depth++;
		}while(elm);
}

window.getPosition = function(elm,root) {

		var elms = root.getElementsByTagName('li');
		var ln   = elms.length;

		if(JAME.DOM.lastNode(root)  === elm) return ln;
		if(JAME.DOM.firstNode(root) === elm) return 1;
		for(var i=1;i<ln-1;i++)
			if(elms[i]===elm) return i+1;

			
}

	function jparent(elm) {
		return (elm) ? elm.parentNode : elm ;
	}

	function jcheckParent(target,origin) {
		do {
			if(target===origin) return true;
			target = jparent(target); 
		} while(target);
		return false;
	}


var loaderBar=function(){
	if(JAME.ASYNC._gnlCounter && JAME.ASYNC._gnlCounter>0){

		if(JAME.$('loader-container')) new JAME.FX({duration:10}).Tween(JAME.$('progressBar'),{width:[12]});
	}

}

JAME.ASYNC.Events.addListener('onLoadProgress',loaderBar);
JAME.ASYNC.Events.addListener('onFullLoad',function() {JAME.ASYNC._gnlCounter++;});

JAME.ASYNC.Events.addListener('onFullLoad',function() {

	document.body.style.marginTop='0px';
	var menu = JAME.$('globalmenu');
	var uri = SWFAddress.getValue();

	/*==MENU LOGIC==*/

	window.TreeMenu = new JAME.Components.TreeMenu({
		startingDepth: 2,
	    mainMenu     : JAME.$('globalmenu'),
	    breadCrumb	 : JAME.$('breadCrumb'),
	    root		 : parse('#globalmenu ol.tree'),
		offColor	 : '',
		isDot	     : false,
		closedHeight : 53,
		openedHeight : 160

	});
	TreeMenu.closeMainMenu();

	JAME.ASYNC.Events.removeListener('onFullLoad',arguments.callee);

	var searchli = JAME.$('searchproduct');
	searchli.onsubmit = function(e) {

		e  = JAME.Events.Normalize(e);
		e.preventDefault();

		var form        = e.target;
		var queryString = JAME.Form.serialize(form);
		var url         = form.action+'?'+queryString;
		var URLElements = Value(url);
		SWFAddress.setValue(URLElements[1]);

		queryString = null;
		form        = null;
		queryString = null;
		URLElements = null;
	}

});

/*-------------------*/
/*GOODIES & VIDEO---*/
/*-----------------*/

JAME.ASYNC.Events.addListener('onFullLoad',function() {
	if(! /goodies\/Video/.test(SWFAddress.getValue()) ) return;

	var links = parse("#html_container a.lightwindow");

	links = links && links.constructor === Array?links : links ? [links]:[];
	links.each(function(elm) {
			if(!elm) return;
			elm.onclick=function(e) {
				e = JAME.Events.Normalize(e);
				var elm = e.target;
				e.preventDefault();
				var myURI = elm.href;
				if(!myURI || elm.nodeName.toLowerCase()=='img') {
					myURI = elm.parentNode.href;
					elm.title = elm.alt;
				}
				open_lightwindow(myURI,elm.title);
			}
	});
	JAME.ASYNC.stack.push(links);
});

function open_lightwindow(myURI,myTitle) {

	var effect  = JAME.$('html_contents2').state ? {}:{position:['relative','relative'],left:[0,450],width:[880,400]};
	var divs  = parse('.vgitem_left');
	divs = divs && divs.constructor===Array ? divs : divs ? [divs] :[];
	var divs2 = parse('.vgitem_right');
	divs2 = divs2 && divs2.constructor===Array ? divs2 : divs2 ? [divs2] :[];
	if(!JAME.$('html_contents2').state){
	new JAME.FX({duration:10}).Tween(divs,{width:[430,160],overflow:['visible','hidden']});
	new JAME.FX({duration:10}).Tween(divs2,{width:[430,160],overflow:['visible','hidden']});

	divs.each(function(elm,i) {
			elm.onmouseover=function() {
				JAME.$('displayBox').innerHTML=elm.innerHTML;
				JAME.$('displayBox').getElementsByTagName('div')[0].style.display='none';
				JAME.$('displayBox').getElementsByTagName('a')[0].parentNode.style.display='none';
				new JAME.FX().Tween(JAME.$('displayBox'),{display:['block'],opacity:[1],left:[getElementLeft(elm)-100],top:[600]})
			}
	})
	divs2.each(function(elm,i) {
			elm.onmouseover=function() {
				JAME.$('displayBox').innerHTML=elm.innerHTML;
				JAME.$('displayBox').getElementsByTagName('div')[0].style.display='none';
				JAME.$('displayBox').getElementsByTagName('a')[0].parentNode.style.display='none';
				new JAME.FX().Tween(JAME.$('displayBox'),{display:['block'],opacity:[1],left:[getElementLeft(elm)-100],top:[600]})
			}
	})
	}
	new JAME.FX({duration:200}).Tween(JAME.$('html_contents2'),effect).queue(function() {
		var me = this;
		JAME.$('html_contents2').state=true;
		myURI= myURI.replace('.xhtml','.flv');
		myURI= myURI.replace('/snowboard/goodies/Video/','/images/video/movies/');

		if(!JAME.$('flash-container')) {
			var div = JAME.DOM.createNode('div');
			div.id="flash-container";
			JAME.$('website').appendChild(div);
			JAME.CSS.setStyles(div,{
				position:'absolute',
				top:80,
				left:40,
				height:348,
				width:360,
				backgroundColor:'black',
				color:'white',
				border:'3px solid white'
			});
			var close     = JAME.DOM.createNode('div');
			close.innerHTML = "CLOSE";
			JAME.CSS.setStyles(close,{cursor:'pointer'});
			close.onclick = function(e) {
				JAME.$('html_contents2').state=false;
				JAME.CSS.setStyles(div,{display:'none'});
				new JAME.FX({duration:10}).Tween(divs,{width:[160,400],overflow:['visible','hidden']});
				new JAME.FX({duration:10}).Tween(divs2,{width:[160,400],overflow:['visible','hidden']});
				divs.each(function(elm,i) {
						elm.onmouseover=null;
				})
				divs2.each(function(elm,i) {
					elm.onmouseover=null;
				})
				new JAME.FX().Tween(JAME.$('displayBox'),{display:['none'],opacity:[0]})
				me.reverse();
			}
			JAME.CSS.setStyles(close,{float:'right',marginRight:3});
			div.appendChild(close);
			var title   = JAME.DOM.createNode('h2');
			    title.id= "tv-title";
			JAME.CSS.setStyles(title,{float:'left',color:'white',padding:'5px',fontSize:14});
			div.appendChild(title);
		}
		else if(JAME.$('youtube_frame')) {
      JAME.$("flash-container").removeChild(JAME.$("flash"));
    } else {
			swfobject.removeSWF("flash");
		}
	 	JAME.CSS.setStyles(JAME.$('flash-container'),{display:'block'});

		JAME.$('tv-title').innerHTML=myTitle;

		var flash=JAME.DOM.createNode('div');
		flash.id="flash";
		JAME.$('flash-container').appendChild(flash);

    if(myURI.match('youtube')) {
      var youtube_frame = JAME.DOM.createNode('iframe');
      youtube_frame.id = 'youtube_frame';
      youtube_frame.src = myURI;
      youtube_frame.width = 360;
      youtube_frame.height = 318;
      JAME.CSS.setStyles(youtube_frame,{border:'none'});
      flash.appendChild(youtube_frame);
    } else {
      var swf ='/snowboard/local/swf/flv_player.swf?vid=';
      swf+=myURI;
      var flashvars = {'vid':myURI};
      var params = {};
      var attributes = {};
      swfobject.embedSWF(swf, "flash", "360", "320", "9.0.0","expressInstall.swf", flashvars, params, attributes);
    }

	});
}

/*---------------------*/
/*=TEAM BLOGS VIEW----*/
/*-------------------*/
/*
JAME.ASYNC.Events.addListener('onFullLoad',function() {

	if(! /(?:athletes\/RidersBlog)/.test(SWFAddress.getValue()) ) return;

	var riders = parse('#html_contents div.riders');

	riders.each(function(rider,i) {
		new JAME.FX({duration:450}).Tween(rider,{
				opacity:[0,.9]
		})
		if(!rider.id) return;

		var div       = JAME.DOM.createNode('span');	
		div.innerHTML = 'loading...';
		div.className = 'riderRSSFeed';
		rider.appendChild(div);

	
		setTimeout(function() {

		new JAME.Ajax({
			url:'/cgi-bin/atomic/snowboard/index.cgi?view=athletes.GetRiderRSS&output=raw&pk='+rider.id,
			onSuccess: function(resp) {
				div.innerHTML = '<h2>LATEST ENTRIES</h2>'+resp;
				var toggle    = div.getElementsByTagName('span')[0];
				var feed      = div.getElementsByTagName('ul')[0];
				var height    = JAME.CSS.getStyle(feed,'height');


			},
			onError:function() {
				div.innerHTML='';
			},
			timeout:20000,
			retryLimit:1
		});
		},300*i);
		    
	});
});
*/

//onload fading and menu settings
JAME.ASYNC.Events.addListener('onFullLoad',function() {
	if( !/info\/NewsTop.html/.test(SWFAddress.getValue())) return;

	var years = parse("div#years a");

	var list = parse('#news_list ol');
	var current=0;
	var maxHeight = 0;

	if(list.constructor == Array) {
		JAME.CSS.setStyles(JAME.$('news_list'),{display:'block',visibility:'hidden'});
		list.each(function(elm,i) {
   	     if(i==0) return;
			var height = parseInt(JAME.CSS.getStyle(elm,'height'));
     	   JAME.CSS.setStyles(elm,{opacity:0});
		    if (height > maxHeight) { maxHeight = height; }
		});
		years.each(function(elm,i) {
  			elm.onclick = function(e) {
     			e = JAME.Events.Normalize(e);
     			e.stopPropagation();
     			e.preventDefault();

				new JAME.FX({duration:100}).Tween(e.target,{backgroundColor:['#000000','#CC0000']});
				new JAME.FX({duration:100}).Tween(years[current],{backgroundColor:['#CC0000','#000000']});
     			var id = e.target.innerHTML;
	
     			var t  = current;
     			new JAME.FX({duration:200}).Tween(list[t],{opacity:[0]}).queue(function() {
        			JAME.CSS.setStyles(list[t],{display:'none'});
        			JAME.CSS.setStyles(JAME.$(id),{top: 0, display:'block'});
       				new JAME.FX().Tween(JAME.$(id),{opacity:[1]});
     			});
      			current=i;
  			}
		})
	}

	JAME.CSS.setStyles(JAME.$('news_list'),{height: maxHeight,display:'block',visibility:'visible'});
	JAME.CSS.setStyles(JAME.$('website'),{height: maxHeight+150});

});

/*=HOME CONTENTS */

JAME.ASYNC.Events.addListener('onFullLoad',function() {
	if(! /(?:home)/.test(SWFAddress.getValue()) ) return;
	if (window.TreeMenu) {
		window.TreeMenu.closeMainMenu();
	}

});


JAME.ASYNC.Events.addListener('onFullLoad',function() {
	if(! /Technologies/.test(SWFAddress.getValue()) ) return;

	parse('#html_contents div.technology').style.height='500px';

});


