// Open new windows as target="_blank"
function externallinks () {

}

/*
Swap images
*/
function img_swap(imgobj, new_img){
	var temp = new Image();
	var image = new Image();
	image.src = new_img;
	temp.src = imgobj.src;

	imgobj.src = image.src;

	imgobj.onmouseout = function (){
		imgobj.src = temp.src;
	}
}

/*
container for header flash obj used for onload function
*/
function header_flash_obj(path, type){
	switch(type){
		default:
		case 1:
			flash_file = 'http://flash.sonypictures.com/tv/shows/seinfeld/site/seinfeldHeader.swf';
		break;
		case 2:
			flash_file = 'http://flash.sonypictures.com/tv/shows/seinfeld/site/seinfeldHeader_3LargeLogo.swf';
		break;
		case 3:
			flash_file = 'http://flash.sonypictures.com/tv/shows/seinfeld/site/seinfeldHeader_1.swf';
		break;
		case 4:
			flash_file = 'http://flash.sonypictures.com/tv/shows/seinfeld/site/seinfeldHeader_2.swf';
		break;
		case 5:
			flash_file = 'http://flash.sonypictures.com/tv/shows/seinfeld/site/seinfeldHeader_3.swf';
		break;
	}
	var header_flash = new SWFObject(flash_file, "flash", "749", "79", "8", "#FFFFFF", true);
	header_flash.addParam("wmode", "transparent"); 
	header_flash.addParam("menu", "false"); 
	header_flash.addParam("allowScriptAccess", "always");
	header_flash.write("header_flash_obj");
}

/*
container for flash player obj used for onload function
*/
function flv_player(path){
	var width = 335;//320;
	var height = 283;//240;
	var no_flash = document.getElementById('no_flash');
	var player = new SWFObject(path+"flash/videoplayer_jdSeinfeld_htmControls_wMute.swf", "videoPlayer", width, height, "8", "#FFFFFF", true);
	player.addParam("wmode", "transparent"); 
	player.addParam("menu", "false"); 
	player.addParam("allowScriptAccess", "always");
	player.write("flv_player");
	no_flash.style.display = 'block';
}

/*
Play flash video on flash player
*/
function play_flash_video(id, video){
	// Use a variable to reference the embedded SWF file.
    var flashVideoPlayer;

    /* Check whether the browser is IE. If so, flashVideoPlayer is
	window.videoPlayer. Otherwise, it's document.videoPlayer. The
	videoPlayer is the ID assigned to the <object> and <embed> tags. */
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    flashVideoPlayer = (isIE) ? window[id] : document[id];

    /* When the user clicks the play button in the form, and call the playVideo() function within the SWF file, passing it the URL of the FLV file. */
    flashVideoPlayer.playVideo(video);
}

/*
Play episode in flash player
*/
function play_episode(path,episode,wait){
	//var ep_dir = 'http://flash.sonypictures.com/tv/shows/seinfeld/FLV/';
	var ep_dir = 'http://flash.sonypictures.com/video/tv/shows/seinfeld/episodes/';
	var vid_id = 'videoPlayer';
	episode = String(episode);
	//alert(path+','+episode);
	var season = episode.substr(0,1);
	var ep_name = 'Seinfeld_Ep_Promos_Season_'+season+'_Episode_'+episode+'.flv';
	//play_flash_video(vid_id, path+ep_dir+ep_name);
	if(!wait)
		wait = 0;
	window.setTimeout('play_flash_video(\''+vid_id+'\',\''+ep_dir+ep_name+'\')',wait);//wait for flash player to load
}

/*
returns Sypnopsys of Episode. Play episode in Player for dvd section.
*/
function dvd_ep_onclick(divId, ep_num, path){

	//playFlashVideo('videoPlayer',video);
	//return get_xml_content(divId, season, ep_num, tab);

	var textDiv = document.getElementById(divId);
	textDiv.innerHTML = '<p class="smaller_text bold_left orange_text" >SYPNOPSIS</p><p class="large_text bold_left dark_orange_text">' + 	season_episodes[ep_num][0] + '</p><p class="general_text add_padding">' + 	season_episodes[ep_num][1] + '</p>';
	
	//reload scroll obj
	myScroll0 = new ScrollObj(5,41,283,"dvdc_text_scroller_track","dvdc_text_scroller_up","dvdc_text_scroller_down","dvdc_text_scroller_drag","dvdc_text_scroller_contentMask","dvdc_text_scroller_content");

	play_episode(path, ep_num);
}

/*
returns Sypnopsys of Episode. Play episode in Player for video section.
*/
function search_ep_onclick(obj, content_div_id, ep_id, ep_ary, ep_season, path, images_dir){
	var	content_div = document.getElementById(content_div_id);
	var link = document.getElementById('dvd_link');
	var img = document.getElementById('dvd_img');

	obj.href = 'javascript:void(0);';//prevent redirect

	content_div.innerHTML = '<p class="large_text bold_left dark_orange_text">'+ep_ary[ep_id][0]+'</p><p class="general_text add_padding">'+ep_ary[ep_id][1]+'(<a href="'+path+'episode_guide/?sl=episode&ep='+ep_id+'">more</a>)</p><p class="small_text add_padding"><img src="'+images_dir+'small_brown_arrow.gif" alt="Seinfeld" /> <a href="'+path+'episode_guide/?sl=episode&ep='+ep_id+'">Click here to view the version of this clip including the seinfacts and episode information</a></p>';

	play_episode(path, ep_id);

	link.href = ep_ary[ep_id][2];

	img.src = './images/buy_season_'+ep_season+'_button.jpg';

	img.onmouseover = function(){
		img_swap(img, './images/buy_season_'+ep_season+'_button_selected.jpg');
	}

	return false;
}

/*
Function used to change from cast to character and vice versa
*/
function change_cast_type(sl,tab,type){
	window.location = '?sl='+sl+'&tab='+tab+'&type='+type;
}

/*
Show background button for selected type
*/
function show_cast_buttons(unselected_id){
	//give unselected rollover affect
	var obj2 = document.getElementById(unselected_id);
	obj2.style.cursor = 'pointer';

	obj2.onmouseover = function(){
		obj2.style.background = 'url(../about/images/cast_button_selected.jpg) no-repeat center';
	}

	obj2.onmouseout = function(){
		obj2.style.background = 'url()';
	}
}

/*
Logo onclick function, returns home directory
*/
function returnHome(){
	window.location = abs_path;
}

function setHome(path){
	abs_path = path;
}

function station_lookup(station){
	var value = document.getElementById('station_lookup').value;
	var url = '';
	var func = 'station_result_action';
	if(value!=''){
		document.getElementById('station_result').innerHTML = '<p>Searching...</p>';
		url = 'station_finder/station_results.php?code='+value;
		if(station!='' && station!=undefined)
			url += '&station='+station;


		ajaxFunction(url,func);
	}
}

function station_result_action(result){
	if(result == ''){
		station_lookup_popup();
		document.getElementById('station_result').innerHTML = '';
	}else{
		document.getElementById('station_result').innerHTML = result;
	}
}

function station_lookup_popup(){
	var url = 'station_finder/index.php';
	var value = document.getElementById('station_lookup').value;
	if(value!=''){
		url += '?code='+value;
		seinfeldPopUp(url,450,603,true);
	}
}

/*
Pop up function that centers popup window
*/
function seinfeldPopUp(url,width,height,scroll){
	var top = screen.width/2 - width/2;
	var left = screen.height/2 - height/2;
	var scrollbar = 'scrollbars=0';
	if(scroll==true)
		scrollbar = 'scrollbars=1';
	
	window.open(url,'popup','width='+width+',height='+height+',top='+top+',left='+left+',resizable=0,toolbar=0,menubar=0,location=0,'+scrollbar+',status=0');
}

/*
Determine whether seinfacts or seinspeak will need a scroller.  Only include a scroller for the taller of the two.
*/
function toggleSeinScroller(){
	var seinfacts = document.getElementById("seinfacts");
	var seinspeak = document.getElementById("seinspeak");
	var maxheight = 159;//set default height just in case this breaks//should be grabbed from style sheet rule minheight_prop_box but too much work right now
	var needscroller = null;
	var scrollerobj = null;
	var height_offset = 0;

	//get height
	var seinfacts_height = seinfacts.offsetHeight;
	var seinspeak_height = seinspeak.offsetHeight;

	//get shorter of the two
	height_offset = Math.abs(seinfacts_height - seinspeak_height);
	if(height_offset < 2){//if difference of the 2 heights is only 1 px or less then forgo the scroller
		return;
	}else if(seinfacts_height > seinspeak_height){
		maxheight = seinspeak_height;
		needscroller = seinfacts;
	}else{
		maxheight = seinfacts_height;
		needscroller = seinspeak;
	}

	createScrollerEncasing(needscroller,maxheight);
}

/*
Create a scroller wrapped around the object
*/
function createScrollerEncasing(obj,maxheight){
	var scrollname = 'beige';
	var images_dir = '../images/';
	var scroller_track_img = 'scroller_beige_bg.jpg';
	var scrollerhtml = '';
	var parent = obj.parentNode;
	var scrollerobj = document.createElement('div');

		scrollerhtml += '<div id="'+scrollname+'_text_scroller_up"></div>';
		scrollerhtml += '<div id="'+scrollname+'_text_scroller_track">';
			scrollerhtml += '<img src="'+images_dir+scroller_track_img+'" alt="Seinfeld" width="16" height="'+maxheight+'"/></div>';
		scrollerhtml += '<div id="'+scrollname+'_text_scroller_drag">';
			scrollerhtml += '<img src="'+images_dir+'scroller.gif" alt="Seinfeld" /></div>';
		scrollerhtml += '<div id="'+scrollname+'_text_scroller_down"></div>';
		scrollerhtml += '<div id="'+scrollname+'_text_scroller_content" class="general_text">';
			scrollerhtml += '<div id="'+obj.id+'">'+obj.innerHTML + '</div></div>';

	//set properties for scroller
	scrollerobj.innerHTML = scrollerhtml;
	scrollerobj.id = scrollname+"_text_scroller_contentMask";
	scrollerobj.style.height = maxheight + 'px'; 
	scrollerobj.style.clip = 'rect(0,320px,'+(maxheight-20)+'px,0)'; 

	parent.replaceChild(scrollerobj,obj);
	myScroll2 = new ScrollObj(5,41,maxheight,scrollname+"_text_scroller_track",scrollname+"_text_scroller_up",scrollname+"_text_scroller_down",scrollname+"_text_scroller_drag",scrollname+"_text_scroller_contentMask",scrollname+"_text_scroller_content");
}

function ajaxFunction(file,func){
   var ajaxRequest;  // The variable that makes Ajax possible!
   var str;

   try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
   } catch (e){
      // Internet Explorer Browsers
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
         }
      }
   }
   // Create a function that will receive data sent from the server
   ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
		 eval(func+'(\''+ajaxRequest.responseText+'\');');
         //document.getElementById(div).innerHTML = ajaxRequest.responseText;
      }
   }

   ajaxRequest.open("GET", file, true);
   ajaxRequest.send(null);
}