/**
 * TalentFM Javascript
 * Copyright 2011 TalentFM
 * Alle rechten voorbehouden
 * Created By: Fabian Tack
 */

/* STARTUP */
$(document).ready(function () { 
     
	// Start Now Playing
	GetRemotePlayInfo();
	 
	/* MENU MODULE */
	$('#hdr_Menu li').hover(
		function () {
			
			//s Show the submenu
			$('ul', this).fadeIn(250);
			$(this).addClass("active");
 
		}, 
		function () {
			//hide its submenu
			$('ul', this).fadeOut(250); 
			$(this).removeClass("active");     
		}
	);
	
	/* Banner Start */
	setInterval("StartSlideshow('2000')", 7500);
	
});

/**
 * Open new page
 **/
function openWebPage(value) {

	window.location.href = "/talent/top25/&date="+value;
	
}

/**
 * Radionomy Playlist
 * Now Playing
 * Created By: Radionomy
 * Edited By: Fabian Tack
 **/
/* Remote Information */
function jsonp(url, callback, name, query) {
	if (url.indexOf("?") > -1) { url += "&jsonp="; }
	else { url += "?jsonp="; }

	url += name + "&";

	if (query) { url += encodeURIComponent(query) + "&"; }
	url += new Date().getTime().toString(); // prevent caching

	var script = document.createElement("script");
	script.setAttribute("src", url);
	script.setAttribute("type", "text/javascript");
	var head = document.getElementsByTagName('head')[0];
	if (head) {
		head.appendChild(script);
	}
}

function GetRemotePlayInfo() {
	var reqUrl = "http://api.radionomy.com/currentsong.cfm?radiouid=198a2ff2-da4c-44d8-86a0-d9bef8129ae3&type=json&previous=no&next=no&cover=yes&callmeback=yes&defaultcover=no";
	jsonp(reqUrl, "GetPlayInfo", "GetPlayInfo", ""); //This method will asynchronously call GetRemoteUrl to specify the URL
}

function GetPlayInfo(result) {
	
	// Update value's for social
	var social_Artist = result.tracks.artists;
	var social_Title = result.tracks.title;
	var social_Cover = result.tracks.cover;
	
	if(social_Cover == "") {
		social_Cover = "http://1.bp.blogspot.com/_4pLBX9Qr7gE/SW5FjCboQHI/AAAAAAAAAYc/-iaSZwCOAIw/s320/no_cover.jpg";
	}
	
	// Show on site
	$("#song_pic_ava").attr('src', social_Cover);
	TrimTitle("#SongTitle", social_Title);
	TrimTitle("#SongArtist", social_Artist);
	
	social_Artist = social_Artist.toUpperCase();
	
	if(social_Cover == "") {
		social_Cover = "http://talentfm.be/template/images/logo.png";
	}
	
	// FACEBOOK STATUS UPDATE
	var facebookURL = "/facebook.php?a=song&ar="+social_Artist+"&tl="+social_Title+"&cv="+social_Cover;
	$("#song_soc_face").attr('href', facebookURL);
	
	// TWITTER STATUS UPDATE
	var twitterStatus = escape("#nowplaying @TalentFMbe "+social_Artist+" met "+social_Title);
	var twitterURL = "http://twitter.com/share?url=http://talentfm.be&text="+twitterStatus;
	$("#song_soc_twit").attr('href', twitterURL);
	
	// UPDATE PLAYLIST
	//jQuery("#playlist_LIVE").load("/playlist.php");

	if (result.tracks.callmeback <= 0)
		_pollDelay = 5000;
	else
		_pollDelay = (result.tracks.callmeback * 1);

	restartPoll();
}

//POLLING
var pollId;
function startPoll() { pollId = setInterval(GetRemotePlayInfo, _pollDelay); }
function stopPoll() { if (pollId) clearInterval(pollId); }
function restartPoll() { stopPoll(); startPoll(); }

var dots = "...";
function TrimTitle(div, text) {

	if (text.length > 27) {
		$(div).text(text.slice(0, 25) + dots);
	}
	else {
		$(div).html(text);
	}
	$(div).attr("title", text);
}
/**
 * jQuery Easing
 * Created By: George Smith
 */
$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};
/* Banner */
function StartSlideshow(duration) {

	// Get final duration
	var finalDuration = duration/4;
	
	// Get current position
	var scrollP = $('#bannerHolder').scrollTop();
	var endScroll = $('#bannerHolder')[0].scrollHeight;
	var endScrollP = endScroll - 140;
	var nextScroll = null;
	
	if(scrollP == endScrollP) {
		nextScroll = 0;
	} else {
		nextScroll = scrollP + 140;
	}
	
	// first, DEBUG mode
	$('.banner_subTitle').slideUp(finalDuration, function() {
		$('.banner_Title').slideUp(finalDuration, function() {
			$('#bannerHolder').scrollTo(nextScroll, 1000, {easing:'elasout', queue:true, onAfter:function(){
				$('.banner_Title').slideDown(finalDuration, function() {
					$('.banner_subTitle').slideDown(finalDuration);
				} );
			}});
		});
	});
	
}

/* TWITTER SETUP */
var TweetTimer = "";

function getTweet(action) {
	
  // Get the current position of the box
  var scrollP = $('#overlay').scrollTop();
  var endScroll = $('#overlay')[0].scrollHeight;
  var endScrollP = endScroll - 175;
  var nextScroll = null;
  
  // Set the new position
  if(action == "next") {
	  if(scrollP != endScrollP) {
		$('#overlay').scrollTo('+=55px', 500);
	  }
  } else if(action == "previous") {
	  if(scrollP != 0) {
		$('#overlay').scrollTo('-=55px', 500);
	  }
  }
	
}

function updateTwitterNav() {
  var scrollP = $('#overlay').scrollTop();
  
  // Set the nav menu
  if(scrollP == 0) {
	  $('#twitter_getprevious').hide();
  } else if (scrollP > 0 && scrollP < boxHeight) {
	  $('#twitter_getnext').show();
	  $('#twitter_getprevious').show();
  } else if(scrollP == boxHeight) {
	  $('#twitter_getnext').hide();
  }
}

/* VOTING SCRIPTS */
function getNextStep(data) {
	
	var inc = 'fields';
	
	if(data == "top25") {
		$(".top_add_btn").show();
		// For IE7 or older
		$(".top_add_btn").css('visibility', 'visible');
	}
	
	// Load the URL
	$.ajax({
	   type: 'post',
	   url: '/includes/classes/vote.inc.php',
	   data: ({a:data,d:inc}),
	   success: function(r) {
		   $("#hit_vote_box").fadeOut(500, function() {
		   	   $("#hit_vote_box").html(r); 
		   	   $("#hit_vote_box").fadeIn(500);
		   });
	   }
	});
	
}

function getFinal(data) {
	
	var inc = "send";
	var user = $("#vote_user").val();
	
	// Kijk of alles is ingevuld
	if(data == "hit") {
		
		for(i=0; i<document.HitForm.fld_hit_song.length; i++)
		{
			
			if(document.HitForm.fld_hit_song[i].checked==true) {
				var t = document.HitForm.fld_hit_song[i].value;
			}
			
		}
		
		if(t == 0) {
			
			alert('Oops... Blijkbaar heb je geen favoriet aangeduid! Probeer opnieuw.');
		
		} else {
			
			$.ajax({
				
				type: 'post',
				url: '/includes/classes/vote.inc.php',
				data: ({ a:data,d:inc,id:user,c:t }),
				success: function(r) {
		   			$("#hit_vote_box").fadeOut(500, function() {
		   	  			$("#hit_vote_box").html(r); 
		   	   			$("#hit_vote_box").fadeIn(500);
		   			});
	   			}
				
				
			});
			
		}
		
		
	} else if(data == "top25") {
		
		// Verzamel alles
		var song1 = $("#fld_top_1").val();
		var song2 = $("#fld_top_2").val();
		var song3 = $("#fld_top_3").val();
		var song4 = $("#fld_top_4").val();
		var song5 = $("#fld_top_5").val();
		
		// kijken of alles is ingevuld
		if(song1 == '' || song2 == "" || song3 == "" || song4 == "" || song5 == "") {
			alert('Oops... Blijkbaar zijn niet alle velden ingevuld. Je bent verplicht 5 platen te kiezen!');
		} else {
			$(".top_add_btn").hide();
			// For IE7 or older
			$(".top_add_btn").css('visibility', 'hidden');
			$.ajax({
				
				type: 'post',
				url: '/includes/classes/vote.inc.php',
				data: ({ a:data,d:inc,id:user,c1:song1,c2:song2,c3:song3,c4:song4,c5:song5}),
				success: function(r) {
		   			$("#hit_vote_box").fadeOut(500, function() {
		   	  			$("#hit_vote_box").html(r); 
		   	   			$("#hit_vote_box").fadeIn(500);
		   			});
	   			}
				
				
			});
		}
		
	}
	
}

function putList(song) {
	
	if($("#fld_top_1").val() == "") {
		$("#fld_top_1").val(song);
	} else if($("#fld_top_2").val() == "") {
		$("#fld_top_2").val(song);
	} else if($("#fld_top_3").val() == "") {
		$("#fld_top_3").val(song);
	} else if($("#fld_top_4").val() == "") {
		$("#fld_top_4").val(song);
	} else if($("#fld_top_5").val() == "") {
		$("#fld_top_5").val(song);
	} else {
		alert('Je hebt al 5 platen geselecteerd! Verwijder er eentje om deze plaat toe te voegen');
	}
	
}

function deleteSong(song) {
	
	$("#fld_top_"+song).val("");
	
}

function moveSong(status, begin, end) {
	
	// Verzamel alle informatie
	var song1 = $("#fld_top_"+begin).val();
	var song2 = $("#fld_top_"+end).val();
	
	// nu switchen maar
	$("#fld_top_"+begin).val("");
	$("#fld_top_"+end).val("");
	$("#fld_top_"+begin).val(song2);
	$("#fld_top_"+end).val(song1);
	
}

function getComments(id) {

	$.ajax({
	
		type: "post",
		url: '/includes/classes/comments.php',
		data: ({a:"read",id:id}),
		success: function(r) {
			$("#cmnt_box_holder").html(r);
		}
	
	});

}

function getChatBox(id) {

	$.ajax({
	
		type: "post",
		url: '/includes/classes/comments.php',
		data: ({a:"write",id:id}),
		success: function(r) {
			$("#cmnt_post").fadeOut(500, function() {
				$("#cmnt_box_placer").html(r);
				$("#cmnt_box_placer").fadeIn(500);
			});
		}
	
	});

}

function postComment(id) {

	// Haal alle velden op
	var comment = $("#fld_comment_text").val();
	var source = $("#fld_comment_source").val();
	var userid = $("#fld_comment_usid").val();
	
	if(comment == "") {
	
		alert('Oops... Je hebt geen bericht ingevuld! Probeer opnieuw.');
	
	} else {

		$.ajax({
		
			type: "post",
			url: '/includes/classes/comments.php',
			data: ({a:"writeFinal",id:id,c:comment,source:source,userid:userid}),
			success: function(r) {
				
				$.ajax({
					
					type: "post",
					url: '/includes/classes/comments.php',
					data: ({a:"read",id:id}),
					success: function(r) {
						$("#cmnt_box_holder").html(r);
						var cn_number = parseInt($("#cmt_count_number").html());
						cn_number = cn_number+1;
						$("#cmt_count_number").html(cn_number);
						$("#fld_comment_text").val('');
					}
				
				});
				
			}
		
		});
		
	}

}

/* Open Internet Player */
function openInternetPlayer(url, width)
{	
	var d = new Date();
	window.open(url, 'radiospeler'+d.getTime(), 'resizable=1,toolbar=0,scrollbars=0,location=0,status=0,menubar=0,width='+width+',height=400,left=150,top=150');
}

/** iframe resizer **/

// Safari & Opera fix
<!--
function autoResize(id){
    var newheight;

    if(document.getElementById){
        newheight=document.getElementById(id).contentWindow.document .body.scrollHeight;
    }

    document.getElementById(id).height= (newheight) + "px";
}
//-->
