var currentOpenVideoId = null;

// perform request
function pr() {
    var input = jQuery.trim($('#url')[0].value);
    if (!input.length) {
        return false;
    }
    var sort = $('#sort')[0].options[$('#sort')[0].selectedIndex].value;
    pr2(input, 0, sort);
}

function pr2(input, index, sort) {
    var args = 'sender=f&args=' + encodeURIComponent(input);
    args += '&i=' + encodeURIComponent(index);
    args += '&s=' + encodeURIComponent(sort);
    $.post('.', args, callback, 'json');
}

function mp() {
    var args = 'sender=f&args=popular';
    $.post('.', args, callback, 'json');
}

function nv() {
    var args = 'sender=f&args=new';
    $.post('.', args, callback, 'json');
}

function tc() {
    var args = 'sender=f&args=tagcloud';
    $.post('.', args, callback, 'json');
}

function t(tagid) {
     var args = 'sender=f&args=tag&id=' + tagid;
    $.post('.', args, callback, 'json')
}

function callback(data, textStatus) {
    if (data.id) {
        $('#' + data.id).html(data.content);
        if (data.id == 'panel') {
            var links = $("a[@rel^='shadowbox']");
            if (links)
                Shadowbox.setup();
        }
        //if (data.scrollTop) {
            scroll(0,0);
        //}
        if (data.debug) {
            alert(data.debug);
        }
    }
    if (data.error) {
        alert(data.error);
    }
}

// block when ajax activity starts
$().ajaxStart(function() { $.blockUI({ css: { padding: '2em', border: '2px solid #fff', color: '#eee', backgroundColor:'#222' }, message: '<h1 style="lineheight:2em;vertical-align:middle"><img src="images/load.gif" />  Processing your request.  Please wait.</h1>' }); });
// unblock when ajax activity stops 
$().ajaxStop($.unblockUI); 

var VideoItem = {
	mouseOver : function(item, titleId, controlsId) {
		item.className = 'videoItemHover';
		document.getElementById(titleId).className = 'videoItemTitleHover';
		document.getElementById(controlsId).className = 'videoItemControlsHover';
	},
	
	mouseOut : function(item, titleId, controlsId) {
		item.className = 'videoItem';
		document.getElementById(titleId).className = 'videoItemTitle';
		document.getElementById(controlsId).className = 'videoItemControls';
	},
	
	download : function(url) {
		$('durl').value = url;
		$('df').submit();
	}
}


var loaded = false;
var imer;
function mouseOverImage(name,id,nr){

	if (!loaded) return;
	
	if(name)
		imname = name;
	
		imname.className = 'hover';
		imname.src = "http://img.youtube.com/vi/" + id + "/" + nr + ".jpg";
		nr++;
		if (nr > 3) 
			nr = 1;
		timer = setTimeout("mouseOverImage(false,'" + id + "'," + nr + ");", 1000);
}


function mouseOutImage(name){
	
	if (!loaded) return;

	if(name)
		imname = name;
	//make border back to greyish
	
	//imname.style.border = 	'4px solid #333333';
	imname.className = '';
	if(timer)
		clearTimeout(timer)

}

function getVideoId(url){

	if (!loaded) return;

	var id = false;
	var fIndex = url.indexOf("watch?v=");
	if (fIndex != -1) {
		var str = url.substr(fIndex + 8);
		var lIndex = str.indexOf('&');
		if (lIndex != -1) {
			id = str.substr(0, lIndex);
		}
		else {
			id = str;
		}
	}
	return id;
}

var DirectLink = {
	_last: null,
	
	toggle: function(e) {
		if (this._last == e) {
			this._last.style.display = 'none';
			this._last = null;
			return false;
		}
		
		if (this._last != null) {
			this._last.style.display = 'none';
		}
		
		this._last = e;
		this._last.style.display = 'block';
		return false;
	},
	
	listItemOnMouseOver: function(e) {
	},
	
	listItemOnMouseOut: function(e) {
	}
};

$(document).ready(function(){
    loaded = true;
    var url = $('url');
    if (url && url.focus) url.focus();
    
    
            
        
    $("form").bind("submit", pr);
    $('#url')[0].focus();
});

Shadowbox.loadSkin('classic', 'js/shadowbox/js/skin');
Shadowbox.loadLanguage('en', 'js/shadowbox/js/lang');
Shadowbox.loadPlayer(['flv', 'html', 'iframe', 'img', 'qt', 'swf', 'wmp'], 'js/shadowbox/js/player');
    
window.onload = function(){
Shadowbox.init({ loadingImage: 'images/load.gif' });
}
/*
jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        vertical: true,
        scroll: 2,
        buttonNextHTML: '',
        buttonPrevHTML: '',box/
        auto: 3,
        wrap: 'last',
        initCallback: carousel_initCallback,
        itemLoadCallback: carousel_itemLoadCallback
    });
});
*/
function carousel_initCallback(carousel)
{
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function carousel_itemLoadCallback(carousel, state)
{
    // Since we get all URLs in one file, we simply add all items
    // at once and set the size accordingly.
    if (state != 'init')
        return;

    jQuery.get('carousel.php', function(data) {
        carousel_itemAddCallback(carousel, carousel.first, carousel.last, data);
    });
};

function carousel_itemAddCallback(carousel, first, last, data)
{
    var items = data.split('$');
    for (var i = 0; i < items.length; i++) {
        carousel.add(i+1, items[i]);
    }
    carousel.size(items.length);
    var links = $("a[@rel^='shadowbox']");
       if (links)
            Shadowbox.setup();
};
