/**
 * handles the code for the schedule page
 */

	var counter = 0;
	    
	function initSchedule(event){
		
		Event.observe($('prev_btn'), 'click', function() {
			scrollSchedule('left');
		});

		Event.observe($('next_btn'), 'click', function() {
				scrollSchedule('right');
		});
		
		if (Prototype.Browser.IE){
	        $$('div.tv_block').each( function(e) {
	            Event.observe(e, 'mouseover', function() {
		  			Element.removeClassName(e, 'tv_block');  
	                Element.addClassName(e, 'tv_block_hover');  
	            });  
	            Event.observe(e, 'mouseout', function() {  
					Element.removeClassName(e, 'tv_block_hover');  
					Element.addClassName(e, 'tv_block');  
	            });	
	        });
	
	        $$('div.movie_block').each( function(e) {  
	            Event.observe(e, 'mouseover', function() {
		  			Element.removeClassName(e, 'movie_block');  
	                Element.addClassName(e, 'movie_block_hover');  
	            });  
	            Event.observe(e, 'mouseout', function() {  
					Element.removeClassName(e, 'movie_block_hover');  
					Element.addClassName(e, 'movie_block');  
	            });  
	        });  
	    }   
	}
	
	function setInitialLocation(){     
		var this_date = new Date();
		var hour = this_date.getHours();
		var initial_move_factor = 0;
		var scheduleBlock = $('scheduleContainer');
		var timeBlock = $('timeContainer');		
		var moveWidth = timeBlock.down(1).next(4).getWidth();  
  
		switch(hour)
		{
			case 0:
			case 1:
			case 2:
				//do nothing
				break;
			case 3:
			case 4:     
				initial_move_factor = 1; 
			    break;
			case 5:  
			case 6:
			case 7:      
				initial_move_factor = 2;
				break;
			case 8:
			case 9:            
				initial_move_factor = 3;
			    break;
			case 10:  
			case 11:
			case 12:
				initial_move_factor = 4;
				break;   
			case 13:
			case 14:      
				initial_move_factor = 5;
			    break;
			case 15:  
			case 16:
			case 17:    
				initial_move_factor = 6;
				break; 
			case 18:
			case 19:     
				initial_move_factor = 7;
			    break;
			case 20:  
			case 21:
			case 22:       
				initial_move_factor = 8;
				break;      
		   	case 23:
			case 24:           
				initial_move_factor = 9;
			    break;   
	    }               
	
		if (hour < 23){
			var moveFactor = initial_move_factor*(moveWidth*-5); 
		}else{
			var moveFactor = initial_move_factor*(moveWidth*-3);       
		}
		     
		new Effect.Move(scheduleBlock, { x: moveFactor, y: 0, mode: 'relative', beforeStart: beforeInitial , afterFinish: afterInitial, queue: { position: 'end', scope: 'scheduleblock', limit: 1 } });
		new Effect.Move(timeBlock, { x: moveFactor, y: 0, mode: 'relative', queue: { position: 'end', scope: 'timeblock', limit: 1 } });                          
	}
	
	function beforeInitial(){
		deactivateControls();
		goModal();
	}
	    
	
	function afterInitial(){
		initSchedule();
		stopModal();      
	}   
	
	function scrollSchedule(direction){           
		var should_move = false;
 		//remove old popup if exists
		if ($('videoinfo')){
			$('videoinfo').remove();
			$('close_btn').remove();
		}           
		var scheduleBlock = $('scheduleContainer');
		var timeBlock = $('timeContainer');	 
		var moveWidth = timeBlock.down(1).next(4).getWidth();
		var schedule_offset = scheduleBlock.cumulativeOffset();
		var shell_offset = $('scheduleShell').cumulativeOffset();
		var differential = shell_offset.left - schedule_offset.left;   

		if(direction == 'left'){
			if ((differential != 0) && (differential < 7400)){
				var moveFactor = moveWidth*5;  
				should_move = true; 
			}
			else if((differential > 7400) && (differential < 7700)) {
				var moveFactor = moveWidth*3;
				should_move = true; 
			}else{
				should_move = false;
			}
		}
		else{
			if (differential < 7000){
				var moveFactor = moveWidth*-5;
				should_move = true; 
			}
			else if((differential > 7000) && (differential < 7500)) {
				var moveFactor = moveWidth*-3;
				should_move = true; 
			}else{
				should_move = false; 
			}
		}
		if (should_move){
			new Effect.Move(scheduleBlock, { x: moveFactor, y: 0, mode: 'relative', beforeStart: deactivateControls, afterFinish: initSchedule, queue: { position: 'end', scope: 'scheduleblock', limit: 1 } });
			new Effect.Move(timeBlock, { x: moveFactor, y: 0, mode: 'relative', queue: { position: 'end', scope: 'timeblock', limit: 1 } });
		}
	}
	
	function showNextSchedule(){
		//remove old popup if exists
		if ($('videoinfo')){
			$('videoinfo').remove();
			$('close_btn').remove();
		}
				
		goModal();
		$('todays_date').removeClassName('this_date');      
		$('todays_date').addClassName('next_weeks_date');   
		var url = 'parse_schedule.php?start_date=next_week';
		
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(transport) {
							
		    $('this_week_btn').removeClassName('active');
			$('next_week_btn').removeClassName('inactive');
			
			$('this_week_btn').addClassName('inactive');
			$('next_week_btn').addClassName('active');
			
			$('this_week_btn').src = '_images/thisweek_tab.png';
			$('next_week_btn').src = '_images/nextweek_tab_active.png';
			
			$('todays_date').innerHTML = getScheduleDate();
			$('scheduleContainer').update(transport.responseText);
			stopModal();
			if (Prototype.Browser.IE){
		        $$('div.tv_block').each( function(e) {  
		            Event.observe(e, 'mouseover', function() {
			  			Element.removeClassName(e, 'tv_block');  
		                Element.addClassName(e, 'tv_block_hover');  
		            });  
		            Event.observe(e, 'mouseout', function() {  
						Element.removeClassName(e, 'tv_block_hover');  
						Element.addClassName(e, 'tv_block');  
		                
		            });  
		        });  
			
		        $$('div.movie_block').each( function(e) {  
		            Event.observe(e, 'mouseover', function() {
			  			Element.removeClassName(e, 'movie_block');  
		                Element.addClassName(e, 'movie_block_hover');  
		            });  
		            Event.observe(e, 'mouseout', function() {  
						Element.removeClassName(e, 'movie_block_hover');  
						Element.addClassName(e, 'movie_block');  
		                
		            });  
		        });  
		    }
			
		  }
		});
	}
	
	function showThisSchedule(){
		//remove old popup if exists
		if ($('videoinfo')){
			$('videoinfo').remove();
			$('close_btn').remove();
		}
		
		goModal();
		                                                            
		$('todays_date').removeClassName('next_weeks_date');  
		$('todays_date').addClassName('this_date');     
		
		var url = 'parse_schedule.php?start_date=this_week';
		
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(transport) {
		   	
			$('this_week_btn').removeClassName('inactive');
			$('this_week_btn').addClassName('active');
			
			$('next_week_btn').removeClassName('active');			
			$('next_week_btn').addClassName('inactive');
			
			$('this_week_btn').src = '_images/thisweek_tab_active.png';
			$('next_week_btn').src = '_images/nextweek_tab.png';
			
			$('todays_date').innerHTML = "Today";      
			sIFR.replace(gotham, {
			  selector: 'li.sifr-head-today',
			  css: '.sIFR-root { background-color: #000000; color: #ffffff;}'
			});
			$('scheduleContainer').update(transport.responseText);
			
			stopModal();
			
			if (Prototype.Browser.IE){
		        $$('div.tv_block').each( function(e) {  
		            Event.observe(e, 'mouseover', function() {
			  			Element.removeClassName(e, 'tv_block');  
		                Element.addClassName(e, 'tv_block_hover');  
		            });  
		            Event.observe(e, 'mouseout', function() {  
						Element.removeClassName(e, 'tv_block_hover');  
						Element.addClassName(e, 'tv_block');  
		                
		            });  
		        });  
			
		        $$('div.movie_block').each( function(e) {  
		            Event.observe(e, 'mouseover', function() {
			  			Element.removeClassName(e, 'movie_block');  
		                Element.addClassName(e, 'movie_block_hover');  
		            });  
		            Event.observe(e, 'mouseout', function() {  
						Element.removeClassName(e, 'movie_block_hover');  
						Element.addClassName(e, 'movie_block');  
		                
		            });  
		        });  
		    }
			
		  }
		});
		
	}
	
	function goModal(){               
        var d = new Element('div', { 'class': 'modal_screen', id:'modal_layer'}).update("<img src='_images/schedule/loader.gif'/>");                                   
        d.setOpacity(0.8);                   
        d.clonePosition("scheduleShell");
        document.body.appendChild(d);    
    }         
	
	function stopModal(){   
		$('modal_layer').remove();
	}
	
	function getScheduleDate(){
		var d=new Date();
		var weekday=new Array(7);
			weekday[0]="Sun";
			weekday[1]="Mon";
			weekday[2]="Tue";
			weekday[3]="Wed";
			weekday[4]="Thu";
			weekday[5]="Fri";
			weekday[6]="Sat";
		return weekday[d.getDay()];
	}
	
	function deactivateControls(){    
		Event.stopObserving('next_btn');
		Event.stopObserving('prev_btn');
		//$('prev_btn').stopObserving();
		//$('next_btn').stopObserving();
	}
	
	function createShowPopup(el){
		//remove old popup if exists
		if ($('videoinfo')){
			$('videoinfo').remove();
			$('close_btn').remove();
		}
		
		id = el.id;            
		//grab x,y of div that called this function
	   	var offset = el.cumulativeOffset();
	
		var body_width = document.body.clientWidth;

		var schedule_offset = $('scheduleShell').cumulativeOffset();
    
		//make sure that the item being clicked is within the schedule bounds, otherwise don't popup
		if (offset.left >= schedule_offset.left){
			//check to see where div is to decide which side to popup on
			if (offset.left > (schedule_offset.left + 441)){
				var new_x = offset.left - 228;
				var new_y = offset.top - 20;
                right_arrow = true;
				//create new popup on the fly
				var a = new Element('div', { 'class': 'popup_left', id:'videoinfo'}).update("<div><img src='_images/schedule/loader.gif'/></div><img id='right_arrow' src='_images/rt_arrow.gif'/>");
				var b = new Element('img', {id:'close_btn', src:'_images/btn-close.gif'});
			}else{			    
				var new_x = offset.left + 82;
				var new_y = offset.top - 20;
								
   				right_arrow = false;
				//create new popup on the fly 
				var a = new Element('div', { 'class': 'popup_right', id:'videoinfo'}).update("<img id='left_arrow' src='_images/arrow.gif'/><div><img src='_images/schedule/loader.gif'/></div>"); 	  
				var b = new Element('img', {id:'close_btn', src:'_images/btn-close.gif'});
			}                  
			
			document.body.appendChild(a);			
			document.body.appendChild(b); 
   
			Event.observe($('close_btn'), 'click', function() {
				$('videoinfo').remove();
				$('close_btn').remove();
            });                       
			
			a.setOpacity(0.8);
			var text_width = getTextWidth($(id).down("p", 1).textContent);
                     
			$('videoinfo').style.top = new_y+"px";    
			if (offset.left < (schedule_offset.left + 441)){
				$('videoinfo').style.left = ((new_x-70)+text_width)+"px";				
				$('close_btn').style.top = (new_y - 10)+"px";
				$('close_btn').style.left = (new_x+123)+text_width+"px";
			}else{
				$('videoinfo').style.left = (new_x-23)+"px";				
				$('close_btn').style.top = (new_y - 10)+"px";
				$('close_btn').style.left = (new_x -15)+"px";
			}            
		}                        
		
	   
	   	var show_time = el.down("p").innerHTML;
		show_code = id.substring(5);
		var URL = "get_show_detail.php?show_code="+show_code+"&show_time="+show_time;
		
		
		new Ajax.Request(URL, {
		  method: 'get',
		  onSuccess: function(transport) {
			    var body_text = transport.responseText;             
				$('videoinfo').down('div').update(body_text);		
		  }
		});			
	}
	
	function getTextWidth(text)
	{
		var spanElement = document.createElement("span");
		spanElement.style.whiteSpace = "nowrap";
		spanElement.innerHTML = text;
		document.body.appendChild(spanElement);
		var width = spanElement.offsetWidth;
		document.body.removeChild(spanElement);
       
		return width;
	}
	
	function printSchedule(){
		print_frame.focus(); 
		print_frame.print(); 	
	}