// WPP Player Scripts 


// Frame Check
if(parent.frames.length<2){
	var uri = document.location.toString();
	//if(uri.indexOf('/player/')==-1){
		document.location.href = "/?redirect="+document.location.pathname+window.location.search;
	//}
}


// Player Interface functions
function getFlashMovie(movieName) {
  var ret = false;
  if(parent.playerFRM){
	  var isIE = navigator.appName.indexOf("Microsoft") != -1;
	  if(isIE){
		ret = parent.playerFRM.window[movieName];
	  } else {
		ret = parent.playerFRM.document[movieName];
	  }
  }
  return ret;
}

function toPlayer(str){
	getFlashMovie('playeri').sentToPlayer(str);	
}

function playSong(id){
	getFlashMovie('playeri').playSong(id);
}

function playRelease(release_id){
	getFlashMovie('playeri').playRelease(release_id);
}

function addToCartFRM(id){
	parent.WPPmainFrame.addToCart(id);
}

function setSongInfoFRM(str){
	parent.WPPmainFrame.setSongInfo(str);
}

function parseXml(xml) {
  if (jQuery.browser.msie) {
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.loadXML(xml);
	xml = xmlDoc;
  }
  return xml;
}

function setSongInfo(strXml){
	
	var str = parseXml(strXml);  
	//alert($('mixname',str).text());
	var artist = $('artist',str).text();
	var title = $('tracktitle',str).text();
	var mixname = $('mixname',str).text();
	var price = $('price',str).text();
	var runningtime = $('playtime',str).text();
	
	var htmlstr = "<strong>"+title+"</strong>";
	
	if(mixname!="") htmlstr += " ("+mixname+")";
	htmlstr    += " - "+artist;
	htmlstr    += "&nbsp;&nbsp;"+runningtime;
	htmlstr    += "&nbsp;&nbsp;"+price;
	
	
	
	$('#plSonginfo').html(htmlstr);
}

function updatePlaylist(){
	$('#playlist').load('/ajax/getPlaylist/');
}

function updatePlaylistFRM(){
	parent.WPPmainFrame.updatePlaylist();
}

function PlSetActiveTrack(id){
	$('#playlist li').each(function(){
		if(this.id=='plid_'+id){
			this.className = 'active';
		} else {
			this.className = '';
		}
	});
}

function PlSetActiveTrackFRM(id){
	parent.WPPmainFrame.PlSetActiveTrack(id);
}

function deleteFromPlaylist(){
	var a = new Array();
	$('#playlist > li .col_1 input').each(function(){
		if(this.checked==true){
			var plid_a = jQuery(this).parent().parent().attr('id').split('_');
			var plid = plid_a[1];
			a.push(plid);
		}
	});
	if(a.length>0){
		$('#playlist').load('/ajax/delPlaylist/', { ids: a.join('|')}, function(){updatePlayerPlaylist()});
	}
}

function wpplayerFromPlaylist(){
	var a = new Array();
	$('#playlist > li .col_1 input').each(function(){
		if(this.checked==true){
			var plid_a = jQuery(this).parent().parent().attr('id').split('_');
			var plid = plid_a[1];
			a.push(plid);
		}
	});
	if(a.length>0){
		$.post('/ajax/createWPPlayer/', { ids: a.join('|')}, function(res){
			if(res=="false"){
				
			} else {
				var win = window.open('/whatpeopleplayer/showExternal.php?p='+res, 'external_wpp_player', 'scroll=no, resize=no, width=457, height=614');
				if(win==undefined){
					alert("Please disable your PopUp Blocker for using this Feature.");
				} else {
					win.focus();
				}
			}
		});
	}
}

function updatePlayerPlaylist(){
	getFlashMovie('playeri').updatePlaylist();	
}

function addToPlaylist(){
	var rel = new Array();
	var trx = new Array();
	var typ = "";
	for(i=0;i<arguments.length;i++){
		var item = arguments[i].toString();
		if(item.charAt(0)=='a'){
			rel.push(item.substr(1));
			typ = "Album was";
		} else {
			trx.push(item);
			typ = "Track was";
		}
	}
	if(arguments.length>1) typ = "Items were"
	$.post('/player/addToPlaylist/', {tracks:trx.join('|'),releases:rel.join('|')}, function(data){
		updatePlaylist();
		updatePlayerPlaylist();
		showMessage("The "+typ+" added to your Playlist", 3000);
	});	
}


// Playlist

var PlayList = function(){
	
	var self = this;
	this.pl_container = document.getElementById('playlist_sc');
	this.actions      = document.getElementById('pl_actions');
	this.toggle_btn   = document.getElementById('plToggle');
	this.pl_footer    = document.getElementById('playlistCont');
	this.plCont       = document.getElementById('listing');
	this.isIE         = (navigator.appName.indexOf("Explorer") == -1)?false:true;
	this.isOpen       = false;
	this.easeTime	  = 100;
	
	
	
	this.init = function(){
		self.toggle_btn.onclick = self.toggle;
		if(screen.availWidth<1035){
			var listpos = 634 - (1035 - screen.availWidth);
			document.getElementById('listing').style.left = listpos+"px";
		}
	}
	
	this.toggle = function(){
		if(self.isOpen){
			self.close();
		} else {
			self.open();
		}
	}
	
	this.open = function(){
		self.isOpen = true;
		$('#listing').animate({top:'-373px'},self.easeTime);
		this.pl_container.style.display='block';
		this.actions.style.display='block';
	}
	
	this.close = function(){
		self.isOpen = false;
		$('#listing').animate({top:'0px'},self.easeTime);
	}
	
	
	
	
	this.getWinHeight = function() {
		  if( typeof( window.innerWidth ) == 'number' ) {
		    //Non-IE
		    return window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		    return document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
		    return document.body.clientHeight;
		  }
	}	
	self.init();
}

function setSongInfoFromPlayer(){
	if(getFlashMovie('playeri')){
		if(typeof getFlashMovie('playeri').setSongInfoHTML != 'undefined'){
			getFlashMovie('playeri').setSongInfoHTML();
		}
		
	}
}

function plchall(){
	var chstatus = $('#plchallcbox').is(':checked'); 
	$('#playlist input[type=checkbox]').each(
		function(){
			if(chstatus){
				this.checked = true;
			} else {
				this.checked = false;
			}
		}
	);
	return false;
}

/*
function wpplayerFromPlaylist(){
	$('#playlist input[type=checkbox]').each(
		function(){
			if(this.checked){
				alert(this.id);
			}
		}
	);
}*/




/*
 * 
 * 
 *  	OLD FUNCTIONS
 * 
 * 
 */
function posPL(){
	//alert("pos");
	//parent.playerFRM.document.getElementById('trace').innerHTML = parseInt(parent.playerFRM.document.getElementById('trace').innerHTML) + 1;
	var arrayPageScroll = parent.mainFRM.document.viewport.getScrollOffsets();
	parent.mainFRM.document.getElementById('playlist').style.top = arrayPageScroll[1] + parent.mainFRM.document.viewport.getHeight() - 200+'px';
	if(parent.WPPmainFrame.document.getElementById('playlist').style.display == "block"){
		parent.WPPmainFrame.document.plp_to = setTimeout(posPL, 50);
	}
}


// Positioning
function getPageSize() {
		
	 var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}