function videoAjax(type) {
	if (type == 'add' || type == 'remove') {
		var vidID = $('div.video_fav a').attr('rel');
		$.ajax({
			type: 'post',
			url: 'system/core/videos/videos.ajaxpost.php',
			data: { video: vidID, vtype: type},
			beforeSend:  function () {
				$('div.video_fav').html('<img src="/skins/bfhq/img/ajax-loader.gif" alt="loading">');
			},
			error: function (data) {
				alert('Ett fel inträffades: '+data);
				$('div.video_fav').html('Fel inträffadess');
			},
			success: function (data) {
				var favtxt = (type == 'add' ? 'sparad!' : 'raderad!');
				$('div.video_fav').html('Favorit '+favtxt);
			}
		});
	}
}

function adminAjax() {
	var sel = $('div.video_admin_watch select option:selected');
	var id = $('div.video_admin_watch span#loader').attr('class');
	
	$.ajax({
		type: 'post',
		url: 'system/core/videos/videos.ajaxadmin.php',
		data: { cat: sel.val(), vid: id },
		beforeSend: function() {
			$('div.video_admin_watch span#loader').html('<img src="/skins/bfhq/img/ajax-loader.gif" alt="loading">');
		},
		error: function(data) {
			alert('Fel inträffades: '+data);
			$('div.video_admin_watch span#loader').html('Fel inträffadess');
		},
		success: function (data) {
			$('div.video_admin_watch span#loader').html('Kategori uppdaterad');
			$('span#vid_cat').html(sel.text());
		}
	});
}

function voteAjax(id) {
	if (id) {
		var watch = $('div.video_vote span#vote_loader').attr('class');
		$.ajax({
			type: 'post',
			url: 'system/core/videos/videos.ajaxvote.php',
			data: { vote: id, wid: watch },
			beforeSend: function() {
				$('div.video_vote').html('<img src="/skins/bfhq/img/ajax-loader.gif" alt="loading">');
			},
			error: function (data) {
				$('div.video_vote').html('Fel inträffadess');
				alert('Fel inträffades: '+data);
			}, 
			success: function (data) {
				if (data != 'fail') {
					$('div.video_vote').html(data);
				}
			}
		});
	}
}

$(document).ready(function(){
	$('div.video_desc a#openAdmin').click(function(event){
		$('div.video_admin_watch').toggle();
	});
	
	var starstring = new Array();
				
	for (nid = 1; nid <= 5; nid++) {
		starstring[nid] = $('div.video_vote img#star'+nid).attr('src');
	}
	
	$('img.video_star')
		.css({'cursor' : 'pointer'})
		.attr('title', 'Klicka för att rösta')
		.hover(
			function(){
				var id = $(this).attr('alt');
				for (sid = 1; sid <= id; sid++) {
					$('div.video_vote img#star'+sid).attr('src', '/system/core/videos/img/star_h.gif');
				}
			},
			function(){
				for (oid = 1; oid <= 5; oid++) {
					$('div.video_vote img#star'+oid).attr('src', starstring[oid]);
				}
			}
		)
		.click(function(event){
			voteAjax($(this).attr('alt'));
		});
	
	$('div.video_admin_watch input#save').click(function(){
		adminAjax();
	});
	
	$('div.video_fav a#v_add').click(function(event){
		videoAjax('add');
	});
	
	$('div.video_fav a#v_remove').click(function(event){
		videoAjax('remove');
	});
});