/* javascripts */

// extend prototype lib
Position.getWindowSize = function() {
    var w = window;
    var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
    var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
    return {x:width,y:height};
}

// register all events
function init() {
    
    // preload images
    var images = ['/img/search-btn-hover.gif'];
    for ( i=0;i<images.length;i++ ) {
        var img = new Image();
        img.src = images[i];
    }
	
    // set tab onhover effects
	var tabLists = document.getElementsByClassName('tab-list');
	
	if ( tabLists.length ) {
		var tabList = tabLists[0];
		
		var tabs = tabList.getElementsBySelector('li');
		
		tabs.each(function(tab) {
			
			Event.observe(tab,'mouseover',function(){ 
				if ( tab.className != 'selected' ) {
					tab.style.backgroundColor = '#F2F2F2'; 
				}
			});
			Event.observe(tab,'mouseout',function(){ 
					tab.style.backgroundColor = '#fff';
			});
		});
	}
	
	// set more tab events
	var tabs = $A(document.getElementsByClassName('tab-list-more'));
	tabs.each(function(tab) {
		var els = tab.getElementsBySelector('ul');
		var list = els[0];
		
		Event.observe(tab,'mouseover',function(e) {
			list.style.display = 'block';
		});
		
		Event.observe(tab,'mouseout',function(e) {
			list.style.display = 'none';
		});
	});
}

// open a custom player window
function customplayer(name,params)
{
	var height = 0;
	var width = 0;
	
	if ( name == "movielink" ) {
		height = 460;
		width = 350;
	}
	
	var w = window.open("/customplayers/"+name+"/?"+params,"_blank","location=0,height="+height+",width="+width+",menubar=0,resizeable=0,scrollbars=0,toolbar=0,status=0,titlebar=0,fullscreen=0,top=150,left=200");
	w.focus();
}

var DirSendToFriend = {
	submit: function() {
		
		if ( !this.validate() ) {
			return false;
		}
		
		var form = $('form-send-to-friend');
		var serialized = Form.serialize(form);
		
		serialized += '&url=' + escape(window.location);
	
		new Ajax.Updater('send-to-friend', '/actions/SendDirPage.php', {
			parameters: serialized
		});
		
		new Effect.SlideUp('send-to-friend',{delay: 4.0, duration:0.5});
		
	},
	setLoading: function(){},
	validate: function(){
		var form = $('form-send-to-friend');
		var inputs = $A(form.getInputs());
		
		var valid = true;
		
		inputs.each( function(input) {
			if ( input.value.length < 1 ) {
				valid = false;
			}
		});
		
		if ( !valid ) {
			alert('Please complete the form and try again.');
			return false;
		}
		
		return true;
	}
}

// alert when a user submits a blank search request
function search() {
    var k = $('k');
    if ( k.value.length == 0 || k.value == 'Enter your search here' ) {
    	alert('Please enter a search phrase.');
    } else {
        window.location = '/search.php?k=' + k.value;
    }
}

var RecommendedList = {
	select: function( titleToSelect ) {
		var list = $('recommended-tabs');
		var tabs = $A(list.getElementsBySelector('li'));
		
		var h2 = $('recommended-title');
		h2.innerHTML = titleToSelect + ' Videos';
		
		tabs.each( function(tab) {
			var els = tab.getElementsBySelector('a');
			var link = els[0];
			
			var title = link.innerHTML;
			var videoListId = 'recommended-' + title.toLowerCase().replace(' ','-');

			var videoList = $(videoListId);
			
			if ( title == titleToSelect ) {
				tab.className = 'selected';
				videoList.style.display = 'block';
			} else {
				tab.className = '';
				videoList.style.display = 'none';
			}
		});		
	}
}

// subscribe to a podcast
function podcastSubscribe( title, feed ) {
	var nav = navigator.userAgent.toLowerCase();
	
	if ( nav.indexOf('macintosh') < 0 ) {
		// if windows, use pcast file jsp
		window.open('/pcast.php?feed=' + feed + '&title=' + title);
	} else {
	    feed = unescape(feed);
		// if mac & firefox, use pcast protocol
		if ( nav.indexOf('firefox') > -1 ) {
			feed = feed.replace("http://","itpc://");
			window.location = feed;
		} else if ( nav.indexOf('safari') > -1 ) {
			feed = feed.replace("http://","pcast://");
			window.location = feed;
		}
	}
}
