// ALERT FUNCTION
function alertThis(header,body){
	if(header!==undefined) {
		$headertext = "<span class='header'>"+header+"</span>";
	}else{
		$headertext ="";
	}
	if(body!==undefined) {
		$bodytext = "<span class='mess'>"+body+"</span>";
   	} else {
		$bodytext = "";
	}
	$("body").prepend("<div class='message alert'>"+$headertext+$bodytext+"</div>");
	$(".message").delay(2000).fadeOut(500, function(){$(".message").remove();});
}

$(document).ready(function(){
	
	// Validate submit video form
	$("#addVideoForm").submit(function() {
		$mess = $("#message").val();
		if($("#videoname").val().length<3 || $("#message").val().length<3){
			alertThis("Error","The name and address must be at least 3 characters long.");
			return false;
		}
		if($mess.search(/youtube/i)!=-1 || $mess.search(/break/i)!=-1 || $mess.search(/google/i)!=-1 || $mess.search(/yahoo/i)!=-1 || $mess.search(/current/i)!=-1 || $mess.search(/vimeo/i)!=-1 || $mess.search(/metacafe/i)!=-1){
			
		} else {
			alertThis("Sorry","We don\'t support videos from that provider.");
			return false;
		}
	});

	//$(".aboutrpx").hide();
	$("#toolbar").hover(function(){$(".aboutrpx").fadeIn(200);},function (){$(".aboutrpx").fadeOut();});

	// menu hover - IE6 fix som blev bra fšr alla  
	$("#menu li").hover(function(){$(this).find("ul").fadeIn();},function(){$(this).find("ul").fadeOut();});	
	
	// ADD TAG
	$(".tagAddBtn").click(function(){
		$vid = $(this).prev().attr("title");
		$tagText = $(this).prev().val();
		$this = $(this);
		if($tagText.length<3){
			alertThis("Error","Too short Tag. It must contain at least 3 characters.");
		} else {
			if($this.hasClass("clicked")){
				alertThis("Hold your horses, i'm working!","No need to rush");
			} else {
				$this.addClass("clicked");
				$this.parent().fadeTo(400, 0.4);
				$.ajax({
					type: "POST",
					url: "/akro/edit.php",
					data: "tagText="+$tagText+"&vid="+$vid,
					success: function(msg){
						if (msg=="tagAdd=2") {
							alertThis("Error","That tag already exists");
						} else	if (msg=="tagAdd=3") {
							alertThis("ERROR","Empty field");
						} else if (msg=="tagAdd=4"){
							alertThis("Error","You must be logged in to add tag");
						} else {
							$msg2 = msg.split(",");
							$this.parent().parent().parent().parent().find("ul:first").prepend("\n\t\t\t\t<li><a href='/akro/tag/"+$msg2[3]+"/'>"+$msg2[3]+"</a></li>");
						}
						$this.parent().fadeTo(400, 1);
						$this.removeClass("clicked");
						$this.prev().val("");
					},
					error: function(){
						alert("failure");
						$this.parent().fadeTo(400, 1);
						$this.removeClass("clicked");
					}
				});
			}
		}
		return false;
	});
	
	// LIKE/UNLIKE VID
	$(".like,.unlike").click(function(){
		$vid = $(this).attr("title");
		$this = $(this);
		$thisVidName = $this.parent().parent().parent().find("h2 a").html();
		$.ajax({
			type: "POST",
			url: "/akro/edit.php",
			data: "like="+$vid,
			success: function(msg){
				if (msg=="like=1"){
					$this.addClass("like").removeClass("unlike");
					$("#likeMenu li#likeVid"+$vid).remove();
					alertThis("&raquo;"+$thisVidName+"&laquo;","Is removed from your favourites");
				} else {
					$("#likeMenu").append("\n\t\t\t\t\t<li id='likeVid"+$vid+"'><a href='/akro/vid/"+$vid+"/'>"+$thisVidName+"</a></li>");
					$this.addClass("unlike").removeClass("like");
					alertThis("&raquo;"+$thisVidName+"&laquo;","Is added to your favourites");
				}
			},
			error: function(){
				alert("failure");
			}
		});
	});
	
	
	// REPORT/UNREPORT VID
	$(".report,.unreport").click(function(){
		$vid = $(this).attr("title");
		$this = $(this);
		
		$.ajax({
			type: "POST",
			url: "/akro/edit.php",
			data: "report="+$vid,
			success: function(msg){
				if (msg=="report=1"){
					$this.html("Report").addClass("report").removeClass("unreport");
				} else if (msg=="unreport=1") {
					$this.html("Unreport").addClass("unreport").removeClass("report");
				} else if (msg=="reportall=1"){
					$this.parent().remove(); //html("tabort maj");
				}
			},
			error: function(){
				alert("failure");
			}
		});
	});
	
	
	// ADD/REMOVE CATEGORY
	$(".addCat").click(function(){
		$str = $(this).attr("id");
		$catName = $(this).attr("title");
		$this = $(this);
		$this.parent().fadeTo(200, 0.4);
		$.ajax({
			type: "POST",
			url: "/akro/edit.php",
			data: "addCat=1&id="+$str,
			success: function(msg){
				if (msg=="delete=1"){
					$tmpstr = $this.parent().parent().parent().parent().parent().find(".li_"+$str.substr(3)).remove();
				} else if (msg=="add=1") {
					$this.parent().parent().parent().parent().parent().find("ul:first").prepend("\n<li class='li_"+$str.substring(3)+"'><a href='/akro/cat/"+$catName+"/' class='btn3'>"+$catName.toUpperCase().substring(0,1)+$catName.substring(1)+"</a></li>");
				}
				$this.parent().fadeTo(200, 1);
			},
			error: function(){
				alert("failure");
				$this.parent().fadeTo(200, 1);
			}
		});
	});

	// SHOW/HIDE
	$(".showHide").click(function(){
		$target = $(this).attr("href");
		if($($target).is(':visible')){
			$($target).slideUp();
		} else {
			$($target).slideDown();
		}
		return false;
	});
	
	// RATE 
	//copyright 2008 Jarrett Vance
	//http://jvance.com
	$.fn.rater = function(options) {
		var opts = $.extend({}, $.fn.rater.defaults, options);
		return this.each(function() {
			var $this = $(this);
			var $on = $this.find('.ui-rater-starsOn');
			var $off = $this.find('.ui-rater-starsOff');
			opts.size = $on.height();
			if (opts.rating == undefined) opts.rating = $on.width() / opts.size;
			if (opts.id == undefined) opts.id = $this.attr('id');

			$off.mousemove(function(e) {
				var left = e.clientX - $off.offset().left;
				var width = $off.width() - ($off.width() - left);
				width = Math.ceil(width / (opts.size / opts.step)) * opts.size / opts.step;
				$on.width(width);
			}).hover(function(e) { $on.addClass('ui-rater-starsHover'); }, function(e) {
				$on.removeClass('ui-rater-starsHover'); $on.width(opts.rating * opts.size);
			}).click(function(e) {
				var r = Math.round($on.width() / $off.width() * (opts.units * opts.step)) / opts.step;
				$off.unbind('click').unbind('mousemove').unbind('mouseenter').unbind('mouseleave');
				$off.css('cursor', 'default'); $on.css('cursor', 'default');
				$.fn.rater.rate($this, opts, r);
			}).css('cursor', 'pointer'); $on.css('cursor', 'pointer');
		});
	};

	$.fn.rater.defaults = {
		postHref: location.href,
		units: 5,
		step: 1
	};

	$.fn.rater.rate = function($this, opts, rating) {
		var $on = $this.find('.ui-rater-starsOn');
		var $off = $this.find('.ui-rater-starsOff');
		$off.fadeTo(600, 0.4, function() {
			$.ajax({
				url: opts.postHref,
				type: "POST",
				data: 'id=' + opts.id + '&rating=' + rating,
				complete: function(req) {
					if (req.status == 200) { //success
						opts.rating = parseFloat(req.responseText);
						$off.fadeTo(600, 0.1, function() {
							$on.removeClass('ui-rater-starsHover').width(opts.rating * opts.size);
							var $count = $this.find('.ui-rater-rateCount');
							$count.text(parseInt($count.text()) + 1);
							$this.find('.ui-rater-rating').text(opts.rating.toFixed(1));
							$off.fadeTo(600, 1);
							$this.attr('title', 'Your rating: ' + rating.toFixed(1));
						});
					} else { //failure
						alert(req.responseText);
						$on.removeClass('ui-rater-starsHover').width(opts.rating * opts.size);
						$this.rater(opts);
						$off.fadeTo(2200, 1);
					}
				}
			});
		});
	};


	// COMMENT ADD
	$(".addComment").click(function(){
		$vid = $(this).prev().val();
		$commentText = $(this).prev().prev().val();
		$this = $(this);
		
		if($commentText.length<3){
			alertThis("Error","Too short Comment. It must contain at least 3 characters.");
		} else {
			
			if($this.hasClass("clicked")){
				alertThis("Hold your horses, i'm working!","No need to rush.");
			} else {
				$this.addClass("clicked");
				$this.parent().fadeTo(400, 0.4);
				$.ajax({
					type: "POST",
					url: "/akro/edit.php",
					data: "addComment=add&video="+$vid+"&mess="+$commentText,
					success: function(msg){
						if (msg=="commentAdd=2") {
							alert("ERROR empty field");
						} else if (msg=="commentAdd=3"){
							alert("You must be logged in to add a comment");
						} else {
							$("#commentAdd"+$vid+" ul:first").prepend("\n\t\t\t\t<li><span class='icon btn2 date right'>Today</span>"+$commentText+"</li>");
							$antal = parseInt($("#commentVideo"+$vid+" p a span").html());
							$("#commentVideo"+$vid+" p a span").html($antal+1);
						}
						alertThis("Your comment was added");

						$this.parent().fadeTo(400, 1);
						$this.removeClass("clicked");
						$this.prev().prev().val("");
					},
					error: function(){
						alert("failure");
						$this.parent().fadeTo(400, 1);
						$this.removeClass("clicked");
					}
				});
			}
		}
		return false;
	});
	
	

	
});


