// Om ja hoppa till plats
function yesNo(varning, plats) {
	resultat = confirm(varning);
	if (resultat == true) {
		document.location = plats;
	}
}

function DisplayElement( element, display ) {
	getElementById(element).style.display = display;
}

// Visa göm videokategrier på edit article
function showVideoTypes() {
	if( $("#Category").val() == "3" ) {
		$("#VideoTypes").show();
		$("#videotypeID").disabled = false;
	} else {
		$("#VideoTypes").hide();
		$("#videotypeID").disabled = true;
	}
}


// Ladda upp fil genom ajax
function ajaxFileUpload()
{
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});

	$.ajaxFileUpload
	(
		{
			url:'ajax/fileupload.php',
			secureuri:false,
			fileElementId:'fileToUpload',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '') {
						alert(data.error);
					} else {
						
						// Visa uppladdad bild
						if ($("img#PhotoGalleryThumb")) {
							$("#PhotoGalleryContainer h2").text("File uploaded");
							$("img#PhotoGalleryThumb").attr('src', data.thumbnail);
							$("#PhotoGalleryTitle").text(data.title);
							$("#PhotoGalleryContainer").show();
							var NumberOfImages = parseInt($("#PhotoGalleryNoImages").text());
							$("#PhotoGalleryNoImages").text(NumberOfImages + 1);
						}
						
						// Lägg till uppladdad bild till picker
						if ($("div#ImagePicker")) {
							$("div#ImagePicker").show();
							$("div#ImagePicker").append('<img src="'+ data.thumbnail +'" alt="" title="Add to article" style="max-width: 75px; max-height: 75px;" onClick="AddImageToEditor(\''+ data.filename +'\')"/>');
							$("span#ImagePickerNoImages").hide();
						}
						
						// Lägg till uppladdad bild till lista
						if ($("span#ImageGalleryList")) {
							$("span#ImageGalleryList").append('<div class="galleryItem"><a href="photogallery?galleryID='+ data.galleryID +'&iID='+ data.iID +'&galleryDestination='+ data.galleryDestination +'" class="galleryImage"><img src="'+ data.thumbnail +'" alt="" title="'+ data.title +'" /></a></div>');
						}
						
					}
				}
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
	)
	
	return false;
}

// Ta bort fil genom ajax
function ajaxFileDelete(varning, imageID) {
	resultat = confirm(varning);
	if (resultat == true) {
		$.post("ajax/filedelete.php", { iID: imageID },
		  function(data){
			$("#galleryItem"+imageID).fadeOut();
		  });
	}
}


// Lägg till bild i editorn
function AddImageToEditor(image) {
	tinyMCE.execCommand('mceFocus',false,'Content');
	tinyMCE.execCommand('mceInsertContent',false,'&nbsp;');
	tinyMCE.execCommand('mceInsertContent',false,'<img src="'+ image +'"/>');
}

// Rösta på rankings
function Vote(fID,Vote,Category) {
$.get("ajax/vote.php", { fID: fID, Vote: Vote, Category: Category },
  function(data){
  
	// tagit bort
	if (Vote == 0) {
		$("div#"+ fID +" img.rate_up").attr("src", "img/icons/rate_up_grey.png");
		$("div#"+ fID +" img.rate_down").attr("src", "img/icons/rate_down_grey.png");
		$("div#"+ fID +" a.rate_delete").hide();
		UserIsAllowedToVote = 1;
	}
	
	if (data > 0) UserIsAllowedToVote = 1;
	
	if (UserIsAllowedToVote) {
		// röstat upp
		if (Vote == 1) {
			$("div#"+ fID +" img.rate_down").attr("src", "img/icons/rate_down_grey.png");
			$("div#"+ fID +" img.rate_up").attr("src", "img/icons/rate_up_blue.png");
			$("div#"+ fID +" a.rate_delete").show()
		// röstat ner
		} else if (Vote == -1) {
			$("div#"+ fID +" img.rate_up").attr("src", "img/icons/rate_up_grey.png");
			$("div#"+ fID +" img.rate_down").attr("src", "img/icons/rate_down_blue.png");
			$("div#"+ fID +" a.rate_delete").show();
		}
		
		if (data > 0) {
			$(".ListItem"+Category).attr("title", VotesLeft +" "+ Category + ": "+ data +"|"+ VotesLeftText);
		} else {
			$(".ListItem"+Category).attr("title", VotingComplete +"|"+ VotesLeftText);
			UserIsAllowedToVote = 0;
		}
		$('.ListItem'+Category).cluetip({splitTitle: '|', leftOffset: -90});
	}
  });
}

// Check if user is allowed to vote on rankings
function ShowUserIsAllowedToVote(Category, VotesLeftNum) {
	$(document).ready(function(){ 
		$(".ListItem"+Category).attr("title", VotesLeft +" "+ Category + ": "+ VotesLeftNum +"|"+ VotesLeftText);
		$('.ListItem'+Category).cluetip({splitTitle: '|', leftOffset: -90});
	});
}

// Open all external links in new window
function ForumOpenExternalLinksInNewWindow() {
	$(document).ready(function(){
	  $("span.commenttext a[href^=http:]").click(function() {
		window.open(this.href);
		return false;
	  });
	});
}

// Show commenting-form
function ForumShowCommentingForm(resetform) {
	$("#areaQuote").hide();
	if (resetform) {
		$("textarea#Comment").val("");
		$("textarea#Quote").val("");
		$("span#spanQuoteUsername").html("");
		$("input#QuoteUsername").html("");
		$("input#cID").val("");
		ForumSmileyClick("");
	}
    $("div#CommentsForm").slideDown(function() {
        $("textarea#Comment").focus();
    });
}

// Show "Edit comment"-form
function ForumEditComment(cID) {
	commenttext = $("span#originalcommenttext"+cID).text().replace(/-br-/g, "\n");
	smiley = $("img#iconForumHeaderSmiley"+cID).attr("alt");
	$("textarea#Comment").val(commenttext);
	$("input#cID").val(cID);
	ForumSmileyClick( smiley );
	ForumShowCommentingForm(false);
}

// Quote another comment
function ForumQuote(cID, username) {
	originalText = $("#originalcommenttext" + cID).html();
    quoteText = originalText.replace(/{q\suser=\".*\"}.*{\/q}/g, "");
    quoteText = quoteText.replace(/:\s/g, "");
    quoteText = quoteText.replace(/-br-/g, "\n");
	$("span#spanQuoteUsername").html(username);
	$("input#QuoteUsername").val(username);
	$("textarea#Quote").val(quoteText);
	$("input#cID").val("");
	ForumShowCommentingForm(false);
	$("#areaQuote").show();
}

// Add click-event to smileys
function ForumAddSmileyClick() {
	$("span.smileys a").click(function () {
		ForumSmileyClick( $(this).attr( "smiley" ) );
	});
}

// click-event on smileys
function ForumSmileyClick( $selectedsmiley ) {
	$("#Smiley").val( $selectedsmiley );
	if ( $selectedsmiley != "")
		$selectedsmiley = '<img src="img/icons/smileys/'+ $selectedsmiley +'.gif"/>';
	$("#SelectedSmileyImage").html( $selectedsmiley );
}

// Add cluetip-function to all quotes
function ForumAddQuotes(  ) {
	$(document).ready(function(){
		$(".commenttext a.titleQuote").cluetip({splitTitle: '|', dropShadow: false});
	});
}