
//Basic powderroom javascript.

(function() {
//begin	
$pr = YAHOO.namespace('Champion');

var YE = YAHOO.util.Event;
var YD = YAHOO.util.Dom;
var YL = YAHOO.lang;

$pr.clickHandler = function (e) {

	var namesAndIds = {
										 'toggler_ids': { //Use these to build ids of the elements to toggle. 
																			'view_product_toggle'	: 'product'
																		}
										}

	var elTarget = YE.getTarget(e); //get the target of the click

	//For glossary links hovers
	switch(true) {
  	case(YD.hasClass(elTarget, 'noLink')):
  	  YE.stopEvent(e);
      return;
		break;
	}
	
	if(elTarget.nodeName.toUpperCase() == "A") { //Is it a link that was clicked?
//@debug
//YAHOO.util.Event.stopEvent(e);

		var passThru = true;

		var command = elTarget.id.split('-')[1];
		
		//Set up open in new window targets, and for PDFs
		var href = window.location.hostname; //Current site

		re = new RegExp('\\b' + href + '\\b', "i");

		if (elTarget.href.search(re) == -1) { //if www.foo.com is not found, add blank to the target
			//@TODO - Fix Leaving Alert
			alert('LEAVING ALERT');
 			elTarget.target = "_blank";

 		} else if(elTarget.pathname.toLowerCase().lastIndexOf('.pdf') != -1){ //Checking for PDF
 			elTarget.target = "_blank";
 		}

		switch(true) {
			case('toggler' == command ):

				passThru = false;
				
				var target = elTarget.id.split('-')[2];
				var id = elTarget.id.split('-')[3] || '';
				
				var togId = target+id;
				var elToggle = YD.get("toggler_"+togId)			

				var curVisibility = YD.getStyle(elToggle,'visibility'); 
				
				if( 'visible' == curVisibility ) {
					//hide it, no action necessary, regular 
				} else {
					//hide any others open. may require ID with Target
					for(name in namesAndIds.toggler_ids) {
						if((namesAndIds.toggler_ids[name]+id) != togId) {
							//To hide other on restroom rating page. so only do it is el exists
							var tohide = YD.get("toggler_"+(namesAndIds.toggler_ids[name]+id) );
							if(tohide){
								$pr.toggler(tohide, {'hide':true});								
							}
						}
					}
				}

				$pr.toggler(elToggle)
				
			break;
			case('rate' == command):
				passThru = false;

				var restRoomID = elTarget.id.split('-')[2];
				//When we need to pass in an ID, for example, for the editUser form, will be [3]
				var criteriaID = elTarget.id.split('-')[3];
				var rateVal = elTarget.id.split('-')[4];

				var hidden_val_input = YD.get('data[criteria-'+restRoomID+'-'+criteriaID+']');

				var oldStarClass = 'star'+hidden_val_input.value;

				//Set the new value
				hidden_val_input.value = rateVal;
				
				//get the UL for the ratigs:
				var ul = YD.get('ul-ratings-'+restRoomID+'-'+criteriaID);

				//And set its class to reflect the new setting!
				YD.replaceClass(ul,oldStarClass, 'star'+rateVal);

			break;
		}

		if( false == passThru ) {
			YAHOO.util.Event.stopEvent(e);
		}

 	} 
}


/*
 * To toggle visibility of element
 * Use:
 * el: HTMLElement to show/hide
 * o {show: true, hide: true} show: override normal behviour.
 * This allows for multiple links to trigger same <el> to toggle (ie: in the case of a hide button when content is visible)
 */
$pr.toggler = function (el, o) {
	
	o = o || {};
	
	//overrides
	o.show = o.show || false;
	o.hide = o.hide || false;
	
	//Using Visibility to control whether to show or hide.
	var curVisibility = YD.getStyle(el,'visibility'); 

	//Override:
	//to do override, we see if its set - if it it, just manipulate the 
	//curVisibility value
	if(o.show) {
		curVisibility = '';
	} else if (o.hide) {
		//if its already hidden, dont bother hiding.
		if(curVisibility == 'hidden') return true;
		curVisibility = 'visible'
	}

	if( 'visible' == curVisibility ) {
		
		//store the elements current height, to restore once hidden, so its available on the next call.
		var anim = new YAHOO.util.Anim(el, {opacity: {to: 0 }, height: {to:0, unit: '%' } } , .2, YAHOO.util.Easing.easeOut);
		
		//Once the animation is done.
		anim.onComplete.subscribe(
			function () {
				YD.setStyle(el, 'visibility', 'hidden');
				YD.setStyle(el, 'display', 'none');
			}
		);

	} else {
		//Set Opacity to 0, so it doesnt pop in.
		YD.setStyle(el, 'opacity', '0');
		var anim = new YAHOO.util.Anim(el, {opacity: { to: 1 }, height:{ from: 0 , to: 100, unit: '%'} } , .2, YAHOO.util.Easing.easeIn);
	}
	
	//Set this up first, results in smoother anims, though not on table row efects
	YD.setStyle(el, 'visibility', 'visible');

	var displayStyle = (el.tagName.toLowerCase() == 'tr') ? 'table-row' : 'block' ;
	
	//Compensate for IE bug - make it block regardless of if its a tr
	if(YAHOO.env.ua.ie > 0) { displayStyle = 'block';}

	YD.setStyle(el, 'display', displayStyle);
	
	anim.animate();
	
}

YAHOO.util.Event.onDOMReady(function() {
	YE.on(window.document, "click", $pr.clickHandler); //Start listening	
});

//end
})();
