$(function(){
	$('#PlaylistAlani').load('login/playlist.php');
	sarkiAdiSil();
	$("#paylas").fancybox({'onComplete': function(){$('#shareLink').focus().select()}});
	$('#kayit').click( kayit );
	hash();
	$(window).bind( 'hashchange', function(e) { hash(); });
	$('#FormSearch').submit(function(){ 
		var aKelime = $('#s').attr('value');
		hashDegistirArama(aKelime); 
	});
	
	$("#s").autocomplete("login/aramalistesi.php", {
        width: 260,
        matchContains: true,
        selectFirst: false,
		minChars: 3
    });
	
		//Login Panel
	$('#loginPanel').hide();  
	$('#login').toggle(PGoster,PGizle);
	
	
	// Aramadaki cümleyi silme
	$('#s').click(function(){ $('#s').attr("value" ,""); });
	$('#btnPlaylist').toggle(function(){$('.subpanel').show(); $('#playlistAlani').show();},function(){$('.subpanel').hide(); $('#playlistAlani').hide();});

	$('.socialNetworks a').css({opacity: 0.5});
	$('.socialNetworks').find('a').hover(function(){$(this).stop().css({opacity: 1});},function(){$(this).stop().css({opacity: 0.5});});
});

function ekle(){

if( document.getElementById('ykullanici').value.length < 1 ) {		
	$('#ykullanici').css({"background": "url(images/yanlis.gif) right center no-repeat #FFF"});
	return false;
}else{
	$('#ykullanici').css({"background": "url(images/dogru.gif) right center no-repeat #FFF"});
}


if( document.getElementById('yparola').value.length < 1 ) {
	$('#yparola').css({"background": "url(images/yanlis.gif) right center no-repeat #FFF"});
	return false;
}else{
	$('#yparola').css({"background": "url(images/dogru.gif) right center no-repeat #FFF"});
}

	var a = "ykullanici="+document.getElementById('ykullanici').value;
	var b = "yparola="+document.getElementById('yparola').value;
	var c = "ip="+document.getElementById('ip').value;
	var sc = a+"&"+b+"&"+c;
	
	$('#h1giris').html('Üye Girişi <img height="14" width="14" src="images/loadingAnimation.gif" />');
	$.ajax({
		type: "POST",
		url: "login/kontrol.php",
		data: sc,
		success: function(cevap){
		if (cevap=="TRUE")
			{
			setTimeout("window.location = '';",0);
			}
		else
			{
			$.jGrowl(cevap, { life: 3000 });
			}
	   }
	});
}

function kayit(){

if(document.getElementById('uyeadi').value.length < 1 ) {		
	$('#uyeadi').css({"background": "url(images/yanlis.gif) right center no-repeat #FFF"});
	return false;
}else{
	$('#uyeadi').css({"background": "url(images/dogru.gif) right center no-repeat #FFF"});
}


if(document.getElementById('pass').value.length < 1 ) {		
	$('#pass').css({"background": "url(images/yanlis.gif) right center no-repeat #FFF"});
	return false;
}else{
	$('#pass').css({"background": "url(images/dogru.gif) right center no-repeat #FFF"});
}


if(document.getElementById('email').value.length < 1 ) {		
	$('#email').css({"background": "url(images/yanlis.gif) right center no-repeat #FFF"});
	return false;
}else if(document.getElementById('email').value.indexOf('@') == -1){
	$('#email').css({"background": "url(images/yanlis.gif) right center no-repeat #FFF"});
	return false;
}else{
	$('#email').css({"background": "url(images/dogru.gif) right center no-repeat #FFF"});
}

	var a = "uyeadi="+document.getElementById('uyeadi').value;
	var b = "parola="+document.getElementById('pass').value;
	var c = "email="+document.getElementById('email').value;
	var sc = a+"&"+b+"&"+c;
	
	$('#h1uye').html('Hemen üye olun! <img height="14" width="14" src="images/loadingAnimation.gif" />');
	$.ajax({
		type: "POST",
		url: "login/kayitkontrol.php",
		data: sc,
		success: function(cevap){
		if (cevap=="TRUE")
			{
			$.jGrowl("<img src = 'images/dogru.gif' /> Başarıyla Kayıt Oldunuz!", { life: 3000 });
			$('#uyeadi').val("").css({"background": ""});
			$('#pass').val("").css({"background": ""});
			$('#email').val("").css({"background": ""});
			$('#h1uye').html('Hemen üye olun!');
			}
		else
			{
			$.jGrowl(cevap, { life: 3000 });
			}
	   }
	});

}

function arama(kelime) {
$.ajaxSettings.cache = false;
$('#sonuclar').html('<img src = "images/ajax-loader.gif" />');
//var ara = document.getElementById('s').value;
var ara = kelime;
var yeniAra = encodeURIComponent(ara);

  		$.ajax({
		type: "POST",
		url: "liste.php",
		data: "s=" + yeniAra,
		success: function(cevap){ $('#sonuclar').html(cevap); }
		});
		
		$.ajax({
		type: "POST",
		url: "login/aramaekle.php",
		data: "aKelime=" + kelime
		});
		
  	s=1;
}	   

function sonrakiSayfa() {
//$('#sonuclar').html('<center><img src = "../../images/ajax-loader.gif" /></center>');
var ara = document.getElementById('s').value;
var yeniAra = encodeURIComponent(ara);
s++;
$('#sonuclar').load('liste.php?s=' + yeniAra + '&p=' + s);
}

function oncekiSayfa() {
//$('#sonuclar').html('<center><img src = "../../images/ajax-loader.gif" /></center>');
var ara = document.getElementById('s').value;
var yeniAra = encodeURIComponent(ara);
s--;
$('#sonuclar').load('liste.php?s=' + yeniAra + '&p=' + s);
}

function playerCalistir(id,yer,sira) {
	var videoURL = (window.location.href).split('#');
	videoURL = videoURL[0] + id + '/';
	
	$.get("fetchName.php",{vid: id},function(vName){
	document.title =  'Dinleniyor : ' + vName + ' # muzik.cc Müzik Dinle';
	$("meta[name='description']").attr('content','Online ' + vName + ' Dinle, Online ' + vName + ' İzle, Online ' + vName + ' Klip');
	$("meta[name='keywords']").attr('content','Online ' + vName + ' Dinle, Online ' + vName + ' İzle, Online ' + vName + ' Klip');
	$('#SarkiAlani').html('<p>Dinlenmekte: <b>' + vName + '</b></p>');
	
	$('#shareLink').attr('value',videoURL);
	$('#fbShare').attr('href','http://www.facebook.com/sharer.php?u='+ videoURL + '&t=' + encodeURIComponent(vName));
	$('#twShare').attr('href','http://twitter.com/share?url='+ videoURL +'&text='+ encodeURIComponent(vName));
	$('#ffShare').attr('href','http://www.friendfeed.com/share?url='+videoURL+'&title='+ encodeURIComponent(vName));
	$('#delShare').attr('href','http://del.icio.us/post?url='+videoURL+'&title='+encodeURIComponent(vName));
	$('#diggShare').attr('href','http://digg.com/submit?phase=2&url='+videoURL+'&title='+encodeURIComponent(vName));
	
	switch(yer){
	case 'results': $.post('login/haberekle.php',{'sarkiadi': vName,'sarkikodu': id, 'nereden': 'sonuclar'}); break;
	case 'playlist': $.post('login/haberekle.php',{'sarkiadi': vName,'sarkikodu': id, 'nereden': 'playlist'}); break;	
	}
	
});

switch(yer){
case 'results' :

		jwplayer("PlayerAlani2").setup({
		flashplayer: "jwplayer/player.swf",
		file: "http://www.youtube.com/watch?v=" + id,
		autostart: true,
		controlbar: 'bottom',
		dock: 'true',
		height: 30,
		width: 470,
		skin: "jwskin/skin-modieus/stylish_slim.swf"
		});
	
		

break;
case 'playlist' :

	
	//if ($.browser.mozilla) {
	//	$.post('player.php',{w: id},function(response){$("#PlayerAlani").html(response);});
 	//}else{
		jwplayer("PlayerAlani2").setup({
		flashplayer: "jwplayer/player.swf",
		file: "http://www.youtube.com/watch?v=" + id,
		controlbar: 'bottom',
		dock: 'true',
		autostart: true,
		height: 30,
		width: 470,
		skin: "jwskin/skin-modieus/stylish_slim.swf",
		events: {
			onComplete: function() {
				NextSongInPlaylist(sira);
			}
		}	
		});
	//}

break;	
}

footerGoster();

}

function footerGoster(){ $('#footpanel').fadeIn(400); $('#paylas').show();}
function PGoster(){$('#loginPanel').css({"position":"absolute","z-index":"9998","float":"right","right":"60px","top":"100px"});$('#loginPanel').fadeIn(500)}
function PGizle(){$('#loginPanel').fadeOut(500)}
function sarkiAdiSil(){ $('#SarkiAlani').html(''); }


function playlistEkle(kod,name){
	
$.getJSON("login/playlistekle.php", { 'kod': kod },
function(response){
		if (response.data.durum == "TRUE"){
		bosSil();
		
		var i = $('#PlaylistAlani ul li:last').attr('id');
		if (i == null){ i=0; }else{ var i = i.replace('sarki',''); }
		i++;
		
		var isim = response.data.sarki;
		var isim = isim.slice(0,49);
		
		$('#PlaylistAlani ul').append("<li id='sarki" + i + "' data-vid='"+ kod +"' class = 'sarki'><span class='delete' style='visibility:hidden; width:6px;'><img src='images/delete.png' onClick = \"playlistSil('" + i + "','" + kod + "');\" /></span> <a id='PL" + i + "' onclick=\"javascript:hashDegistir('" + kod + "','p','"+ i +"');\" href=\"javascript:void(0);\" >" + isim + "</a></li>");
		effectVer()

		}else{
		$.jGrowl(response.data.durum, { life: 3000 });	
		}
});
}

function playlistSil(id,kod){
$.ajax({
type: "POST",
url: "login/playlistsil.php",
data: "kod=" + kod,
success: function(cevap) { 
		if (cevap == "TRUE"){
		var sayi = $('#PlaylistAlani ul li').length;
		$('#sarki' + id).remove();
		
		var i = $('#PlaylistAlani ul li:last-child').attr('id');
		if (i == null){ i=0; }else{ var i = i.replace('sarki',''); }
		i--;
		
		if(sayi == 1){
		$('#PlaylistAlani ul').append("<li id = 'bos'><font style = 'color:red; margin-left:11px;'>Listeniz henuz boş.</font></li>");
		}
		
		}else{
		$.jGrowl(cevap, { life: 3000 });
		}
	}
});
}

function bosSil(){
var deger = $('#bos').length;
	if(deger != 0){
	$('#bos').remove();
	}
}


function effectVer(){
	$('.sarkilar li').hover(
	function(){
	$(this).children('a').css({'color' : '#15adff'});
	$(this).children('.delete').css({"visibility" : "visible"});						 
	},function(){
	$(this).children('a').css({'color' : '#000'});
	$(this).children('.delete').css({"visibility" : "hidden"});
	});	
}

function hash(){
	var yer;
	var sira;
	if (document.location.hash != ""){
	var currentHash = document.location.hash;
		if (currentHash.indexOf('arama') != -1){
			if((currentHash).indexOf('/') != -1){
			var currentHash = currentHash.replace('#','');
			var currentHash = currentHash.split('/');
				if(currentHash[0] == 'arama'){
				playlistToIndexArama()
				arama(currentHash[1]);
				$('#s').attr('value',currentHash[1]);
				}
			}
		}else if((currentHash.indexOf('user') != -1)){
			if((currentHash).indexOf('/') != -1){
			var currentHash = currentHash.replace('#','');
			var currentHash = currentHash.split('/');
				if(currentHash[0] == 'user'){
				indexToPlaylist(currentHash[1]);
				}
			}
		}else if((currentHash).indexOf('/') != -1){
		var currentHash = currentHash.replace('#','');
		var currentHash = currentHash.split('/');
			switch(currentHash[1]){
			case 'p': yer = 'playlist'; break;
			case 'r': yer = 'results'; break;
			}
			
			switch(currentHash[2]){
			case '!': sira = ''; break;
			default: sira = currentHash[2]; break;
			}
		playerCalistir(currentHash[0],yer,sira);
		}
	}
}
function hashDegistir(sKodu,yer,sira){
document.location.hash = sKodu + '/' + yer + '/' + sira + '/';
}

function hashDegistirArama(aKelime){
document.location.hash = 'arama/' + aKelime;
}

function haberEkle(sarkikodu,sarkiadi,nereden){
$.post('login/haberekle.php',{'sarkiadi': sarkiadi,'sarkikodu': sarkikodu, 'nereden': nereden});
}

function indexToPlaylist(userid){
$('#FormSearch').fadeOut(500);
$('#sonuclar').fadeOut(500);
$('#logo').html('<img src="images/playlistim.png" width="350" height="86"/>');
$('#sonuclar').empty();
$('#sonuclar').load('login/playlist.php?id='+userid + '&l=index');
$('#sonuclar').fadeIn(500);
}

function playlistToIndex(){
$('#FormSearch:hidden').fadeIn(500);
$('#sonuclar').fadeOut(500);
$('#logo').html('<img src="images/logo.png" width="288" height="86"/>');
$('#sonuclar').empty();
$('#sonuclar').fadeIn(500);
if (document.location.hash != ""){document.location.hash = ""}
}

function playlistToIndexArama(){
$('#FormSearch:hidden').fadeIn(500);
$('#sonuclar').fadeOut(500);
$('#logo').html('<img src="images/logo.png" width="288" height="86"/>');
$('#sonuclar').empty();
$('#sonuclar').fadeIn(500);
}

function indexToUyeArama(){
$('#sonuclar').fadeOut(500);
$('#logo').html('<img src="images/playlistim.png" width="350" height="86"/>');
$('#sonuclar').empty();
$('#sonuclar').load('login/uyearama.php');
$('#sonuclar').fadeIn(500);
}


function NextSongInPlaylist(sira){
	sira++;
	var totalTrack = $('li.sarki').length;
	var nextTrack = $('#sarki' + sira).length;
	var lastTrack = $('li.sarki:last').attr('id').replace('sarki','');
	if (nextTrack == 1){
		if (sira > lastTrack){ sira = 1; }
		var nextVid = $('#sarki'+ sira).data('vid');
		hashDegistir(nextVid,'p',sira);
	}else if(nextTrack == 0){
		if (sira > lastTrack){ sira = 0; }
		NextSongInPlaylist(sira);
	}
}

