<!-- 
$(document).ready(function() {


	// Standaardwaarden
	$("#clipchannel_artist").attr("defaultVar", $("#clipchannel_artist").val());
	$("#clipchannel_song").attr("defaultVar", $("#clipchannel_song").val());	

	// Formuliervelden
	// Focus
	$("#clipchannel_artist, #clipchannel_song").focus(function() {
		checkInputs();
		if($(this).val() == $(this).attr("defaultVar")) {												   
			$(this).val("");												   
		}
	});
	// Keyup
	/*
	$("#clipchannel_artist, #clipchannel_song").keyup(function() {

	});
	*/

	// Kijken of de invoervelden leeg zijn
	function checkInputs() {
		$("#clipchannel_artist, #clipchannel_song").each(function() {
			if($(this).val() == "") {
				$(this).val($(this).attr("defaultVar"));
			}
		});
	}
		


	// Formulier niet versturen
	$("#clipchannel_form :submit").click(function() { return false; });
	// Formulier afhandelen
	$("#clipchannel_form :submit").click(function() { 

		// Loading weergeven
		$("#clipchannel_video .video").html("Loading...");

		// Vorige hits verwijderen
		$("#clipchannel_video .caption").html("");
		
		// Keywords opbouwen
		var keywords = createArtistSong();
		findYouTube(keywords, 10)		
		
	});
	
	// Artiest en song mooi weergeven
	function createArtistSong() {
		
		return $("#clipchannel_artist").val() + " - " + $("#clipchannel_song").val();
	}
	
	// YouTube filmpje weergeven
	function composeYouTube(movie, width, height) {
		
		var defaultUrl = "http://www.youtube.com/v/";
		var movieUrl = defaultUrl + movie;
		
		return '<object type="application/x-shockwave-flash" data="' + movieUrl + '" width="' + width + '" height="' + height + '"><param name="movie" value="' + movieUrl + '" /><param name="FlashVars" value="playerMode=embedded" /><param name="wmode" value="transparent" /></object>';
	
	}
	
	// Zoeken op filmpje
	function findYouTube(keywords, results) {
		
		$("head [title=youtubeFix]").remove();		

		// Instellingen en begin workaround
		var url = "http://gdata.youtube.com/feeds/videos?alt=json-in-script&vq=" + keywords + "&max-results=" + results;
		var script  = document.createElement('script');
		script.type = 'text/javascript';
		script.src  = url;
		script.title  = "youtubeFix";
		document.documentElement.firstChild.appendChild(script);		
			
		// JSON van YouTube workaround
		gdata = { io: { handleScriptLoaded: function(data) { 
		
			var matches = new Array();
			var matchLinks = new Array();
			
			if(data.feed.entry != undefined) {
						
				$.each(data.feed.entry, function(i, item) {
	
					// URL van de video opzoeken
					/* Bestaat niet altijd
					var url = item.media$group.media$content[0].url;
					*/
				
					for( var k = 0; k <item.link.length; k++ )  {
						if( item.link[k].rel == 'alternate' ) {
							var url = item.link[k].href;
							var urlArray = url.split("=");
							break;
						}
					}
					
					matches[i] = urlArray[1];
					matchLinks[i] = '<img class="clipchannel_switch" src="' + item.media$group.media$thumbnail[1].url + '" alt="' + urlArray[1] + '" title="' + urlArray[1] + '" />';
					
				});
				
				// Video plaatsen
				placeYouTube(matches[0]);
				
				// Hits weergeven
				$("#clipchannel_video .caption").html(matchLinks.join(" "));
	
				// Links actief maken
				$(".clipchannel_switch").click(function() {
					placeYouTube(this.alt);
				});
				
				$(".clipchannel_switch").hover(function() {
					$(this).addClass("clipchannel_hover");
				},
				function() {
					$(this).removeClass("clipchannel_hover");
				});
				
				showAddToCliplist();
	
			}
			else {
				$("#clipchannel_video .video").html("Geen video's gevonden :(!");
			}
			
		} } }; 
		
	}

	// Klikken op de toevoegen-knop
	$("#clipchannel_video .save").click(function() {
		
		if (confirm("'" + createArtistSong() + "' wordt toegevoegd.\n\nBevestig om door te gaan.\n\n")) { 
			save($("#clipchannel_artist").val(), $("#clipchannel_song").val(), $("#clipchannel_youtube").val());
		}
		
	});
	
	// Klikken op de refresh-knop
	$("#clipchannel_listrefresh").click(function() {
					
		// Clips opnieuw inladen
		loadClips();
		return false;
	
	});
	
	// Entry opslaan
	function save(artist, song, youtube) {
		
		$.ajax({
			dataType: 'jsonp',
			data: { "artist":artist, "song":song, "youtube":youtube, "category":"90s08" },
			jsonp: 'jsonp_callback',
			url: 'http://live.3fm.nl/jsAPI/api.php?m=clipchannel.Store',
			success: function (data) {
				alert(data.resultaat.message);
				hideAddToCliplist();
				clearInput();
				loadClips();
			}
		});
		
	}

	// Opslaan-knop weergeven
	function showAddToCliplist() {
	
		$("#clipchannel_video .save").parent().show();	
	
	}
	
	// Opslaan-knop vergbergen
	function hideAddToCliplist() {

		$("#clipchannel_video .save").parent().hide();	
	
	}
	
	// Invoervelden leegmaken
	function clearInput() {
		
		$("#clipchannel_artist").val("");
		$("#clipchannel_song").val("");
		$("#clipchannel_youtube").val("");
	
	}
	
	// Filmpje op de pagina plaatsen
	function placeYouTube(movie, clean) {
		if(clean == true) {
			// Knopje opslaan verwijderen
			hideAddToCliplist();
			// Vorige hits verwijderen
			$("#clipchannel_video .caption").html("");
		}
		$("#clipchannel_video .video").html(composeYouTube(movie, $("#clipchannel_video .video").width(), $("#clipchannel_video .video").height()));
		$("#clipchannel_youtube").val(movie);
	}

	// Clips in de cliplist zetten
	function loadClips(firstRun) {
		
		$.ajax({
			dataType: 'jsonp',
			data: '',
			jsonp: 'jsonp_callback',
			url: 'http://live.3fm.nl/jsAPI/api.php?m=clipchannel.Retrieve',
			success: function (data) {
				
				// Veld leegmaken
				$("#clipchannel_list").html("");
				
				$.each(data.items, function(i, item) {
	
					$("#clipchannel_list").append('<ul class="item" title="' + item.youtube + '"><li class="artist" style="padding-left: 15px;">' + item.artist + '</li><li class="song" style="padding-left: 15px;">' + item.song + '</li></ul><br />');
					
				});
	
				if(firstRun == true) {
					//nieuwe manier van inladen (een harde film url) voor 90s09, kan weg als 90s09 voorbij is.
					placeYouTube("Cs7-ZgsKV7U", true);

					//oude manier van inladen
					placeYouTube($("#clipchannel_list .item")[0].title, true);
					
				}
	
				attachItemClicks();
	
			}
		});
		
	}
	loadClips(true);

	// Clicks aan items attachen
	function attachItemClicks() {
		
		$("#clipchannel_list .item").click(function() {
			
			// Filmpje inladen
			placeYouTube(this.title, true);

			// View toevoegen
			$.ajax({
				dataType: 'jsonp',
				data: { "youtube":this.title },
				jsonp: 'jsonp_callback',
				url: 'http://live.3fm.nl/jsAPI/api.php?m=clipchannel.updateViews',
				success: function (data) {
				}
			});
			
		});
		
	}
	
});	
// -->