function loadPasswordHelp() {
	$("#popupLayer").load("/profile/includes/password_help.php");	
}

function hooplaStats(page_id,uri,http_referrer) {
	
	var rand = Math.random();
	var screen_resolution = screen.width+'x'+screen.height;
	var browser = $.browser;


	
	$.get("/profile/process/stats.php?page_id=" + page_id + "&uri=" + uri + "&http_referrer=" + http_referrer + "&screen_resolution=" + screen_resolution + "&rand=" + rand, function(data) {
//		alert(data);
	});
}



//SETUP POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup
function loadPopup(script,width,height){
	
	$("#popupLayer").hide();
	$("#backgroundPopup").hide();
	
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var docWidth = $(document).width();
	var docHeight = $(document).height();
	
	//centering & sizing
	$("#popupLayer").css({
		"position": "fixed",
		"top": windowHeight/2-height/2,
		"left": windowWidth/2-width/2,
		"width": width,
		"height": height,
		"background-color": "#ffffff",
		"z-index": "99"
	});
	
	$("#backgroundPopup").css({
		"position": "absolute",
		"background-color": "#000000",
		"height": docHeight,
		"width": docWidth,
		"opacity": "0.7",
		"z-index": "98"
	});	
	
	$("#backgroundPopup").fadeIn("slow");
	$("#popupLayer").fadeIn("slow");
	
	$("#popupLayer").load(script);
	popupStatus = 1;
	
}


//disabling popup
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupLayer").fadeOut("slow");
		popupStatus = 0;
	}
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//CLOSING POPUP
	//Click the x event!
	$("#popupLayerClose").click(function(){
		disablePopup();
	});

	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});



//PASSWORD HELP
function sendPassword() {

		function verify(data) {
			if (data=="1") {
				//success
				var html = "<br /><br /><br /><strong>Check your email</strong><br />We've sent you an email with your login information";
				$("#password_help").html(html);
			}
			else {
				alert(data);	
			}
		}
		
		$.post("/public_1110/process/send_password.php", $("#frmPasswordHelp").serialize(), function(data){ verify(data) } );

}


//FORMS
function submitForm(form_id,submit_id,target_div,msg) {
	
	if ((!submit_id) || submit_id=='') submit_id = "input_form_submit";
	$("#" + submit_id).hide();
	
	function verify(data) {
		if (data=="1") {
			//success
			if (!msg) msg = "<h3>Thank You!</h3><br /><p>Your message has been sent and we will contact you shortly.</p>";
			$("#" + target_div).slideUp("slow",function() {
				$("#" + target_div).html(msg);
				$("#" + target_div).slideDown("slow");
			});
		}
		else {
			alert(data);
			$("#" + submit_id).show();
		}
	}

	$.post("/profile/process/submit_form.php",$("#" + form_id).serialize(), function(data){ verify(data) } );

}



//STORE
function loadStore(account_id) {
	loadStoreNav(account_id);
	loadStoreProducts(account_id);
}

function loadStoreNav(account_id) {
	var rand = Math.random();
	$("#store_nav").load("/profile/includes/store/nav.php?account_id=" + account_id + "&rand=" + rand);	
}

function loadStoreProducts(account_id,cat_id) {
	if(!cat_id) cat_id='';
	var rand = Math.random();
	$("#store_main").load("/profile/includes/store/products.php?account_id=" + account_id + "&cat_id=" + cat_id + "&rand=" + rand);
}

function resizeStore() {
	var wrapper_w = $("#store_wrapper").width();
	var nav_w = ($("#store_nav").width() + 15);
	var main_w = (wrapper_w - nav_w - 50);
	$("#store_main").width(main_w);
}


//SLIDESHOW
var ss_current_slide = 0;
var ss_current_panel = 1;
var ss_arr_filenames = '';
var ss_arr_urls = '';
var ss_arr_headlines = '';
var ss_arr_captions = '';
var ss_slide_count = 0;
var ss_image_path = '';

function createSlideshow(page_id, site_id, filenames, urls, headlines, captions) {
	
	//create arrays
	ss_arr_filenames = filenames.split("*|*");
	ss_arr_urls = urls.split("*|*");
	ss_arr_headlines = headlines.split("*|*");
	ss_arr_captions = captions.split("*|*");
	
	//count filenames
	ss_slide_count = ss_arr_filenames.length;
	
	//define image base path
	ss_image_path = "http://content.thehoopla.com/slideshows/" + site_id + "/" + page_id + "/resized/";
	
	//load first slide
	loadSlide(0,1);
	
	//set timer
	var timer = setInterval( "loadSlide()", 7000 );
	
	$(".zindex_override").css({'zIndex':20});
	
}

function loadSlide(i,panel) {
	
	//default values
	if (!i) i=ss_current_slide;
	if (!panel) panel=ss_current_panel;
	
	var filename = getSlideAttr('filename',i);
	var url = getSlideAttr('url',i);
	var headline = getSlideAttr('headline',i);
	var caption = getSlideAttr('caption',i);

	var str = '<img src="' + ss_image_path + filename + '" />';
	if (url!="") str = '<a href="' + url + '">' + str + '</a>';
	
	if (panel==1) {
		$("#ss_panel1").html(str);
		$("#ss_panel1").fadeIn("slow");
		ss_current_slide = nextSlide(i);
		ss_current_panel = 2;
	}
	if (panel==2) {
		$("#ss_panel2").html(str);
		$("#ss_panel1").fadeOut("slow");
		ss_current_slide = nextSlide(i);
		ss_current_panel = 1;		
	}
	
	$(".ss_zindex_over").css({
		'zIndex':'3'
	});


}

function nextSlide(i) {
	var next = i+1;
	if (next>=ss_slide_count) next=0;
	return next;
}

function getSlideAttr(attr,i) {
	
	var val = '';

	switch (attr) {
		case "filename":
			val = ss_arr_filenames[i];
			break;
		case "url":
			val = ss_arr_urls[i];
			break;
		case "headline":
			val = ss_arr_headlines[i];
			break;
		case "caption":
			val = ss_arr_captions[i];
			break;
	}
	
	return val;
	
}


//MP3s
var FlashHelper =
{
	movieIsLoaded : function (theMovie)
	{
		if (typeof(theMovie) != "undefined") return theMovie.PercentLoaded() == 100;
		else return
		false;
  },

	getMovie : function (movieName)
	{
  	if (navigator.appName.indexOf ("Microsoft") !=-1) return window[movieName];
	  else return document[movieName];
	}
};

function niftyplayer(name)
{
	this.obj = FlashHelper.getMovie(name);

	if (!FlashHelper.movieIsLoaded(this.obj)) return;

	this.play = function () {
		this.obj.TCallLabel('/','play');
	};

	this.stop = function () {
		this.obj.TCallLabel('/','stop');
	};

	this.pause = function () {
		this.obj.TCallLabel('/','pause');
	};

	this.playToggle = function () {
		this.obj.TCallLabel('/','playToggle');
	};

	this.reset = function () {
		this.obj.TCallLabel('/','reset');
	};

	this.load = function (url) {
		this.obj.SetVariable('currentSong', url);
		this.obj.TCallLabel('/','load');
	};

	this.loadAndPlay = function (url) {
		this.load(url);
		this.play();
	};

	this.getState = function () {
		var ps = this.obj.GetVariable('playingState');
		var ls = this.obj.GetVariable('loadingState');

		// returns
		//   'empty' if no file is loaded
		//   'loading' if file is loading
		//   'playing' if user has pressed play AND file has loaded
		//   'stopped' if not empty and file is stopped
		//   'paused' if file is paused
		//   'finished' if file has finished playing
		//   'error' if an error occurred
		if (ps == 'playing')
			if (ls == 'loaded') return ps;
			else return ls;

		if (ps == 'stopped')
			if (ls == 'empty') return ls;
			if (ls == 'error') return ls;
			else return ps;

		return ps;

	};

	this.getPlayingState = function () {
		// returns 'playing', 'paused', 'stopped' or 'finished'
		return this.obj.GetVariable('playingState');
	};

	this.getLoadingState = function () {
		// returns 'empty', 'loading', 'loaded' or 'error'
		return this.obj.GetVariable('loadingState');
	};

	this.registerEvent = function (eventName, action) {
		// eventName is a string with one of the following values: onPlay, onStop, onPause, onError, onSongOver, onBufferingComplete, onBufferingStarted
		// action is a string with the javascript code to run.
		//
		// example: niftyplayer('niftyPlayer1').registerEvent('onPlay', 'alert("playing!")');

		this.obj.SetVariable(eventName, action);
	};

	return this;
}

var current_mp3 = '';
function playMP3(file,id,mobile) {
	
	function play(file,id) {
		if (file==current_mp3) {
			niftyplayer('niftyPlayer1').pause();
			$(".btn_play").removeClass("active");
		}
		else {
			niftyplayer('niftyPlayer1').loadAndPlay(file);
			$(".btn_play").removeClass("active");
			$("#" + id).addClass("active");
		}	
	}

	if (!mobile) mobile="false";
	if (mobile=="true") {
		parent.window.location = file;	
	}
	else {
		//load flash mp3 player	
		play(file,id);
	}
	current_mp3 = file;
}


//SOCIAL NETWORKING
function sn_login() {
	function verify(data) {
		if (data.indexOf("1")==0) {
			//success
			var arr = data.split(":");
			var sn_user_id = arr[1];
			sn_load_index(sn_user_id);
		}
		else {
			$("#sn_login_error").html(data);	
			$("#sn_login_error").slideDown("slow");
		}
	}
	
	$.post("/profile/process/sn/login.php",$("#frmSNLogin").serialize(), function(data) { verify(data) });
}

function sn_logout(site_id,sn_user_id) {
	
	function verify(data) {
		sn_load_login(site_id);
	}
	
	var rand = Math.random();
	$.get("/profile/process/sn/logout.php?site_id=" + site_id + "&sn_user_id=" + sn_user_id + "&rand=" + rand, function(data) { verify(data) } );
}

function sn_load_login(site_id) {
	var rand = Math.random();
	$("#sn_wrapper").load("/profile/includes/sn/login.php?site_id=" + site_id + "&rand=" + rand);
}

function sn_load_create_account(site_id) {
	var rand = Math.random();
	$("#sn_wrapper").load("/profile/includes/sn/includes/create_account.php?site_id=" + site_id + "&rand=" + rand);	
}

function sn_create_account() {

	function verify(data) {
		if (data.indexOf("1")==0) {
			//success
			var arr = data.split(":");
			var sn_user_id = arr[1];
			sn_load_index(sn_user_id);
		}
		else {
			alert(data);
		}
	}
	
	$.post("/profile/process/sn/create_account.php",$("#frmSNCreateAccount").serialize(), function(data) { verify(data) });

}

function sn_update_profile() {
	
	function verify(data) {
		if (data=="1") {
			//success
			alert("Your changes have been saved.");
		}
		else {
			alert(data);
		}
	}
	
	$.post("/profile/process/sn/update_profile.php",$("#frmSNProfile").serialize(), function(data) { verify(data) });
	
}

function sn_load_index(sn_user_id) {
	var rand = Math.random();
	$("#sn_wrapper").load("/profile/includes/sn/index.php?sn_user_id=" + sn_user_id + "&rand=" + rand);
}

function sn_input_message_focus() {
	$("#sn_input_message").css({
		'border-color':'#8f8f8f',
		'color':'#000000'
	});	
	
	var val = $("#sn_input_message").val();
	if (val=="Type a message to everyone here...") {
		$("#sn_input_message").val('');	
	}
}

function sn_input_message_blur() {
	$("#sn_input_message").css({
		'border-color':'#d4d4d4',
		'color':'#8f8f8f'
	});	
	
	var val = $("#sn_input_message").val();
	if (val=="") {
		$("#sn_input_message").val('Type a message to everyone here...');	
	}	
}

function sn_load_messages(site_id,sn_user_id) {
	var rand = Math.random();
	$("#sn_message_list").load("/profile/includes/sn/includes/message_list.php?site_id=" + site_id + "&sn_user_id=" + sn_user_id + "&rand=" + rand);
}

function sn_clear_message_input() {
	$("#sn_input_message").val('Type a message to everyone here...');
}

function sn_post_message(site_id,sn_user_id) {
	
	function verify(data) {
		if (data=="1") {
			//success
			sn_clear_message_input();
			sn_load_messages(site_id,sn_user_id);
		}
		else {
			alert(data);	
		}
	}
	$.post("/profile/process/sn/post_message.php",$("#frmSN_Message").serialize(), function(data){ verify(data) } );
}

function sn_load_page(page,site_id,sn_user_id) {
	var rand = Math.random();
	if (page!="") {
		$("#sn_right").load("/profile/includes/sn/includes/" + page + ".php?site_id=" + site_id + "&sn_user_id=" + sn_user_id + "&rand=" + rand);	
	}
	else {
		sn_load_messages(site_id);
	}
}

function sn_delete_message(message_id) {
	var status = confirm("Are you sure you want to delete your message?");
	if (status) {
		var rand = Math.random();
		$.get("/profile/process/sn/delete_message.php?message_id=" + message_id + "&rand=" + rand, function(data) {
			if (data.indexOf("1:")==0) {
				var arr = data.split(":");
				var site_id = arr[1];
				var sn_user_id = arr[2];
				sn_load_messages(site_id,sn_user_id);
			}
			else {
				alert(data);	
			}
		});	
	}
}

function sn_message_reply_focus(message_id) {
	
	var content = $("#sn_message_reply_" + message_id).val();
	if (content.indexOf("Reply to")==0) {
		$("#sn_message_reply_" + message_id).val('');	
	}
	
	$("#sn_message_reply_" + message_id).css({
		'color':'#000000'							  
	});
}

function sn_message_reply_blur(message_id) {
	
	var content = $("#sn_message_reply_" + message_id).val();
	var sender = $("#sn_message_sender_" + message_id).val();
	if (content=="") {
		$("#sn_message_reply_" + message_id).val('Reply to ' + sender);	
	}
	
	$("#sn_message_reply_" + message_id).css({
		'color':'#8f8f8f'							  
	});	
}

function sn_load_replies(message_id,sn_user_id) {
	var rand = Math.random();
	$("#sn_message_replies_" + message_id).load("/profile/includes/sn/includes/message_replies.php?message_id=" + message_id + "&sn_user_id=" + sn_user_id + "&rand=" + rand);	
}

function sn_post_reply(message_id) {
	
	function verify(data) {
		if (data.indexOf("1:")==0) {
			var arr = data.split(":");
			var sn_user_id = arr[1];
			sn_load_replies(message_id,sn_user_id);
			$("#sn_message_reply_" + message_id).val('');
		}
		else {
			alert(data);	
		}
	}
	
	$.post("/profile/process/sn/post_message_reply.php", $("#frmMessageReply_" + message_id).serialize(), function(data){ verify(data) } );
	
}

function sn_load_user(site_id,sn_user_id) {
	var rand = Math.random();
	$("#sn_right").load("/profile/includes/sn/includes/profile.php?site_id=" + site_id + "&sn_user_id=" + sn_user_id + "&rand=" + rand);
}

function sn_input_search_focus(type) {
	$(".sn_input_search").css({
		'border-color':'#8f8f8f',
		'color':'#000000'
	});	
	
	var val = $(".sn_input_search").val();
	if (val=="Search for " + type + "...") {
		$(".sn_input_search").val('');	
	}
}

function sn_input_search_blur(type) {
	$(".sn_input_search").css({
		'border-color':'#d4d4d4',
		'color':'#8f8f8f'
	});	
	
	var val = $(".sn_input_message").val();
	if (val=="") {
		$(".sn_input_message").val('Search for ' + type + '...');	
	}	
}

function sn_load_people_list(site_id,sn_user_id,start) {
	
	if (!start) start=1;
//	alert(site_id + ',' + sn_user_id + ',' + start);
	var rand = Math.random();
	var query = $("#input_people_search").val();
	if (query=="Search for people...") query='';
	query = escape(query);
	$("#sn_people_list").load("/profile/includes/sn/includes/people_list.php?site_id=" + site_id + "&query=" + query + "&sn_user_id=" + sn_user_id + "&start=" + start + "&rand=" + rand);
	
}
