function log(m){
if( window.console&&window.console.log)
	window.console.log(m);
}

$(document).ready(function(){
	init_thumbs()
	init_recent()

	var t;
	function matchHeights(left, right, bothways) {
		var lefth = parseInt(left.outerHeight());
		var righth = parseInt(right.outerHeight());
		if(righth>lefth) {
			left.height(righth);
		}
		if(bothways && righth<lefth) {
			right.height(lefth);
		}
	}
	
	$("#categories li:first-child").addClass("first-child");
	$("#categories li:last-child").addClass("last-child");
	$("label[for=other_sub_category]").hide();
	
	$("#sub_category").bind("change", function(e) {
		if($(this).val()=="other") {
			$("label[for=other_sub_category]").slideDown(400, function() {
				$("#other_sub_category").focus();
			});
		}
		else {
			$("label[for=other_sub_category]").slideUp();
		}
	});

	$("input[value=http://]").each(function() {
		$(this).bind("click", function() {
			$(this).select();
		})
	});


// login menu
	$("#login_form_li").hide();
	$("#login a").click(function() {
		$("#login_form_li").slideToggle(500, function() {
			if($("#login_form_li:visible").length>0) {
				$("#login a:contains(Logga in)").text("Avbryt");
			} else {
				$("#login a:contains(Avbryt)").text("Logga in");
			}
			$("#username:visible").focus();
		});
	})


	matchHeights($(".content"), $(".secondary_content"));


	$("#movie_player .manuscript").hide();
	var manus_opened = false
	$("#movie_player .interactive_script").click(function(e) {
		$("#movie_player .manuscript").slideToggle();
		if(!manus_opened){
			manus_opened = true;
			$('.interactive_script').html('- St&auml;ng programinneh&aring;ll/interaktivt manus').addClass('close');
		}else{
			manus_opened = false;
			$('.interactive_script').html('+ &Ouml;ppna programinneh&aring;ll/interaktivt manus').removeClass('close');
		}
		
	})
	
});

function init_recent( base ){
	if(base==null)
		base=document;

	$(".pager", base).hide();
	$(".video_scroll", base).each(function() {
		var cycle = $(".videos", this);
		var cycle_id =  "#" + $(this).attr("id");

		if($(cycle_id + " .videos>*", base).length>1) {
			$(cycle_id + " .pager", base).show();
			$(cycle).cycle({
				fx: "fade",
				speed: 100,
				timeout: 0,
				next: cycle_id + " .next",
				prev: cycle_id + " .prev",
				after: function(curr, next, opts) {
					var index = opts.currSlide;
					$(cycle_id + " .total_pages", base).text("av " + opts.slideCount);
					$(cycle_id + " .current_page", base).text(index+1);
					$(cycle_id + ' .prev',base).css("display","block")[index == 0 ? 'hide' : 'show']();
					$(cycle_id + ' .next',base).css("display", "block")[index == opts.slideCount - 1 ? 'hide' : 'show']();
				}
			});
			$(cycle_id + " .first", base).click(function() {$(cycle).cycle(0);return false;})
		}
	})
}

function init_thumbs(){
	$(".thumb").each(function() {
		$(this).hover(function() {
			$(".description",this).stop().fadeTo(300, 1, function(){
				$(this).parent().find('.image').css('background-color', 'black');
			});
		}, function() {
			$(".description",this).stop().fadeTo(500, 0);
			$(".image",this).css('background-color', 'transparent');
		})
	});
}

function favv(id,s){
	if(s) $.post( '/api/json/method/addFavourite/movie/'+id, function(result, status){ }, 'json' );
	else $.post( '/api/json/method/removeFavourite/movie/'+id, function(result, status){ }, 'json' );
	return false;
}

function remove_favv(id, who) {
	$.post( '/api/json/method/removeFavourite/movie/'+id, null, function(res, s) {
		if( s == 'success' && res['status'] == "ok") {
			$(who).parent().slideUp('normal', function(){$(this).remove()} );
		}
	},'json');
}
function remove_movie(id){
	if( confirm('Vill du verkligen ta bort filmen?') ){
		window.location = '/user/delete-movie/id/'+id;
	}
}
function show_most_recent(){
	$("#flip").load('/index/most-recent-partial', {}, function(){ init_recent($('#featured_videos').parent()); });
	$("#mr").addClass('here');
	$("#mv").removeClass('here');
}

function show_most_viewed() {
	$("#flip").load('/index/most-viewed-partial', {}, function(){ init_recent($('#featured_videos').parent()); } );
	$("#mv").addClass('here');
	$("#mr").removeClass('here');

}
