$(document).ready(function() {	
	//info popup for featured videos
	var orig_pos = new Array();
	$('.featured.video').hover(function() {
		if( orig_pos[$(this).index('.featured.video')] == undefined ) {
			orig_pos[$(this).index('.featured.video')] = $('.title', this).css('top');
		}
		
		var pos = 0;
		if($(this).hasClass('main')) {
			pos = 130;
		}
		
		$('.title', this).stop(true).animate({'top': pos+'px'}, 500);
	}, function() {
		$('.title', this).stop(true).animate({'top': orig_pos[$(this).index('.featured.video')]}, 500);
	});
	
	//activate tabs for homepage
	$('.tabs a').click(function(event){
		event.preventDefault();
		if( !$(this).hasClass('selectedtab') ) {
			switch_tabs($(this));
		}
	});
	
	$('.fancybox').fancybox({
		'cyclic': true,
		'overlayShow' : false,
		'padding': 0,
		'showNavArrows': false
	});
});


function makeHttpObject() 
{
	try {return new XMLHttpRequest();}
	catch (error) {}

}


function statIpad(video_id) 
{
	var request = makeHttpObject();
	request.open("GET", "http://www.susu.tv/videos/stats/"+video_id, false);
	request.send(null);
}



function switch_tabs(obj) {
	var id = obj.attr("rel");
	
	$('.selectedtab').removeClass("selectedtab");
	obj.addClass("selectedtab");
	
	$('.activetab').stop(true, true).fadeOut(200, function(){
		$('.activetab').removeClass("activetab");
		$('#'+id).addClass("activetab").fadeIn(500);
	});
}
$(document).ready(function() {
	//autocomplete for search box
	var cache = {};
	var lastXhr;
	$('#search').autocomplete({
		minLength: 3,
		source: function(request, response) {
			var term = request.term;
			if (term in cache) {
				response(cache[term]);
				return;
			}

			lastXhr = $.getJSON('/search/results/' + term + '/0/5/ajax', request, function(data, status, xhr) {
				cache[term] = data;
				if(xhr === lastXhr) {
					response(data);
				}
			});
		},
		focus: function(event, ui) {
			//prevent default behaviour
			return false;
		},
		select: function(event, ui) {
			
			return false;
		}
	}).data("autocomplete")._renderItem = function(ul, item) {
		return $("<li></li>")
		.data("item.autocomplete", item)
		.append("<a href=\"/videos/watch/" + item.videoID +"\"><img src=\"" + item.image + "\" style=\"float:left; width:40px; height:25px;\"/>" + item.title + "</a><br>" + item.subtitle)
		.appendTo(ul);
	};
	
	$('#signin').click(function() {
		$('#loginpopup').show();
		$('#user').focus();
	});
	
	$("#loginpopup").click(function(e) {
		if ( $(event.target).closest('#loginform').get(0) == null ) { $("#loginpopup").hide(); }
	});
	
	$('#closelogin').click(function() {
		$('#loginpopup').hide();
	});

	$("#login").click(function() {
		var user = $("input#user").val();
		var pass = $("input#pass").val();
		
		var dataString = 'user='+ user + '&pass=' + pass;
		
		$.ajax({
			type: "GET",
			url: "https://www.susu.org/log-in/",
			data: dataString,
			dataType: "jsonp",
			crossDomain: true,
			success: function(data) {
				if(data.result == 'fail') {
					$('#loginform')	.animate({ left: -25 },25)
									.animate({ left: 0 },25)
									.animate({ left: 25 },25)
									.animate({ left: 0 },25)
									.animate({ left: -25 },25)
									.animate({ left: 0 },25);
			} else {
					$('#loginbox').html(data.user);
					$('#loginpopup').fadeOut();
					var SUSU_ID = ReadCookie('SUSU_ID');
					alert("ID: " + SUSU_ID);
					$('#cookiemonster').html('<iframe src="http://www.susu.org/php/cookie.monster.php?SUSU_ID='+SUSU_ID+'></iframe>');
				}
			}
		});
	});
});

function ReadCookie(cookieName) {
    var theCookie=""+document.cookie;
    var ind=theCookie.indexOf(cookieName);
    if (ind==-1 || cookieName=="") return "";
    var ind1=theCookie.indexOf(';',ind);
    if (ind1==-1) ind1=theCookie.length;
    return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}


