/***************************************************************************
*
* IMPORTANT: This is a commercial product made by AntonLV and cannot be modified for other than personal usage. 
* This product cannot be redistributed for free or a fee without written permission from AntonLV. 
* This notice may not be removed from the source code.
*
***************************************************************************/


var obj = null;
var timeoutID = null;
var thisComp = null;

window.onerror = new Function('return true;');

function changeID(ID, val)
{
	$('#'+ID).attr('id',  ID.substr(0, ID.length - 1) + val);
}

function sharedAllow(id){
	var tmp = id.split('_');
	
	if (alv_owner != null) 
	{
		for(var i = 0; i < alv_owner.length; i++)
		   {	
				if (parseInt(tmp[1]) == alv_owner[i].id && alv_owner[i].share == 1) return true;
				else if (parseInt(tmp[1]) == alv_owner[i].id && alv_owner[i].share == 0) return false;
		   }
	}
	return false;
} 

function isMy(id){
	var tmp = id.split('_');
	
	if (alv_owner != null) 
	{
		for(var i = 0; i < alv_owner.length; i++)
		   {	
				if (parseInt(tmp[1]) == alv_owner[i].id) return true;
		   }
	}
	return false;
} 

var removeContent = function(e){
	var id;
	id = obj.parentNode.parentNode.id;
	
	var reg = /_c_/g;
	
	if (id.match(reg) != null && isMy(id) == true)
		{
			if (confirm(popupMessages.removemyblock))
			{
				$.ajax({
							type: "GET",
							url: "alv/ppcomposer/exec/removeBlock.php",
							data: "ID=" + id,
							async: false
						});	
				$('#' + id).animate({
                            opacity: 0    
                        },function () {
                            $(this).wrap('<div/>').parent().slideUp(function () {
                                $(this).remove();
                            });
                        });
				memSettings.add_block_allowed++;
			}	
		}else 
				{
					$.ajax({
								type: "GET",
								async: false,
								url: "alv/ppcomposer/exec/removeBlock.php",
								data: "ID=" + id
						   });
					
					$('#' + id).animate({
                            opacity: 0    
                        },function () {
                            $(this).wrap('<div/>').parent().slideUp(function () {
                                $(this).remove();
                            });
                        });
				}
			
	$('#menu').remove();
	return false;
};

var popupMenu = function(e){
	var element = this;
	var coverLeft = 0;
	var coverTop = 0;
	var coverWidth = element.width;
	var coverHeight = element.height;
	
	
	while (element.offsetParent) {
		coverLeft += element.offsetLeft;
		coverTop += element.offsetTop;
		element = element.offsetParent;
	}

	obj = this;
	
	var text = '';
	var sharetext = '';
	var membtext = '';
	var nontext = '';
	var frtext = '';
	var content = $('div.boxContent', obj.parentNode.parentNode);
	if (content.css('display') == 'none') text = popupBlockMenu.show; else text = popupBlockMenu.hide; 
	
	if ($('#menu').length > 0) 
	{	
		$('#'+ id + ' object').css('visibility','visible');
		$('#'+ id + ' embed').css('visibility','visible');
		$('#menu').remove();
	}	
	
	id = obj.parentNode.parentNode.id;
	
	if (checkshared(id) == true) sharetext = popupBlockMenu.unshare;
		else
			sharetext = popupBlockMenu.share;
	
	if (checkVis(id, 2) == true) 
	{	
		membtext = popupBlockMenu.unavail + ' ' + popupBlockMenu.formem;
	}	
		else
		{	
			membtext = popupBlockMenu.avail + ' ' + popupBlockMenu.formem;
		}	

	$('#'+ id + ' object').css('visibility','hidden');
	$('#'+ id + ' embed').css('visibility','hidden');
	
	if (checkVis(id, 4) == true) nontext = popupBlockMenu.unavail + ' ' + popupBlockMenu.fornonmem;
		else
			nontext = popupBlockMenu.avail + ' ' + popupBlockMenu.fornonmem;			

	if (checkVis(id, 1) == true) frtext = popupBlockMenu.unavail + ' ' + popupBlockMenu.forfriends;
		else
			frtext = popupBlockMenu.avail + ' ' + popupBlockMenu.forfriends;			
			
	var sharing = '';
	
	var reg = /_c_/g;
	
	if (memSettings.sharing == 1 && memSettings.owner == 1 && id.match(reg) != null && isMy(id) == true && sharedAllow(id) == true)
		sharing = '<a href="javascript:void(0)" id="alv_show_canbe_shar" class="alv_hide_b">' + sharetext + '</a>';
		
	var mem_avail = '';
	
	var reg_stand = /_t_/g;
	
	if (memSettings.owner == 1)
	{
		if (id.match(reg_stand) != null && memSettings.allow_stand_remove == 1)
		{	
			mem_avail = '<a href="javascript:void(0)" id="alv_remove_b" class="alv_hide_b">' + popupBlockMenu.remove + '</a><br/>';
		}	
		else if (id.match(reg_stand) == null)	
		{   
			mem_avail = '<a href="javascript:void(0)" id="alv_remove_b" class="alv_hide_b">' + popupBlockMenu.remove + '</a><br/>';
		}	
		
	if (id.match(reg_stand) != null && memSettings.allow_stand_change == 1)
	{	
		mem_avail += '<a href="javascript:void(0)" id="alv_show_mem" class="alv_hide_b">' + membtext + '</a><br/><a href="javascript:void(0)" id="alv_show_non" class="alv_hide_b">' + nontext + '</a><br/><a href="javascript:void(0)" id="alv_show_only_friend" class="alv_hide_b">' + frtext + '</a><br/>' + sharing + '</div>';
	}	
		else if (id.match(reg_stand) == null)
	{	
		mem_avail += '<a href="javascript:void(0)" id="alv_show_mem" class="alv_hide_b">' + membtext + '</a><br/><a href="javascript:void(0)" id="alv_show_non" class="alv_hide_b">' + nontext + '</a><br/><a href="javascript:void(0)" id="alv_show_only_friend" class="alv_hide_b">' + frtext + '</a><br/>' + sharing + '</div>';
	}	
	}
	
	$('<div id="menu" class = "sample_attach"><a href="javascript:void(0)" id="alv_hide_b" class="alv_hide_b">' + text + '</a><br/>'+ mem_avail).appendTo('body');
	$('#menu').css('top',coverTop + 15);
	$('#menu').css('left',coverLeft - 5);
	
	
	
	$('#menu').hover(function() {
		
	}, function() {
		$('#menu').remove();
		var trestID = getIbMainPart(id);
		$("div[@id^='" + trestID + "'] object").css('visibility','visible');
		$("div[@id^='" + trestID + "'] embed").css('visibility','visible');
	});

	$('#alv_hide_b').bind('click', toggleContent);
	$('#alv_remove_b').bind('click', removeContent);
	
	$('#alv_show_mem').bind('click', showForMem);
	$('#alv_show_non').bind('click', showForNonMem);
	$('#alv_show_only_friend').bind('click', showForFriends);
	
	if (memSettings.sharing == 1)
		$('#alv_show_canbe_shar').bind('click', shareBlock);		
	
	return false;
};

function getIbMainPart(ID){
	var tmp = ID.split('_');
   return tmp[0] + '_' + tmp[1] + '_' + tmp[2];
}
function ifPromoExists()
{
		if ($.browser.msie && $('#iiPPhoto1').length > 0)
		{	
			$('#iiPPhoto1').css('height','443px');
			$('#iiPPhoto1').css('width','100%');
			$('#iiPPhoto1').css('display','block');
			$('#iiPPhoto1').parent().css('width','100%');
			$('#iiPPhoto1 > img').css('height','340px');
			$('#iiPPhoto1 > img').css('width','340px');
			$('#iiPPhoto1 > img').css('left', parseInt(($('#iiPPhoto1').width() - 340)/2));
		}
}

var toggleContent = function(e)
{
	var targetContent = $('div.boxContent', obj.parentNode.parentNode);
	if (targetContent.css('display') == 'none') {
		targetContent.show(function(){ifPromoExists()});
		$(this).html(popupBlockMenu.hide);
		setVis(obj.parentNode.parentNode.id, 0);
		changeID(obj.parentNode.parentNode.id, 0);
		
	} else {
		
		targetContent.hide(300);
		$(this).html(popupBlockMenu.show);
		setVis(obj.parentNode.parentNode.id, 1);
		changeID(obj.parentNode.parentNode.id, 1);
	}
	return false;
};


var popupMemberMenu = function(e)
{
	var targetContent = $('div.boxContent', this.parentNode.parentNode);
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300, function(){ifPromoExists()});
		$(this).html('[-]');
	} else {
		targetContent.slideUp(300);
		$(this).html('[+]');
	}
	return false;
};

function checkshared(ID){
	var tmp = ID.split('_');
	return  parseInt(tmp[3]) & 1; 
}

function checkVis(ID, val){
	var tmp = ID.split('_');

	if ((tmp[4] & val) != 0) return true; 
	return false;
}

function changeVis(ID, val)
{
	var tmp = ID.split('_');
		tmp[4] = parseInt(tmp[4])^val;
	
	$('#'+ID).attr('id', tmp.join('_'));	
	
	return checkVis(tmp.join('_'), val);
}

function makeShare(ID){
	var tmp = ID.split('_');
		tmp[3] = parseInt(tmp[3])^1;
	$('#'+ID).attr('id', tmp.join('_'));
	return  parseInt(tmp[3]) & 1; 
}
function showForMem(e){
	var ID = obj.parentNode.parentNode.id;
	if (ID.length == 0) return false;
	
	if ( changeVis(ID, 2) == true) 
			$('#alv_show_mem').html(popupBlockMenu.unavail + ' ' + popupBlockMenu.formem);
		else
			$('#alv_show_mem').html(popupBlockMenu.avail + ' ' + popupBlockMenu.formem);
	
	$.ajax({
		type: "GET",
		url: "alv/ppcomposer/exec/changeAvail.php",
		async: false,
		data: "ID=" + ID + '&' + 'for=memb'
	});
		
	return false;
}
var showForNonMem = function(e){
	var ID = obj.parentNode.parentNode.id;
	if (ID.length == 0) return false;
	
	if (changeVis(ID, 4) == true) $('#alv_show_non').html(popupBlockMenu.unavail + ' ' + popupBlockMenu.fornonmem);
		else
			$('#alv_show_non').html(popupBlockMenu.avail + ' ' + popupBlockMenu.fornonmem);

	var html = $.ajax({
		type: "GET",
		url: "alv/ppcomposer/exec/changeAvail.php",
		async: false,
		data: "ID=" + ID + '&' + 'for=non'
	});

	return false;
}
var showForFriends = function(e){
	var ID = obj.parentNode.parentNode.id;
	if (ID.length == 0) return false;
	
	if (changeVis(ID, 1) == true) 
		$('#alv_show_only_friend').html(popupBlockMenu.unavail + ' ' + popupBlockMenu.forfriends);
		else
		$('#alv_show_only_friend').html(popupBlockMenu.avail + ' ' + popupBlockMenu.forfriends);		
	
	var html = $.ajax({
		type: "GET",
		url: "alv/ppcomposer/exec/changeAvail.php" ,
		async: false,
		data: "ID=" + ID + '&' + 'for=friends'
	});
	
	return false;
}

var shareBlock = function(e){
	
	var ID = obj.parentNode.parentNode.id;
	if (ID.length == 0) return false;
	
	if (makeShare(ID) == true) 
		$('#alv_show_canbe_shar').html(popupBlockMenu.unshare);
	else 	
		$('#alv_show_canbe_shar').html(popupBlockMenu.share)
		
	var html = $.ajax({
		type: "GET",
		url: "alv/ppcomposer/exec/changeAvail.php",
		async: false,
		data: "ID=" + ID + '&' + 'for=share'
	});
	
	return false;
} 

function sendBlocks(text){
	if (text.length == 0) return false;
	
	$.ajax({
		type: "POST",
		url: "alv/ppcomposer/exec/blockProcessing.php",
		data: "string=" + text,
		async: false
});
}
function setVis(ID, val){
	if (ID.length == 0) return false;
	$.get("alv/ppcomposer/exec/setVisibility.php", { ID: ID, vis: val });
}

function serialize(){
 	var text_arr = [];

$("div[@id^='page_column_']").each(function()
  {
   var st = '';
   $("div[@id^='alv_']", this).each(function()
	{
		st += '[' + $(this)[0].id + ']';
	})
	
	text_arr.push(st);
  })
	sendBlocks(text_arr.toString());
	
	return false;
};

function getBlocks(e){
	var coor = getPageEventCoords(e);
	showBlocks(coor);
	return false;
}
function getPageEventCoords(evt) {
    var coords = {left:0, top:0};
    if (evt.pageX) {
        coords.left = evt.pageX;
        coords.top = evt.pageY;
    } else if (evt.clientX) {
        coords.left = 
            evt.clientX + document.body.scrollLeft - document.body.clientLeft;
        coords.top = 
            evt.clientY + document.body.scrollTop - document.body.clientTop;
        // include html element space, if applicable
        if (document.body.parentElement && document.body.parentElement.clientLeft) {
            var bodParent = document.body.parentElement;
            coords.left += bodParent.scrollLeft - bodParent.clientLeft;
            coords.top += bodParent.scrollTop - bodParent.clientTop;
        }
    }
    return coords;
}
function addBlockToColumn(msg){
			
			var obj = $(msg);
			var id = $("div.disignBoxFirst", obj).attr('id');
			
			
			if (obj.text() == "" ) 
						  {	
							alert(popupMessages.emptyblock);	
							return false;
						  }
						 
						  var p = $('#' + id).length;
						  
						  if (p > 0)  
						  {
							alert(popupMessages.blockexists);
							$('#menu').remove();
							return false;
						  }
			
							
							$('#page_column_1').prepend(obj);
							
							$("div.boxFirstHeader", obj).mousedown(function (e) {
					                $(this).parent().css({
									 width: $(this).parent().width() + 'px'
					            });
					        }).mouseup(function () {
								if($(this).parent().hasClass('dragging')) $(thisComp.settings.columns).sortable('disable');
					        });

						  $("div.boxFirstHeader", obj).css('padding-left','2px');
						  $(obj).addClass('groupItem');
						  
						  $("div.boxFirstHeader", obj).prepend('<a href="javascript:void(0)" class="closeM" id = "popupM"><img src="alv/ppcomposer/icon/menu.gif" style="float:left;padding-right:0px;padding-left:0px;"/></a>&nbsp;');
						  $("a.closeM", obj).bind('click', popupMenu);
						  $("div.boxFirstHeader", obj).css("cursor","move");
						  thisComp.addToSortableList();
}

var showBlocks = function(coor){
	
	var text = '';

	var html = '';
	
	if ($('#menu').length > 0) 
	{	
		$('#menu').remove();
		$('#'+ id + ' object').css('visibility','visible');
		$('#'+ id + ' embed').css('visibility','visible');
	}	
	
	var ext = '';
	if (memSettings.ext  == 1)
		ext = '<a href = "javascript:void(0);" id = "alv_add_my_agadget">' + mainMenu.addmyadv + '</a><br/>';
	
	var sharing = '<a href = "javascript:void(0);"  id = "alv_add_some">' + mainMenu.addblocks + '</a>';
		
	var sharedBlocks = '';
	
	if (memSettings.sharing == 1) 
	  sharedBlocks = '<a href = "javascript:void(0);"  id = "alv_add_shared_blocks">' + mainMenu.addsharedblocks + '</a>';
		
	html = ext + '<a href = "javascript:void(0);" id = "alv_add_my_sgadget">' + mainMenu.addmysimple + '</a><br/>' + sharing + '<br/>' + sharedBlocks;
	
	$('<div id="menu" class = "sample_attach">'+ html +'</div>').appendTo('body');
	
	
	 
	$('#menu').css('top',coor.top + 15);
	$('#menu').css('left',coor.left);
	
	$('#menu').hover(function() {
		
	}, function() {
		$('#menu').remove();
	});
	
	$('#alv_add_my_sgadget').bind('click', addMySimpleBlock);
	
	if (memSettings.ext  == 1)
		$('#alv_add_my_agadget').bind('click', addMyAdvancedBlock);
 
	$('#alv_add_some').bind('click', 
	function(){
		    document.location.href = 'sharedblocks.php?action=standard';
	});
	
	if (memSettings.sharing == 1)
	{
 	 $('#alv_add_shared_blocks').bind('click', 
	 function(){
			document.location.href = 'sharedblocks.php?action=shared';
	 });
       }
	
	return false;
}

var addMySimpleBlock = function(e){
	
	if (memSettings.add_block_allowed == 0) 
		{
			alert(popupMessages.limitexceeded);
			return 0;
		}	
		
	var html = $.ajax({
					  url: "alv/ppcomposer/tmpl/edit_swindow.php",
					  async: false,
					  dataType: "html"
					 }).responseText;
	 
	$(html).appendTo('body');
   try{	
	tinyMCE.execCommand( 'mceAddControl', false, 'alv_content');
	
	}catch(e)
	{
	
	}
	
	var width = 320;
    var height = 300;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - height);
	
	$('#textarea').css('top',top);
	$('#textarea').css('left',left);
	$('#alv_b_exit').css('cursor','hand');
	
	$('#alv_b_create').bind('click', addContent);
	$('#alv_b_exit').bind('click', function()
				{
					
				try{	
					tinyMCE.execCommand( 'mceRemoveControl', false, 'alv_content');
					}catch(e)
						{
						
						}

					$('#textarea').remove();
					$('object').css('visibility','visible');
					$('embed').css('visibility','visible');
					$('#login_div').hide();
				});
	$('object').css('visibility','hidden');
	$('embed').css('visibility','hidden');
	
	$('#textarea').css('visibility','visible');
	
	showItemEditForm('login_div');	
	$('#login_div').css('z-index','100');
	$('#textarea').css('z-index',parseInt($('#login_div').css('z-index')) + 1);

	return false;
}

var addMyAdvancedBlock = function(e){
	
	if (memSettings.add_block_allowed == 0) 
		{
			alert(popupMessages.limitexceeded);
			return 0;
		}	
		
	var html = $.ajax({
					  url: "alv/ppcomposer/tmpl/edit_awindow.php",
					  async: false,
					  dataType: "html"
					 }).responseText;
	 
	$(html).appendTo('body');
	
	var width = 560;
    var height = 270;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - height);
	
	
	
	$('#textarea').css('top',top);
	$('#textarea').css('left',left);
	$('#alv_b_exit').css('cursor','hand');
	
	$('#alv_b_create').bind('click', addContent);
	
	$('#alv_b_exit').bind('click', function()
										{ 
											$('#textarea').remove();
											$('object').css('visibility','visible');
											$('embed').css('visibility','visible');
											$('#login_div').hide();
										});
	
	$('object').css('visibility','hidden');
	$('embed').css('visibility','hidden');
	
	$('#textarea').css('visibility','visible');
	showItemEditForm('login_div');	
	$('#login_div').css('z-index','100');
	$('#textarea').css('z-index',parseInt($('#login_div').css('z-index')) + 1);
	return false;
}

function reloadContentOfOneBlock(object){
	
	
	if (memSettings.owner == 1)
	{	
	
		$("div.boxFirstHeader", object).css('padding-left','2px');
		$("div.boxFirstHeader", object).prepend('<a href="javascript:void(0)" id = "popupM" class="closeM"><img style="float:left;padding-right:0px" src="alv/ppcomposer/icon/menu.gif" /></a>')
		.mousedown(function (e) {
	            $(this).parent().css({
	                width: $(this).parent().width() + 'px'
	            });
	     }).mouseup(function () {
	           if($(this).parent().hasClass('dragging')) $(thisComp.settings.columns).sortable('disable');
	});
	
		$('a.closeM', object).bind('click', popupMenu);
		$("div.boxFirstHeader", object).css("cursor","move");
		thisComp.addToSortableList();
	}else
	{
		if ($("div.boxContent", object).css('display') == 'none')
		$("div.boxFirstHeader", object).prepend('<a href="javascript:void(0)" class="closeMem" style = "text-decoration:none;color:gray;" id = "popupM">[+]</a>&nbsp;');
		else
		 $("div.boxFirstHeader", object).prepend('<a href="javascript:void(0)" class="closeMem" style = "text-decoration:none;color:gray;" id = "popupM">[-]</a>&nbsp;');	
				$('a.closeMem').bind('click', popupMemberMenu);
				return;
	}
	
	$('a.closeM').bind('click', popupMenu);
	$("div.boxFirstHeader", object).css("cursor","move");
	
}

var addContent = function(e){
	
	var type = $("input[@name^='rad']:radio:checked").val();
	var content = null;
	try{
	var content = type == null ? tinyMCE.activeEditor.getContent() :  $('#alv_content').val();
	}catch(e)
	{
	
	}

	var title = $('#alv_t_desc').val();
	
	if (content.length == 0) 
	{
		alert(popupMessages.emptycontent);
		return;
	}	
	
	if (content.length > memSettings.char_count)
	{
		alert(popupMessages.more_then);
		return ;
	}
	
	if (title.length == 0) 
	{
		alert(popupMessages.emptytitle);
		return;
	}	
	
	var html = $.ajax({
					  type: "POST",
					  url: "alv/ppcomposer/exec/addBlock.php",
					  async: false,
					  data: "title="+ encodeURIComponent(title) +"&body=" + encodeURIComponent(content) + '&type=' + (type = type == null? 0 : type),
					  success: function(msg){
						  	if (msg == '0') 
							{	
								alert(popupMessages.blockapproved);
							}else
							{	
								addBlockToColumn(msg);
							}	
							memSettings.add_block_allowed--;
							$('#textarea').remove();
							$('object').css('visibility','visible');
							$('embed').css('visibility','visible');
							$('#login_div').hide();
					  }		
					 }).responseText;
	try{
	if (type == null)
		tinyMCE.execCommand( 'mceRemoveControl', false, 'alv_content');			
	}catch(e)
	{
	}	
		
	return false;
}

var addSomeBlock = function(e){
	document.location.href = 'sharedblocks.php';
	return false;
}
var loading = function() {
		
		$(".boxFirstHeader").css('padding-left','2px');
		
		$("div.page_column").addClass("column");
		$("div[@id^='page_column'] > div").addClass("groupItem");
			
		$("div.column > embed").attr('width','100%');
	    $("div.column > object").attr('width','100%');
    
		thisComp = AlVComposer;
		
	if (memSettings.owner == 0)
	   {
			 $("div.disignBoxFirst").each(
			 function() 
			 {
				if ($("div.boxContent", this).css('display') == 'none')
					$("div.boxFirstHeader", this).prepend('<a href="javascript:void(0)" class="closeMem" style = "text-decoration:none;color:gray;" id = "popupM">[+]</a>&nbsp;');
				else
				 $("div.boxFirstHeader", this).prepend('<a href="javascript:void(0)" class="closeMem" style = "text-decoration:none;color:gray;" id = "popupM">[-]</a>&nbsp;');	
			 }
			 )
				$('a.closeMem').bind('click', popupMemberMenu);
				return;
		 }
			
		$('a.stuff').bind('click', showBlocks);
		
		if ($.browser.msie)
		{			
					$("div.boxContent").css('position','static');
		}			

		if (memSettings.owner == 1)
		{
			AlVComposer.init();
			AlVComposer.params.mainPMenuItmes = mainMenu;
			AlVComposer.params.myBlocks = alv_owner;
			AlVComposer.params.blocksPMenuItems = popupBlockMenu;
			AlVComposer.params.popupMessages = popupMessages;
			AlVComposer.params.memSettings = memSettings;
		}	
				
		window.document.write = function(s){};
}

if ($.browser.msie)
	window.attachEvent("onload", loading);
else
	$(document).ready(loading);	