/***************************************************************************
*
* 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;
var changes = false;
var windowLog = "";
window.onerror = null;

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;
	
	if (memSettings.owner == 0)	showScrollWindow();
	
	if (memSettings.owner != 1)
	{
		$(obj.parentNode.parentNode.parentNode).animate({
                            opacity: 0    
                        },function () {
                            $(this).wrap('<div/>').parent().slideUp(function () {
                                $(this).remove();
                            });
                        });
		$('#menu').remove();				
		return;				
	}
	
	var reg = /_c_/g;
	
	if (id.match(reg) != null && isMy(id) == true)
		{
			if (confirm(popupMessages.removemyblock))
			{
				$.ajax({
							type: "GET",
							url: "alv/hpcomposer/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/hpcomposer/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) 
	{	
	    try{
				$('#'+ id + ' object').css('visibility','visible');
				$('#'+ id + ' embed').css('visibility','visible');
			}catch(e){};
			
		$('#menu').remove();
	}	
	
	id = obj.parentNode.parentNode.id;
	
	if (checkshared(id) == true) sharetext = popupBlockMenu.unshare;
		else
			sharetext = popupBlockMenu.share;
	
	$('#'+ id + ' object').css('visibility','hidden');
	$('#'+ id + ' embed').css('visibility','hidden');
	
	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) || 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) || id.match(reg_stand) == null)
		{	
			mem_avail += 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);
	
	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];
}

var toggleContent = function(e)
{
	if (memSettings.owner == 0)	showScrollWindow();
	
	var targetContent = $('div.boxContent', obj.parentNode.parentNode);
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300);
		$(this).html(popupBlockMenu.hide);
		setVis(obj.parentNode.parentNode.id, 0);
		changeID(obj.parentNode.parentNode.id, 0);
		
	} else {
		
		targetContent.slideUp(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);
		$(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; 
}

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/hpcomposer/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/hpcomposer/exec/blockProcessing.php",
		data: "string=" + text,
		async: false
});
}
function setVis(ID, val){
	if (ID.length == 0) return false;
	$.get("alv/hpcomposer/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());
	changes = true;
	
	if (memSettings.owner == 0)	showScrollWindow();
	
	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/hpcomposer/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();
	try{	
		$('#'+ id + ' object').css('visibility','visible');
		$('#'+ id + ' embed').css('visibility','visible');
		}catch(e){}
	}	
	
	var ext = '';
	if (memSettings.ext  == 1)
		ext = '<a href = "javascript:void(0);" id = "alv_add_my_agadget">' + mainMenu.addmyadv + '</a><br/>';
	
	var sharedBlocks = '<a href = "javascript:void(0);"  id = "alv_add_shared_blocks">' + mainMenu.addsharedblocks + '</a>';
		
	var sharing = '<a href = "javascript:void(0);"  id = "alv_add_some">' + mainMenu.addblocks + '</a>';
		
	html = ext + 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();
	});
	
	if (memSettings.ext  == 1)
		$('#alv_add_my_agadget').bind('click', addMyAdvancedBlock);
 
	$('#alv_add_some').bind('click', 
	function(){
		    document.location.href = 'hpSharedBlocks.php?action=standard';
	});
	
	$('#alv_add_shared_blocks').bind('click', 
	function(){
			document.location.href = 'hpSharedBlocks.php?action=shared';
	});
	
	return false;
}


var addMyAdvancedBlock = function(e){
	
	if (memSettings.add_block_allowed == 0) 
		{
			alert(popupMessages.limitexceeded);
			return 0;
		}	
		
	var html = $.ajax({
					  url: "alv/hpcomposer/tmpl/edit_awindow.php",
					  async: false,
					  dataType: "html"
					 }).responseText;
	 
	$(html).appendTo('body');
	
	try{	
		tinyMCE.execCommand( 'mceAddControl', false, 'alv_content');
		$('input.radio_check').click(
		function(){
			
			if ($(this)[0].value == 3 && $(this)[0].checked == true) 
			{	
				if ($("#alv_content_parent").length == 0 )
						tinyMCE.execCommand( 'mceAddControl', false, 'alv_content');
			}else
			{
				try{	
						if ($("#alv_content_parent").length > 0 )
						tinyMCE.execCommand( 'mceRemoveControl', false, 'alv_content');
					
					}catch(e) {}
			}			
	
		}
	);
	}catch(e)
	{
	
	}
	
	var width = 560;
    var height = 270;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - height/2);
	
	
	
	$('#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{	
												if ($("#alv_content_parent").length > 0 )
												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 reloadContentOfOneBlock = function(e){
	
	$("div.boxFirstHeader", this).css('padding-left','2px');
	$("div.boxFirstHeader", this).prepend('<a href="javascript:void(0)" id = "popupM" class="closeM"><img style="float:left;padding-right:2px" src="alv/hpcomposer/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', this).bind('click', popupMenu);
			$("div.boxFirstHeader", this).css("cursor","move");
			thisComp.addToSortableList();
}	


var addContent = function(e){
	
	var type = $("input[@name^='rad']:radio:checked").val();
	
	var content = null;
	try{
		var content = type == 3 ? 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/hpcomposer/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;
}

function showScrollWindow(){
	
	if ($('#alv_join').length > 0 ) 
			$('#alv_join').hide();
	else		
		  {	
			$('<div id="alv_join" class = "scroll_window">' + windowLog + '</div>').appendTo('body');
			$(".login_ajx_close").click(function(){ $("#alv_join").hide()});
		  }	
			
	$("#alv_join").css("top", $(document).scrollTop() + $(window).height()/2 );
	$("#alv_join").css("left", $(window).width() - $("#alv_join").width() - 50);
	$("#alv_join").show("normal"); 
	
	/*if ($('#alv_join').length > 0 ) $('#alv_join').remove();
	$('<div id="alv_join" class = "scroll_window">' + windowLog + '</div>').appendTo('body');
	$("#alv_join").css("top", $(document).scrollTop() + $(window).height()/2 );
	$("#alv_join").css("left", $(window).width() - $("#alv_join").width() - 50);
	$("#alv_join").show("normal"); 
	$(".login_ajx_close").click(function(){ $("#alv_join").hide()});*/
}

var addSomeBlock = function(e){
	document.location.href = 'hpSharedBlocks.php';
	return false;
}
var loading = function() {
		
		/*if (memSettings.owner == 1)
		{
			try{
				
				var element = $('#page_column_1').get(0);
				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;
				}

				$('body').append('<div id = "alvMainMenu"><a onclick="return getBlocks(event);" href="javascript:void(0);">' + popupBlockMenu.mainmenu + '</a></div>');
				$('#alvMainMenu').css('top', coverTop);
				$('#alvMainMenu').css('left', coverLeft + $('div.main').width());
			}catch(e){
			
			}
		}*/		
			
	$(".boxFirstHeader").css('padding-left','2px');
		
	$("div.page_column").addClass("column");
	$("div[@id^='page_column'] > div").addClass("groupItem");
			
	$("embed").attr('width','100%');
	$("object").attr('width','100%');
    
	$(".column a.").attr('target','');		    
	//if (memSettings.owner == 1)
		//	$("div.boxFirstHeader").prepend('<a href="javascript:void(0)" id = "popupM" class="closeM"><img src="alv/hpcomposer/icon/menu.gif" /></a>');
	
	//$('a.closeM').bind('mousedown', popupMenu);
	$('a.stuff').bind('click', showBlocks);
		
		if ($.browser.msie)
		{			
					$("div.boxContent").css('position','static');
		}			
		
		$(window).scroll(function () { 
			if (changes == true && memSettings.owner == 0) 
			{
				showScrollWindow();
			}	
		});

		//if (memSettings.owner == 1)
		//{
			thisComp = AlVComposer;
			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){};
}

$.post( './member.php?action=show_login_form&amp;relocate=' + encodeURIComponent( window.location ), function(data){
				windowLog = data;
			});
		
		
function ErrHandler(message, Url, number)
{
	//if (e.toString() == 'Invalid argument.') return true;
	return true;
}
if ($.browser.msie)
	window.attachEvent("onload", loading);
else
	$(document).ready(loading);	