$(function(){
	$(".tabBox>.tab").click(function(){
		var node = $(this);
		if(!node.hasClass("tabSelected")) {
			var old = node.siblings(".tabSelected");
			old.removeClass("tabSelected");
			old.children()[0].src = "/assets/img/tabs_gray_"+old.children()[0].alt+".png";
			$("#"+old.attr("id")+"_body").hide();
			node.addClass("tabSelected");
			node.children()[0].src = "/assets/img/tabs_"+node.children()[0].alt+".png";
			$("#"+node.attr("id")+"_body").show();
			// Load films via AJAX
			film_type = node.attr("id").split("_")[1];
			target_node = $("#tab_"+film_type+"_body");
			if(!target_node.find(".topVideo")[0])
			{
				$.ajax({
					url:"get_film_list/",
					type:"POST",
					data:{"film_type":film_type},
					success:function(cb)
					{
						$("#tab_"+film_type+"_body").html(cb);
						$("h4.title, a.hover_title, img.hover_title").bind({
							mouseover:function(){
								$(".ui-dialog-titlebar").hide();
								film_rating = $(this).attr("rating");
								stars_template = "";

								if(film_rating != 0)
								{
									i = 0;
									while(i<film_rating)
									{
										stars_template += "<img src='assets/img/star_vote.gif' />";
										i++;
									}

									while(i<5)
									{
										stars_template += "<img src='assets/img/no_votes.gif' />";
										i++;
									}
								}
								else
								{
									i=0;
									while(i<5)
									{
										stars_template += "<img src='assets/img/no_votes.gif' />";
										i++; 
									}
								}

							  $("#hover_node").html("<strong>"+$(this).attr("full_title")+"</strong><br/>"+
													"<strong>Channels:</strong> " + $(this).attr("channels") + "<br/>" +
													"<strong>Rating:</strong> " + stars_template + "<br/>" +
													$(this).attr("full_desc")
												);
							   timer = setTimeout('$("#hover_node").dialog("open")',1500);
							},
							mousemove:function(e){
								 $("#hover_node").dialog("option", "position", {
								    my: "left top",
								    at: "right bottom",
								    of: e,
								    offset: "20 20"
								  });
							},
							mouseout:function(){
								clearTimeout(timer);
								  $("#hover_node").text("");
								  $("#hover_node").dialog("close");
							},

						});
					}
				});
			}
			//
		}
	});
});

// jQuery input box memory
(function($){
	jQuery.fn.inputDecor = function(options) {
		settings = jQuery.extend({
			defaultClass : 'blurDecor',
			changedClass : 'focusDecor'
		},options);
		this.each(function(){
			$(this).data('blurValue',$(this).val())
				   .addClass(settings.defaultClass);
		}).focus(function(){
			if($(this).val()==$(this).data('blurValue'))
			{
				$(this).val("");
			}
			$(this).removeClass(settings.defaultClass)
				   .addClass(settings.changedClass);
		}).blur(function(){
			if($(this).val()==='') {
				$(this).val($(this).data('blurValue'))
					   .addClass(settings.defaultClass)
					   .removeClass(settings.changedClass);
			}
		});
		
	};
})(jQuery);

var fb_id;
function tlg_check_status(callback_fn)
{
	var result;
	FB.getLoginStatus(function(response){
		if(response.session)
		{
			fb_id = response.session.uid;
			result = true;
		}
		else
		{
			result = false;
		}
		callback_fn(result);
	});
	//return result;
}

function verify_status(res)
{
	if(res != logged_in){
		if(res)
		{
			$("#fb_logged_in").dialog("open");
		}
		else
		{
			//console.log("User logged in, not Facebook!");
		}
	}
	setupRatings(res, "bind_click()");
}


function fb()
{
	FB.api("/me",function(c){return c;});
}

function setupRatings(status, callback_fn)
{
	$("input[name='star1']").rating();
	$("input[name='star2']").rating();
	$("input[name='star2']").rating("select",real_vote_count);
	$("input[name='star2']").rating("disable");
	
	if(status)
	{
		$("input[name='star1']").rating("select",user_rating - 1);
	}
	$("#rating_holder").show();
	$("#avg_rating_holder").show();
	eval(callback_fn);
}

(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }

  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);
