$(function() {

    var POD_SEARCH_TEXT = 'Search podcasts...';
    var APP_SEARCH_TEXT = 'Search apps...';

    // Init tabs
    $(".tabs").tabs({
	select: function(event, ui) {
	    // ui.index is a zero indexed list of tabs
	    if (ui.index == 0) { // apps tab
		setSearch('/apps/search/', APP_SEARCH_TEXT);
	    } else if (ui.index == 1) { // podcasts tab
		setSearch('/podcasts/search/', POD_SEARCH_TEXT);
	    }
	}
    });

    // shamelessly copied from the jquery tutorials page
    function populateElement(selector, defvalue) {
	// first unbind any previous events
	$(selector).unbind('focus');
	$(selector).unbind('blur');

	$(selector).each(function() {
	    var current = $.trim(this.value);
            if(current == "" || current == APP_SEARCH_TEXT || 
	       current == POD_SEARCH_TEXT) {
		this.value = defvalue;
            }
	});
	
	$(selector).focus(function() {
            if(this.value == defvalue) {
		this.value = "";
            }
	});
	
	$(selector).blur(function() {
            if($.trim(this.value) == "") {
		this.value = defvalue;
            }
	});
    };

    // Set the search bar to app/podcasts search, and set default text
    function setSearch(search, defvalue) {
	populateElement("div.search input.text", defvalue);
	
	// set the search parameters
	var form = $('div.search > form');
	form.attr('action', search);
	form.find('input:first').attr('name', 'q');
    };

    function setInitialSearch() {
	var search = "";
	var defvalue = 'Search...';
	var body = $('body');

	if (body.hasClass('apps') || body.hasClass('home')) {
	    defvalue = APP_SEARCH_TEXT;
	    search = '/apps/search/';
	} else if (body.hasClass('podcasts')) {
	    defvalue = POD_SEARCH_TEXT;
	    search = '/podcasts/search/'
	}
	setSearch(search, defvalue);
    }
    setInitialSearch();

    // Init lightbox
    function addFancyBox() {
	    var box = $("a[rel='lightbox']");
	    if (box[0]) {
	        box.fancybox();
	    }
    };
    addFancyBox();
    
    // Podcast player
    $('div.controls a.play').click(function(event) {
        var button = $(this);
        var title = button.parent().parent().find('.title').html();
        var enclosure_url = button.parent().parent().find('.enclosure_url').html();
        var mthumb = $("#pthumb").attr("src");
        doubletwist.Player.checkAndPlay(title,enclosure_url,mthumb,function(errortype){},function(){});
        return false;
    });
    
    $('div.episode span.more').click(function(event) {
        var that = $(this);
        var ul = that.find('ul');
        ul.css('display', 'block');

        that.bind('clickoutside', function(event) {
            ul.css('display', 'none');
            that.unbind('clickoutside');
        });
    });
    
    // Add support for ellipsis text overflow.
    // $('.item .title').textOverflow();
    // $('.item .category').textOverflow();
    // $('.item .description').textOverflow();
    
    // Init video in Lightbox
    $("a[rel='lightbox-video']").click(function() {
    	$.fancybox({
    			'padding'		: 0,
    			'autoScale'		: false,
    			'transitionIn'	: 'none',
    			'transitionOut'	: 'none',
    			'title'			: this.title,
    			'width'		    : 680,
    			'height'		: 495,
    			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
    			'type'			: 'swf',
    			'swf'			: {
                    'wmode'		: 'transparent',
    				'allowfullscreen'	: 'true'
    			}
    		});

    	return false;
    });
});



