/*****************************************************************************/
/*
/* DOM Ready
/*
/*****************************************************************************/

// Event.onReady(callbackFunction);
Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (Event._timer)  clearInterval(Event._timer);
    
    Event._readyCallbacks.each(function(f) { f() });
    Event._readyCallbacks = null;
    
  },
  onReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady;
      
      if (domReady.done) return f();
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") { domReady(); }
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});


function initEventSearch(prefix, fields) {
  if($('results_event_search')) {
    fields.each(function(name) {
      var input = $(prefix + name);
      var msg = '';

      if (input == $('results_event_search_eventName')) msg = 'Event Name';
      if (input == $('results_event_search_toDate')) msg = 'To Date';
      if (input == $('results_event_search_fromDate')) msg = 'From Date';
      if (input == $('results_event_search_state')) msg = 'State';
      if (input == $('results_event_search_sport')) msg = 'Sport';

      if (input) {
        name = name.gsub(/_/, ' ');
        if (input.value == '' || input.value == msg) {
          input.value = msg;
          input.style.color = '#aaaaaa';
        }
        
        input.onfocus = function() {
          if (this.value == msg) {
            this.value = '';
            this.style.color = '#000000';
          }
        }
        
        input.onchange = function() {
          if (this.value != msg) {
            this.style.color = '#000000';
          }
        }
        
        input.onblur = function() {
          if (this.value == '') {
            this.value = msg;
            this.style.color = '#aaaaaa';
          }
        }

        input.onclick = function() {
          this.value = '';
        }
        
      }
    })
  } 
}

function initEventSearchPulldown(prefix, fields) {
  if($('results_event_search')) {
    fields.each(function(name) {
      var input = $(prefix + name);
      var msg = '';

      if (input == $('results_event_search_state')) msg = 'State';
      if (input == $('results_event_search_sport')) msg = 'Sport';

      if (input) {
        input.style.color = '#aaaaaa';
 		
        input.onfocus = function() {
          if (this.selectedIndex <= 0) {
            this.style.color = '#000000';
          }
        }

       input.onblur = function() {
          if (this.selectedIndex > 0) {
            this.style.color = '#000000';
          } else {
            this.style.color = '#aaaaaa';          
          }
        }
                
        input.onchange = function() {
          if (this.selectedIndex > 0) {
            this.style.color = '#000000';
          } else {
            this.style.color = '#aaaaaa';          
          }
        }
        
      }
    })
  } 
}


function initPersonSearch(prefix, fields) {

  if($('results_person_search')) {
    fields.each(function(name) {
      var input = $(prefix + name);
      var msg = '';

      if (input == $('results_person_search_firstName')) msg = 'First Name';
      if (input == $('results_person_search_lastName')) msg = 'Last Name';
      if (input == $('results_person_search_gender')) msg = 'Gender';
      if (input == $('results_person_search_age')) msg = 'Age';
      if (input == $('results_person_search_city')) msg = 'City';
      if (input == $('results_person_search_state')) msg = 'State';
      if (input == $('results_person_search_country')) msg = 'Country';

      if (input) {
        name = name.gsub(/_/, ' ');
        if (input.value == '' || input.value == msg) {
          input.value = msg;
          input.style.color = '#aaaaaa';
        }
        
        input.onfocus = function() {
          if (this.value == msg) {
            this.value = '';
            this.style.color = '#000000';
          }
        }
        
        input.onchange = function() {
          if (this.value != msg) {
            this.style.color = '#000000';
          }
        }
        
        input.onblur = function() {
          if (this.value == '') {
            this.value = msg;
            this.style.color = '#aaaaaa';
          }
        }

        input.onclick = function() {
          this.value = '';
        }
        
      }
    })
  }
}

function initPersonSearchPulldown(prefix, fields) {

  if($('results_person_search')) {
    fields.each(function(name) {
      var input = $(prefix + name);
      var msg = '';

      if (input == $('results_person_search_gender')) msg = 'Gender';
      if (input == $('results_person_search_state')) msg = 'State';

      if (input) {

        input.style.color = '#aaaaaa';
 		
        input.onfocus = function() {
          if (this.selectedIndex <= 0) {
            this.style.color = '#000000';
          }
        }
       
        input.onblur = function() {
          if (this.selectedIndex > 0) {
            this.style.color = '#000000';
          } else {
            this.style.color = '#aaaaaa';          
          }
        }        
        
        input.onchange = function() {
          if (this.selectedIndex > 0) {
            this.style.color = '#000000';
          } else {
            this.style.color = '#aaaaaa';          
          }
        }
        
      }
    })
  }

}

Event.onReady(function() {
	initEventSearch('results_event_search_',new Array('eventName','toDate','fromDate'));
	initEventSearchPulldown('results_event_search_',new Array('state','sport'));
	initPersonSearch('results_person_search_',new Array('firstName','lastName','age','city'));
	initPersonSearchPulldown('results_person_search_',new Array('gender','state'));
});


/*All Channels*/
Effect.ChannelsUp=function(element){
	element=$(element);
	element.makeClipping();
	return new Effect.Scale(element,0,Object.extend({
		scaleContent:false,
		scaleX:false,
		restoreAfterFinish:true,
		afterUpdate:function(effect){
			updateOpaque();
		},
		afterFinishInternal:function(effect){
			effect.element.hide();
			effect.element.undoClipping();
			updateOpaque();
			$('all_channels_opaque').hide();
		}
	},arguments[1]||{}));
}

Effect.ChannelsDown=function(element){
	element=$(element);
	var elementDimensions=element.getDimensions();
	return new Effect.Scale(element,100, Object.extend({
		scaleContent:false,
		scaleX:false,
		scaleFrom:0,
		scaleMode:{originalHeight:elementDimensions.height,originalWidth:elementDimensions.width},
		restoreAfterFinish:true,
		afterSetup:function(effect){
			effect.element.makeClipping();
			effect.element.setStyle({height:'0px'});
			effect.element.show();
		},
		afterUpdate:function(effect){
			updateOpaque();
		},
		afterFinishInternal:function(effect){
			effect.element.undoClipping();
			updateOpaque();
		}
	},arguments[1]||{}));
}

var allChannelsOpen=false;
var allChannelsClose=false;

updateOpaque=function(){
	var ac=$('all_channels');
	var opaque=$('all_channels_opaque');
	opaque.style.top=ac.offsetTop+'px';
	opaque.style.left=ac.offsetLeft+'px';
	opaque.style.width=ac.offsetWidth+'px';
	opaque.style.height=ac.offsetHeight+'px';
	opaque.show();
}



function initAllChannels(){
	if($('all_channels_button')){
		insideAllChannels=0;
		showAllChannelsHandle=null;
		hideAllChannelsHandle=null;
		blinding=false;

		showAllChannels=function(){
			if(blinding)return;
			if(!$('all_channels').visible()&&allChannelsOpen){
				blinding=true;
				troubTab('hide')
				new Effect.ChannelsDown('all_channels',{duration:.3,afterFinish:function(){blinding=false;}});
			}
		}

		hideAllChannels=function(){
			if(blinding)return;
			if($('all_channels').visible()&&insideAllChannels==0){
				blinding=true;
				new Effect.ChannelsUp('all_channels',{duration:.3,afterFinish:function(){blinding=false;}});
				setTimeout("troubTab('show')",200);
			}
		}

		troubTab=function(action){
			var f=$$('#mod_featured.tab_box .title');
			for(var i=0;i<f.length;i++){
				if(action=="show"){
					Element.show(f[i]);
					document.getElementById("mod_featured").style.paddingTop="0em";
				} else {
					Element.hide(f[i]);
					document.getElementById("mod_featured").style.paddingTop="3.14em";
				}
			}
		}

		fireHideAllChannels=function(wait){
			if(hideAllChannelsHandle){
				clearTimeout(hideAllChannelsHandle);
			}
			hideAllChannelsHandle=setTimeout("hideAllChannels()",wait);
		}

		$('all_channels_button').onmouseover=function(){
			allChannelsOpen=true;
			setTimeout("showAllChannels()",200);
		}

		$('all_channels_button').onmouseout=function(){
			allChannelsOpen=false;
			fireHideAllChannels(400);
		}

		$('all_channels_button').onclick=function(){
			if(!$('all_channels').visible()){
				showAllChannels();
			} else {
				hideAllChannels();
			}
		}

		$('all_channels').onmouseout=function(){
			allChannelsClose=true;
			insideAllChannels--;
			fireHideAllChannels(400);
		}

		$('all_channels').onmouseover=function(){
			insideAllChannels++;
			allChannelsClose=false;	
		}
	}
}


/*Page Load*/
Event.observe(window,'load',function(){
	initAllChannels();
});



