function thisMovie(swf) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[swf];
  } else {
    return document[swf];
  }
} 
function loadFile(swf,obj) { 
  thisMovie(swf).loadFile(obj); 
};
var currentSong;
var playerState = 0;
function getUpdate(typ,pr1,pr2,swf) {
	if(typ == 'item') { 
		currentSong = pr1;
	}
	if(typ == 'state') { 
		playerState = pr1;
	}
	if(typ == 'time') { 
		if(pr1 == 1){
			//alert("finished");
			//Load next song
			var obj = thisMovie(swf).itemData(currentSong);
			ajaxConnection('/q_ajax.php?do=song_details&song_id='+obj['id'],'song_details');
		}
	}
	if(typ == 'time') { 
		if(pr2 == 0 && pr1 != 0){
		}
	}
};
function loadRandom(){
loadFile('qmpl', {file:'/playlist.php',autostart:true});
	document.getElementById('playlist_name_div').innerHTML = 'Random Songs';
}
function loadLatest(){
loadFile('qmpl', {file:'/playlist.php?type=latest',autostart:true});
	document.getElementById('playlist_name_div').innerHTML = 'Latest Released Songs';
}
function loadFavorite(){
	loadFile('qmpl', {file:'/playlist.php?playlistid=fav',autostart:true});
	document.getElementById('playlist_name_div').innerHTML = 'Your Favorite Songs';
	updatePlaylist();
}
function getPlayer(gid) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[gid];
	} else {
		return document[gid];
	}
};
function updatePlaylist(){
	ajaxConnection('/q_ajax.php?do=load_playlist&playlist_id=fav','playlist_div');
}

function open_new_window(url)
{
	new_window = window.open(url,'details', 'toolbar=0,menubar=0,dependent=0,status=0,scrollbars=1,width=450,height=400,left=50,top=200');
}
function open_new_window2(url, width, height)
{
	new_window = window.open(url,'details', 'toolbar=0,menubar=0,dependent=0,status=0,scrollbars=0,width='+ width +',height='+ height +',left=150,top=100');
}
function switchAddbox(div, buttonDiv){
	var divToUpdate = document.getElementById(div);
	if(divToUpdate.style.display == "block" || divToUpdate.style.display == "" ){
		divToUpdate.style.display = "none";
		buttonDiv.innerHTML = '<img src="images/icons/bullet_toggle_plus.png" />';
	} else {
		divToUpdate.style.display = "block";
		buttonDiv.innerHTML = '<img src="images/icons/bullet_toggle_minus.png" />';
	}
}
function switchDiv(div){
	var divToUpdate = document.getElementById(div);
	if(divToUpdate.style.display == "block" || divToUpdate.style.display == "" ){
		divToUpdate.style.display = "none";
	} else {
		divToUpdate.style.display = "block";
	}
}
function closediv(div){
document.getElementById(div).innerHTML = "";
}
function ajaxConnection($page,$divName, refresh){
	var divToUpdate=document.getElementById($divName);
	mySelf=this;
	var http=false;var browser=navigator.appName;
	divToUpdate.innerHTML="<div class=\"loading\" ><div>";
	if(browser=="Microsoft Internet Explorer"){
		http=new ActiveXObject("Microsoft.XMLHTTP")
	}else{
		http=new XMLHttpRequest()
	}
	if(http){
		http.open('get',$page);
		http.onreadystatechange=updatePage;
		http.send(null)
	}
	function updatePage(){
		if(http.readyState==4&&http.status==200){
			var response=http.responseText;
			divToUpdate.innerHTML=response + '<div style="clear:both"></div>';
			http=null;
			if(refresh){
				setTimeout(function(){
						window.location.reload();
					},1500);
			}
		}
	}
}

function getArtists(letter, cat){

	ajaxConnection('ajax.php?do=artists_list&letter=' + letter + '&cat=' + cat, 'artists-list');
	$(".category_bar > a").removeClass("selected");
	$(".alphabet_bar > a").removeClass("selected");
	if(letter != ""){
		$(function(){
				
			$(".alphabet_bar").
				find("#" + letter).addClass("selected");
			
		});
	}
	if(cat != ""){
		$(function(){

			$(".category_bar").
				find("#" + cat).addClass("selected");
			
		});
	}
	
}
function confirmDelete(delUrl) {
  if (confirm("Are you sure you want to delete")) {
    document.location = delUrl;
  } 
}
function loadSong(filename, id){

	pheight = 60;
	pdisplayheight = 40; 

	var so = new SWFObject('/flash_player/mediaplayer.swf','song_player','525',pheight,'7');
								so.addParam('allowfullscreen','true');
								so.addParam('allowscriptaccess','always');
								so.addVariable('displayheight',pdisplayheight);
								so.addVariable('file',filename);
								so.addVariable('link','/download.php%3Fsong%3D'+id);
								so.addVariable('height',pheight);
								so.addVariable('width','525');
								//so.addVariable('autoscroll','true');
								so.addVariable('shuffle','false');
								so.addVariable('showeq','true');
								so.addVariable('repeat','list');
								so.addVariable('autostart','true');
								so.addVariable('showdownload','true');
								so.addVariable('backcolor','0xD9D9D9');
								so.addVariable('callback','analytics');
								so.addVariable('javascriptid','song_player');
								so.addVariable('enablejs','true');
								so.write('song_player_div');
									

}
function playlistloaded(obj) { 
		//player.sendEvent("PLAY");
		alert('the new mute state is: '+obj.state); 
	}
	
function loadPlaylist(filename,shuffle,pdisplayheight,autostart){
	var player = getPlayer('qmpl_main');
	player.sendEvent("LOAD",filename);
}
//Yahoo UI

/*
	Detect if the users are using IE6 or older, and tell them to upgrade or start using opera or firefox
	HOW TO USE: all you have to do is link to this js file (shown below), and use conditional comments to run this script if a user is running IE6 or lower
	<!--[if lte IE 6]>
		<script type="text/javascript" src="detect_browser.js"></script>
	<![endif]-->
*/

function detect_browser_hide_warning() {
	document.getElementById('detect_browser_div').style.display = "none";
}

function displayWarning() {
	// IE7 URL
	urls_style = 'color:#333; font-weight:bold; cursor:pointer;';
	ie_btn = '<a style="' + urls_style + '" href="http://www.microsoft.com/windows/downloads/ie/getitnow.mspx" target="_blank" title="Internet Explorer Download Page">click here</a>';
	firefox_btn = '<a style="' + urls_style + '" href="http://www.mozilla.com/firefox/" target="_blank" title="Firefox Download Page">Firefox</a>';
	opera_btn = '<a style="' + urls_style + '" href="http://www.opera.com/download/" target="_blank" title="Opera Download Page">Opera</a>';
	
	message = 'You are using an out-of-date version of Internet Explorer, ' + ie_btn + ' to download the latest version! Better yet, use ' + opera_btn + ' or ' + firefox_btn + '.';
	close_btn = '<a style="' + urls_style + '" onclick="detect_browser_hide_warning()" title="Close this popup">[ close ]</a>';
	
	// stylesheet for the warning message
	warning_style = '';
	warning_style += 'position:absolute;';
	warning_style += 'top:0px;';
	warning_style += 'left:0px;';
	warning_style += 'width:100%;';
	warning_style += 'display:block;';
	warning_style += 'float:left;';
	warning_style += 'font-weight:normal;';
	warning_style += 'background-color:#FFFFE1;';
	warning_style += 'color:#333;';
	warning_style += 'border-bottom:1px solid #333;';
	warning_style += 'padding:7px 0px;';
	warning_style += 'line-height:10px;';
	warning_style += 'font-family:Arial,Verdana,Tahoma;';
	warning_style += 'font-size:10px;';
	warning_style += 'text-align:center;';
	warning_style += 'z-index:999999;';
	
	warning = '<div id="detect_browser_div" style="' + warning_style + '">' + message + '&nbsp;&nbsp;&nbsp;' + close_btn + '</div>';
	
	setBody(warning);
}

function setBody(value) {
	document.body.innerHTML = document.body.innerHTML + value;
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function addToFave(playlistid, songid, artist, album, title, file, albumid, artistid){
	FB_RequireFeatures(["XFBML"], function()
    {
      FB.Facebook.init("e1691ab667fc715c2d5a50f372b88fe8", "xd_receiver.htm");
      //FB.Facebook.get_sessionState().waitUntilReady(function()
      //{
        //window.alert("Session is ready");
		qfb_publishSongStory(title, artist, album , file, albumid, ajaxConnection('/ajax.php?do=add_song2playlist&playlistid=' + playlistid + '&songid=' + songid , 'fav_' + songid , false));
      //});
	  
    });
	
	
}

function addArtistToFave(artist, artistid, file, divtoupdate, fbuserid){
	FB_RequireFeatures(["XFBML"], function()
    {
      FB.Facebook.init("e1691ab667fc715c2d5a50f372b88fe8", "xd_receiver.htm");
		qfb_publishArtistStory(artist, artistid,file, fbuserid, ajaxConnection('/ajax.php?do=add_fan&artist_id=' + artistid, divtoupdate, false));
    });
}
function qfb_publishSongStory(title, artist, album, file, albumid, imageurl, callback){

	var user_message = "Very nice song, check it out on Qeenatha.com.";	
	//FB.Connect.showFeedDialog(57417363060, comment_data, null, null, null, FB.RequireConnect.doNotRequire, callback, "What do you think of the song");
	var attachment = {'media':
						  [{'type':   'mp3',
						   'src':    file, 
						   'title':  title,
						   'artist': artist,
						   'album': album}
						   ]};
	FB.Connect.streamPublish(user_message, attachment, null, null, 'What do you think of the song?');
	
}

function qfb_publishProfileSongStory(title, artist, album, file, albumid, callback){
	var comment_data = {
								"song":title, 
								"artist":artist,
								"album":album,
								"albumid":albumid,
								"mp3":{"src":file, "title":title, "artist":artist, "album":album}
								}; 
	var user_message =  "Very nice song, check it out on Qeenatha.com.";		
	//FB.Connect.showFeedDialog(57416288060, comment_data, null, title + " - " + album, null, FB.RequireConnect.promptConnect, callback, "What do you think of the song", user_message);
	
	var attachment = {'media':
						  [{'type':   'mp3',
						   'src':    file, 
						   'title':  title,
						   'artist': artist,
						   'album': album}]};
	FB.Connect.streamPublish(user_message, attachment, null, null, 'What do you think of the song?');
}
function qfb_publishNewSongStory(title, artist, album, file, albumid, callback){
	var comment_data = {
		"song":title,
		"album":album,
		"albumid":albumid,
		"mp3":{"src":file, "title":title, "artist":artist, "album":album}
		}; 

	var user_message =  "Check out my new song (" + title +") on Qeenatha.com";
	//FB.Connect.showFeedDialog(38708868060, comment_data, null, null, null, FB.RequireConnect.doNotRequire, callback, "What do you think of the song", user_message); 
	var attachment = {'media':
						  [{'type':   'mp3',
						   'src':    file, 
						   'title':  title,
						   'artist': artist,
						   'album': album}]};
	FB.Connect.streamPublish(user_message, attachment, null, null, "What do you think of the song");
	
}

function qfb_publishArtistStory(artist,artistid,file, fbuserid, callback){
	var comment_data = {
						"artist":artist,
						"artistid":artistid,
						"images":[{"src":file, "href":"http://www.qeenatha.com/viewprofile.php?id=" + artistid}]
						}; 
	
	var attachment = {'media':
						  [{'type': 'image',
							'src':  file, 
							'href': "http://www.qeenatha.com/viewprofile.php?id=" + artistid}]};
						   
	
	var user_message = "What do you think of " + artist;
	if(fbuserid != "null")
		FB.Connect.streamPublish('', attachment, null, fbuserid, user_message);
	else 
		FB.Connect.streamPublish('', attachment, null, null, user_message);
}

function qfb_publishNewCommentStory(commenturl, callback, item, comment){
	var comment_data = {
						"commenturl":commenturl, 
						"item":item, 
						"comment":commenturl
						}; 
	var user_message = comment;
	//FB.Connect.showFeedDialog(58319903060, comment_data, null, item, null, FB.RequireConnect.doNotRequire, callback, "Your comments", user_message); 
	FB.Connect.streamPublish(user_message, null, null, null, "Your Comment");
}
function qfb_publishNewAlbumStory(album, albumid, file, callback){
	var comment_data = {
						"album":album,
						"albumid":albumid,
						"images":[{"src":file, "href":"http://www.qeenatha.com/albums.php?albumid=" + albumid}]
						}; 
	var user_message =  "Check out my new album on Qeenatha.com";
	//FB.Connect.showFeedDialog(38706228060, comment_data, null, null, null, FB.RequireConnect.doNotRequire, callback, "What do you think of the song", user_message); 
	var attachment = {
						'name': album,
						'href': 'http://www.qeenatha.com/albums.php?albumid=' + albumid,
						'description': album,
						'media': [{ 'type': 'image', 'src': file, 'href': 'http://www.qeenatha.com/albums.php?albumid=' + albumid}]
						};

	FB.Connect.streamPublish(user_message, attachment, null, null, album);
		
}
function qfb_publishNewVideoStory(title, videoid, callback){
	var comment_data = {
						"title":title,
						"file":"http://www.qeenatha.com/videos.php?videoId=" + videoid + "&queryType=show_video", 
						"video":{ 
						  "video_src":"http://www.youtube.com/v/" + videoid,
						  "preview_img":"http://i.ytimg.com/vi/" + videoid + "/default.jpg" 
							}
						}; 
	var user_message = "Check out my new video on Qeenatha.com";
	//FB.Connect.showFeedDialog(92855663060, comment_data, null, null, null, FB.RequireConnect.doNotRequire, callback, "Comments", user_message); 
		var attachment = {
						'name': title,
						'href':"http://www.qeenatha.com/videos.php?videoId=" + videoid + "&queryType=show_video",
						'description': album,
						'media':
						  [{'type':            'flash',
							'swfsrc':          "http://www.youtube.com/v/" + videoid, 
							'imgsrc':          "http://i.ytimg.com/vi/" + videoid + "/default.jpg"}]
						};

	FB.Connect.streamPublish(user_message, attachment, null, null, title);
	
}
