/*
Title:      Main JavaScript	
Author:     Cristian Deschamps, cristian.deschamps@rocler.com

*/

// ------[ IE6 Cache Control (remove flicker on mouseover) ]------------------------------------------------- //	
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

// ------[ Master Onload for all page. ]------------------------------------------------- //	
window.JFSports = window.JFSports || {};

JFSports = {	
	Init : function() {									

	}
};
//YAHOO.util.Event.onDOMReady(JFSports.Init, JFSports, true);

JFSports.Gallery = {
	images : null,
	current : 0,
	
	Show : function(dir) {
		
		x = this.current+dir;
		if(x<0) x = this.images.length-2;
		else if(x>=this.images.length-1) x = 0;
		
		//this.images
		img = document.getElementById('image-large');
		img.src = this.images[x];
		this.current = x;		
	}
}

JFSports.Email = {
	Init: function() {
		form = document.getElementById('sign-up-form');
		email = JFSports.GetUrlParam('email');
		if(email == 'registered') {
			form.innerHTML = "<p>Thanks for registering</p>";
		
		} else if(email == 'failed') {
			YAHOO.util.Dom.insertBefore("<p>Registration failed</p>", "Form_Form");
		}		
	}
}

JFSports.Products = {
	productPanel : null,
	products : null,
	
	Init : function() {

		var productPanelConf = new Array();
		productPanelConf.id = "yuiLoadingPanel1";
		productPanelConf.header = "";
		productPanelConf.width = "700px";
		productPanelConf.height = "526px";
		this.productPanel = new yuiLoadingPanel(productPanelConf);
		

		this.products = YAHOO.util.Dom.getElementsByClassName('product', 'div', 'content');
		
		for(x=0; x<this.products.length; x++) {
			var div = this.products[x];
			var a = div.getElementsByTagName('a')[0].href;				
			div.href = a;
			
			YAHOO.util.Event.addListener(div, "click", this.Open); 				
		}
	
	},
	
	Open : function(ev, href) {
		YAHOO.util.Event.preventDefault(ev);

		href = (this.href) ? this.href : href;
		
		
		JFSports.Products.productPanel.show("<div id='product-detail'><img class='loading' src='" + JFSports.Path + "/js/yui/assets/rel_interstitial_loading.gif' /></div>");
		var args = "";
		var responseSuccess = function(o) {			
			/* Please see the Success Case section for more
			 * details on the response object's properties.
			 * o.tId
			 * o.status
			 * o.statusText
			 * o.getResponseHeader[ ]
			 * o.getAllResponseHeaders
			 * o.responseText
			 * o.responseXML
			 * o.argument
			 */
			productDetail = document.getElementById('product-detail');
			productDetail.innerHTML = o.responseText;			
			var x = productDetail.getElementsByTagName("script");   
			for(var i=0;i<x.length;i++) {  
				eval(x[i].text);  
			}  			
			
		};
			
		var responseFailure = function(o) {
		// Access the response object's properties in the
		// same manner as listed in responseSuccess( ).
		// Please see the Failure Case section and
		// Communication Error sub-section for more details on the
		// response object's properties.			
			alert('faild to load product');
		};
		
		var callback = {
		  success : responseSuccess,
		  failure : responseFailure,
		  argument : args
		};
		
		var transaction = YAHOO.util.Connect.asyncRequest('GET', href, callback, null); 	
		return false;
	}
}

JFSports.Browser = {
	panels : null,
	CurrentPanel : null,		
	zindex : 1000,
	
	Init : function() {
		this.panels = YAHOO.util.Dom.getElementsByClassName('panel', 'div', 'browser');

		for(x=0; x<this.panels.length; x++) {
			var div = this.panels[x];
			
			div.panel = YAHOO.util.Dom.getElementsByClassName('panel-over', 'div', div)[0];
			div.panelContent = YAHOO.util.Dom.getElementsByClassName('panel-content', 'div', div)[0];
			
			YAHOO.util.Event.addListener(div, "mouseover", JFSports.Browser.MouseOver); 	
			YAHOO.util.Event.addListener(div, "mouseout", JFSports.Browser.MouseOut); 	
			YAHOO.util.Event.addListener(div, "click", JFSports.Browser.Open); 
			
			//'<a class="panel-close"><span>close</span></a>';
			btclose = document.createElement('a');
			btclose.className = "panel-close";
			btcloseSpan = document.createElement('span');
			btcloseSpan.innerHTML = "close";
			
			btclose.appendChild(btcloseSpan);	
			div.panelClose = div.panelContent.appendChild(btclose);	
		}		
	},
	
	MouseOver : function(e) {
		YAHOO.util.Dom.addClass(this, 'open');
	},
	
	MouseOut : function(e) {		
		YAHOO.util.Dom.replaceClass(this, 'open', ' ');
	},
	
	Open : function(e) {							
		YAHOO.util.Event.addListener(this.panel, "mouseout", JFSports.Browser.Close, this, true);	
		YAHOO.util.Event.addListener(this.panelClose, "click", JFSports.Browser.Close, this, true);
		
		YAHOO.util.Event.removeListener(this, "mouseover", JFSports.Browser.MouseOver); 
		YAHOO.util.Event.removeListener(this, "mouseout", JFSports.Browser.MouseOut); 
		YAHOO.util.Event.removeListener(this, "click", JFSports.Browser.Open); 		
		
		JFSports.Browser.zindex = JFSports.Browser.zindex + 1;
		YAHOO.util.Dom.setStyle(this.panel, 'cursor', 'auto');	
		YAHOO.util.Dom.setStyle(this.panel, 'zIndex', JFSports.Browser.zindex);
		
		var attributes = { width: {"to": 318 } };
		anim = new YAHOO.util.Motion(this.panel, attributes, 0.4, YAHOO.util.Easing.easeIn);
		anim.animate();													
	},
	
	Close : function(e) {								
		var elRelatedTarget = YAHOO.util.Event.getRelatedTarget(e);		
		var isAncestor = YAHOO.util.Dom.isAncestor(this, elRelatedTarget);

		if(!isAncestor) {						
			YAHOO.util.Event.removeListener(this.panel, "mouseout", JFSports.Browser.Close); 
			
			var currentPanel = this;
			var attributes = { width: {"to": 123 } };
			var anim = new YAHOO.util.Motion(this.panel, attributes, 0.4, YAHOO.util.Easing.easeOut);
			anim.onComplete.subscribe(function() {
				YAHOO.util.Event.addListener(currentPanel, "mouseover", JFSports.Browser.MouseOver); 	
				YAHOO.util.Event.addListener(currentPanel, "mouseout", JFSports.Browser.MouseOut); 	
				YAHOO.util.Event.addListener(currentPanel, "click", JFSports.Browser.Open); 					
					
				YAHOO.util.Dom.replaceClass(currentPanel, 'open', ' ');
				YAHOO.util.Dom.setStyle(currentPanel.panel, 'cursor', 'pointer');	
				JFSports.Browser.anim = null;
			});
			anim.animate();									
		}
	}
}


// ------[ Ajax Loading Panel ]------------------------------------------------- //
$D = YAHOO.util.Dom;
$E = YAHOO.util.Event;
var yuiLoadingPanel = function(conf){    
	conf = (conf == undefined) ? new Array() : conf;
    conf.id = (conf.id == undefined) ? 'yuiLoadingPanel' : conf.id;    	
	conf.header = (conf.header == undefined) ? 'Processing, please wait...' : conf.header;
    conf.width = (conf.width == undefined) ? '240px' : conf.width;
	conf.close = (conf.close == undefined) ? 'false' : 'true';
    this.conf = conf;
    this.cancelEvent = new YAHOO.util.CustomEvent("cancelEvent", this);
    this.init();
	
	
};


yuiLoadingPanel.prototype = {
    width : null,
	height : null,
	close : null,
	
	init : function() {
        var loadingPanel = new YAHOO.widget.Panel(this.conf.id,{
            width : this.conf.width,
			fixedcenter : true,
            close : this.conf.close,
            draggable : false,
            modal : true,
            visible : false,
			zIndex : 100000
        });

       	loadingPanel.setBody(this.conf.header + '<img src=' + JFSports.Path + '/js/yui/assets/rel_interstitial_loading.gif" />');
      	loadingPanel.render(document.body);
       $D.addClass(loadingPanel.id, 'tcc_lightboxLoader');

	   $D.setStyle(loadingPanel.body, 'text-align', 'center');
	   $D.addClass(document.body, 'yui-skin-sam');
       this.loadingPanel = loadingPanel;
    },
	
    show : function(html) {		
		if(html != undefined) {
           	this.loadingPanel.setBody(html);
        } 
		//this.loadingPanel.render(document.body);
		this.loadingPanel.show();
    },
    
	hide : function() {
        this.loadingPanel.hide();
    }
};

// ------[ Popup Window ]------------------------------------------------- //
JFSports.PopWindow = function(url,w,h,scroll,tools,name,center) {
	var str = "height=" + h + ",innerHeight=" + h;
	str += ",width=" + w + ",innerWidth=" + w;
	if(!center) var center = false;
	if(!scroll) scroll = 0;
	if(!tools) tools = 0;
	if(!name) name = "pop";
	if((window.screen) && (center)) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - w) / 2;
		var yc = (ah - h) / 2;
		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
		}
	pop = window.open(url,name,'toolbar=' + tools + ',location=0,directories=0,status=0,menubar=0,scrollbars=' + scroll + ',resizable=1,' + str).focus();
};

// ------[ Popup Window ]------------------------------------------------- //
JFSports.GetUrlParam = function(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

