/*facebook*/

function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}

/*create message*/

//need for flash (after movie -> change banner)
function showAds()
{
	var iframeObj = jQuery("gooadsFrame")
	iframeObj.src = null;
	iframeObj.src = "templates/gAds.html";
}

function PlayerWBWL(id, name)
{
	var so = new SWFObject(SITE_WWW_ROOT+SOPHIT_WWW_ROOT+"flash/build/PlayerWBWL.swf?v="+VERSION, "WBWL2", "888", "412", "8", "#FFFFFF");

	so.addParam("scale", "exactfit");
	so.addVariable("callBackFunctionName", 'getLongMovie');
	so.addVariable("autoplay", 0);
	so.addVariable("nameCel", name);
	so.addVariable("celID", id);
	so.addVariable("playerURL", 'flash/build/videoplayer.swf');
	so.addParam("menu", "false");
	addToFlash(so);
	so.addVariable("transparencyText", "<TEXTFORMAT LEADING='2'><FONT FACE='Arial' SIZE='26' COLOR='#000000' LETTERSPACING='0' KERNING='0'><P ALIGN='JUSTIFY'>Click next step to email this video or use share options below to link to it.</FONT></TEXTFORMAT>");
	so.write("div_PlayerWBWL");
}

function playPreviewMovie(id)
{
	var so = new SWFObject(SITE_WWW_ROOT+SOPHIT_WWW_ROOT+"flash/build/videoplayer.swf?v="+VERSION, "videoplayer", "480", "408", "8", "#FFFFFF");
	so.addParam("scale", "exactfit");
	so.addVariable("blockId", id);
	so.addVariable("isShared", 1);
	so.addVariable("callBackFunctionName", 'getLongMovie');
	so.addVariable("autoplay", 0);
	so.addParam("menu", "false");
	addToFlash(so);
	so.write("placeForVideoCreatePage3");
}


var TEXT_FOR_FLASH = "<TEXTFORMAT LEADING='2'><FONT FACE='Arial' SIZE='12' COLOR='#FFFFFF' LETTERSPACING='0' KERNING='0'><P ALIGN='JUSTIFY'>This video is the work of a Starmashers&#8482; user and created on Starmashers.com. It is licensed through the Starmashers <A HREF='index.php?ACTION=userAgreement' TARGET='_blank'><U>User Agreement</U></A>. The individual(s) depicted may not actually have made the statements they may appear to make on this video. Starmashers is a trademark of PlaytheDG, Inc. All other trademarks are the property of their respective owners.</FONT></TEXTFORMAT>";


function addToFlash(so) {
	so.addVariable("gatewayUrl", SITE_WWW_ROOT + SOPHIT_WWW_ROOT+"gateway.php");
	so.addVariable("rootHtmltext", TEXT_FOR_FLASH);
	so.addParam("BASE", SITE_WWW_ROOT+SOPHIT_WWW_ROOT);
	so.addParam("wmode", "transparent");
}

function log(str){
	console.log(str);
}
function json(str){return eval('(' + str + ')');}
function updateLoginBox(){
	if(loggin)jQuery("#loginDiv").load("index.php?ACTION=login&todo=getRegisteredHtml&type=html");
	else jQuery("#loginDiv").load("index.php?ACTION=login&todo=getLoginFormHtml&type=html");
}
function showMessage(txt){
	var dlg = jQuery('#warning');
	jQuery('div > span',dlg).html(txt)
	jQuery('#warning').jqmShow()
}
function showError(txt){
	var dlg = jQuery('#error');
	jQuery('div > span',dlg).html(txt)
	jQuery('#error').jqmShow()
}

function submitForgotPass(fm){
	jQuery(fm).ajaxSubmit({
		success: function(j) {
			if(j.error)showError(j.error)
			if(j.message){showMessage(j.message);backToLogin();}
		},
		dataType: 'json',type: "POST"
	});
	return false;
}
function showFormLogin(){
	if (jQuery("#loginDiv > form").is(":hidden")) {
		jQuery("#loginDiv > form").slideDown("fast");
		jQuery("#img_log").removeClass().addClass("logIn1");
	} else {
		jQuery("#loginDiv > form").slideUp("fast");
		jQuery("#img_log").removeClass().addClass("logIn2");
	}
}

function submitLoginForm(fm){
	var lgn = false;
	jQuery(fm).ajaxSubmit({
		dataType: 'json',
		async: false,
		success:function(json){
			if (json.MESSAGE) {
				loggin = true;
				///!!
				if(typeof onLogin =="function")onLogin();
				updateLoginBox();
				//showMessage(json['MESSAGE']);
				if (json['URL']) {
					document.location.replace(json['URL']);
				}
			}
			else showError(json['ERROR']);
		}
	});
	/*if(typeof steps!='undefined')
	if(steps.step==3 && loggin) {
	jQuery("#selABook").disabled = false;
	jQuery("#selABook >option").attr("disabled",false);
	jQuery("#selABook").load("index.php?ACTION=addressBook&todo=showABforSendPage");
	}*/
	return false;
}
function backToLogin(){
	updateLoginBox();return false;
}
function actionShowForgetPasswordForm()
{
	jQuery('#loginDiv').load('index.php?ACTION=forgotPassword');
	return false;
}
function goTo(url)
{
	if(checkIsLoggedIn()){
		window.document.location.replace(url);
	}
	else showError("You must be logged in to access this functionality.");
	return false;
}

function checkAccess()
{
	if(checkIsLoggedIn())return true;
	else showError("You must be logged in to access this functionality.");
	return false;
}

function checkAccessJson(j){
	if(j.access) {
		showError(j.error);
		if(loggin){ loggin = false;updateLoginBox(); }
		return false;
	}
	else return true;
}

function checkIsLoggedIn(elem)
{
	var b = false;
	jQuery.ajax({
		url: "index.php?ACTION=login&todo=checkUserStatus&type=json",
		cache: false,
		async: false,
		dataType: 'json',
		success: function(json) {
			if (json['loggedIn'] == "false") {
				if(loggin){
					loggin = false;
					updateLoginBox();
				}
				return false;
			}
			else {
				if(!loggin){
					loggin = true;
					updateLoginBox();
				}
				b = true;
				return true;
			}
		}
	});
	return b;
}

var AB = {
	setAb:function (){
		if(!checkAccess())return;
		var _t=this;
		jQuery(".a-book").load("index.php?ACTION=addressBook&todo=getAddressBook&type=html", function(){
			_t.updateMailList();
		});
	},
	updateMailList:function(){
		var str = new String(jQuery('#tbTo').val());
		var el;
		jQuery('.icon_pluse').each(function(f){
			el = jQuery(this)
			if (str.search(el.text()) >= 0)el.toggleClass('icon_pluse').toggleClass('icon_minus');
		});
		jQuery('.icon_minus').each(function(f){
			el = jQuery(this)
			if (str.search(el.text()) == -1)el.toggleClass('icon_pluse').toggleClass('icon_minus');
		});
	},
	runPlaxo:function(){
		if(!checkAccess())return;
		var str = '<a  href="javascript:void(0)" onclick="AB.setAb()" class="address">Address book:</a>'+
		'<label for="tbAbook" class="import">Import</label>'+
		'<div class="clear">&nbsp;</div>'+
		'<textarea rows="10" cols="34" id="tbAbook"></textarea>'+
		'<div class="submit">'+
		'<ul class="">'+
		'<li><button type="button" onclick="AB.addEmail();">Add emails</button></li>'+
		'<li><a href="javascript:void(0)" onclick="AB.setAb()">Cancel</a></li>'+
		'</ul></div>';
		jQuery(".a-book").html(str);
		showPlaxoABChooser('tbAbook',PATH_PLAXO);
	},
	addEmail:function(){
		var txt = jQuery('#tbAbook');
		if (txt.val() == '') {
			alertbox.setError('Please click \'Import\' link and choose emails to add.');
			return false;
		}
		//txt.disabled = 0;
		var _t = this;
		jQuery.ajax({
			type: "POST", url:"index.php?ACTION=addressBook&todo=addEmails",
			cache: false, async: false,data:{emails:txt.val()},dataType: 'json',
			success: function(json) {
				if(!checkAccessJson(j))return;
				if (j['error']) alertbox.setError(j['error']);
				if (j['message']) {
					alertbox.setWarning(j['message']);
					_t.setAb();
				}
			}
		});
	},
	addTo:function(aEl){
		var txt = jQuery(aEl).text();
		var tbTo = jQuery('#tbTo')
		var val = tbTo.val();
		var str   = new String(val);
		if(jQuery(aEl).is(".icon_pluse")) {
			tbTo.val(str.replace(/\s+$/, ''));
			if (val == '') tbTo.val(txt);
			else {
				if (val[val.length - 1] == ';' || val[val.length - 1] == ',')tbTo.val(val+' '+ txt);
				else tbTo.val(val+'; '+ txt);
			}
		}
		else {
			var email = new RegExp(txt + '(,|;)*');
			tbTo.val(str.replace(/\s*;/, ';').replace(/\s*,/, ',').replace(email, '').replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/, ' '));
		}
		jQuery(aEl).toggleClass('icon_pluse').toggleClass('icon_minus');
	}
}
