/* var jsReady : Must be set to true on page load, before data can be passed to the Flash object */
var jsReady = false;

/* var variable to indicate the current hovered media */
singstarGlobalVariables = {
	fileId:"",
	fileTypeId:"3",
	permission:"2",
	username:"",
	thumbnail:'#'
} 

/* Array for all the flash players that are ready to be called*/
var flashReady = new Array();

/* The params are the same for all the flash */
var params = {
	menu : "false",
	bgcolor : "#2A180A",
	wmode : "transparent",
	allowScriptAccess : "always"
}

/* The attributes are the same for all the flash */
var attributes = {};

/* function isReady() : Flash calls isReady() on a timer interval at startup before setting up callbacks to javascript */
function isReady(divId) {
	if (jsReady) {
//		alert("flash ready for " + divId + "\nflashReady: " + flashReady[divId]);
		flashReady[divId] = true;
	}
	return jsReady;
};

function unsetFlashReady(divId) {
//	alert("freeze flash for " + divId + "\nflashReady: " + flashReady[divId]);
	if (navigator.appName.indexOf("Microsoft") != -1) {
		freezeFlash(divId);
	} else {
		flashReady[divId]=false;
	}
}


/* function pageInit() : must be fired when the page is fully loaded (onLoad or some other method) */
function pageInit() {
	jsReady = true;
}

/* function getFlashObject() : Gets the Flash element by attribute id or name, or just by the div id in which the Flash object is embedded (We use the div id) */
function getFlashObject(movieName) { 
  var flashObject = null; 
	if (navigator.appName.indexOf("Microsoft") != -1) {
		flashObject =  window[movieName];
	} else {
		flashObject = document[movieName];
	}  
	return flashObject;
}

/* Global variable to check if the disc flash is small or big */
var smallDiscFlash = true;

/* Set the height of the disc flash */
function setCarouselHeight (flashID, value) {
	if(value > 500) {
		smallDiscFlash = false;
	} else {
		smallDiscFlash = true;
	}
	$("#" + flashID).height(value);
	// Added -parent for homepage case, check if this does not cause problems
	$("#" + flashID + "-parent").height(value);
}

function detectBrowserPlatform(){
	return navigator.platform;
}

/* Array for all the flash videos */
var flashVideoHolder = new Array();

/* Array to hold all the flashplayer config */
var flashPlayerConfigHolder = new Array();

/* Play a song */
function playSong(divId, song) {
	if(flashReady[divId] && getFlashObject(divId) != null){
//		alert("playSong called for " + divId + "\nflashReady: " + flashReady[divId]);
		getFlashObject(divId).playSong(song);
	}else{
		setTimeout(function(){playSong(divId, song)}, 250);
	}
}

/* stop playing a song (required for IE to shut up when hiding the div */
function freezeFlash(divId){
	if(flashReady[divId] && getFlashObject(divId) != null){
//	alert('freezeFlash called for ' + divId + "\nflashReady: " + flashReady[divId]);
	 getFlashObject(divId).freezeFlash();
	}
}

function rateSong(id, rate){
	if (isLoggedInMSO() && isLoggedIn() != singstarGlobalVariables.username){
		var params={fileId:id,rating:rate};
		var result = false;
		var status = "200";
		jQuery.ajax({
			type: 'POST',
			url: contextPath+'/ajax/rate',
			dataType: 'text',
			success: function(data) {
				if (data == "") {
					result = true;
				} else {
					var obj = eval('('+data+')');
					if(obj.error == "200"){
						result = true;	
					}
				}
			},
			data : params,
			async: false
		});
		return result;
	}
};

/* Global variable to check which user is loggedIn (changed by wrapper-start on login) */
var loggedInUser = "";
var loggedInMSO = "";

/* Function used by the flash to check if the user is logged in*/
function isLoggedIn() {
	return loggedInUser;
}

function isLoggedInMSO() {
		return loggedInMSO;
};


/* Function for dev purposes to make the flash able to report */
function report($message){
	//alert($message);
}

function setSingstarGlobalVariables(xargs){
	for(var i in xargs){
			singstarGlobalVariables[i] = xargs[i];
	}
	switchReportGriefAndSetPrivacy();
}

//	$("#facebookThumbnailLink").attr("href",singstarGlobalVariables.thumbnail);

function reportGrief(){
	GriefReportUtil.setUpForm(singstarGlobalVariables);
}

function setPrivacy(){
	SetPrivacyUtil.init(singstarGlobalVariables.fileId,singstarGlobalVariables.permission);
}

function isYou(){
	if(loginInfo){
		var userName = loginInfo.psnAccountName;
		if(userName == singstarGlobalVariables.username){
			return true;	
		}
	}
	return false;
}

function switchReportGriefAndSetPrivacy(){
	if(isLoggedIn()){
			if(isYou()){
			$(".singstarreportgrief_button").hide();
			$(".singStarReportGrief_snapshot").hide();
			$(".singstarsetprivary_button").show();
			$(".singStarSetPrivacy_snapshot").show();
			$(".singstarfavourites_button").hide();
		}else{
			$(".singstarreportgrief_button").show();
			$(".singStarReportGrief_snapshot").show();
			$(".singstarsetprivary_button").hide();
			$(".singStarSetPrivacy_snapshot").hide();
			$(".singstarfavourites_button").show();
		}
	}else{
		$(".singstarreportgrief_button").show();
		$(".singStarReportGrief_snapshot").show();
		$(".singstarsetprivary_button").hide();
		$(".singStarSetPrivacy_snapshot").hide();
		$(".singstarfavourites_button").hide();
	}			
}

/* Call pageinit when the page is loaded */	
jQuery(document).ready(function(){
	pageInit();
});
