var de_workspace = null;
var de_toys_conteiner = null;
var de_present_conteiner = null;
var de_workspace_position = null;
var maxZIndex = 1;
var currentPageWidth = 0;
var de_workspace_current_pos = null;
var selected_present_image_id = null;
var visible_gift_image = 1,hidden_gift_image=2;
var gift_animation = false;
var send_present = true;
var countries=new Array,countriesLow=new Array;
var gifts_left_in_conteiner = 0;
var config = {'web_dir_name' : 'http://img.elka10.com'};

document.observe('dom:loaded',function(){
	if ($('document_width')) currentPageWidth = $('document_width').getWidth();

	de_workspace = $('workspace');
	de_toys_conteiner = $('toys_conteiner');
	de_present_conteiner = $('present_conteiner');
	if (de_workspace){
		de_workspace_position = de_workspace.positionedOffset();
		de_workspace_current_pos = de_workspace_position;
	}
	if (de_workspace&&de_toys_conteiner){
		de_toys_conteiner.select('img').each(function(el){
			el.style.zIndex = 1;
			new DraggableToy(el,{
				workspace_x1: de_workspace_position.left,
				workspace_y1: de_workspace_position.top,
				workspace_x2: de_workspace_position.left+de_workspace.getWidth(),
				workspace_y2: de_workspace_position.top+de_workspace.getHeight()
			});
			el.observe('click',function(){
				maxZIndex++;
				el.style.zIndex = maxZIndex;
			});
		});
		toys_position.each(function(el_pos){
			var img = $(el_pos.id);
			img.absolutize();
			img.style.zIndex = el_pos.zIndex;
			var img_pos = img.positionedOffset();
			var x1 = img_pos.left;
			var y1 = img_pos.top;
			var x2 = de_workspace_position.left+el_pos.x;
			var y2 = de_workspace_position.top+el_pos.y;
			new Effect.Move(img,{x: x2-x1, y: y2-y1, duration: 0.5});
		});
	}
	if (de_workspace&&de_present_conteiner){
		de_workspace_position = de_workspace.positionedOffset();
		de_workspace_current_pos = de_workspace_position;
		de_present_conteiner.select('img').each(function(el){
			gifts_left_in_conteiner++;
			el.style.zIndex = 2;
			new DraggableToy(el,{
				workspace_x1: de_workspace_position.left,
				workspace_y1: de_workspace_position.top,
				workspace_x2: de_workspace_position.left+de_workspace.getWidth(),
				workspace_y2: de_workspace_position.top+de_workspace.getHeight()
			});
			el.observe('click',function(){
				maxZIndex++;
				el.style.zIndex = 10000+ maxZIndex;
			});
		});
		presents_position.each(function(el_pos){
			var img = $(el_pos.id);
			img.absolutize();
			img.style.zIndex = el_pos.zIndex;
			var img_pos = img.positionedOffset();
			var x1 = img_pos.left;
			var y1 = img_pos.top;
			var x2 = de_workspace_position.left+el_pos.x;
			var y2 = de_workspace_position.top+el_pos.y;
			gifts_left_in_conteiner--;
			new Effect.Move(img,{x: x2-x1, y: y2-y1, duration: 0.5});
		});
		if (gifts_left_in_conteiner==0) new Effect.Appear('edit_tree_more_gifts');
	}
	if ($('user_given_presents')){
		de_workspace_position = de_workspace.positionedOffset();
		given_user_presents_position.each(function(el_pos){
			var img = $(el_pos.id);
			img.absolutize();
			img.style.zIndex = el_pos.zIndex;
			var img_pos = img.positionedOffset();
			var x1 = img_pos.left;
			var y1 = img_pos.top;
			var x2 = de_workspace_position.left+el_pos.x;
			var y2 = de_workspace_position.top+el_pos.y;
			new Effect.Move(img,{x: x2-x1, y: y2-y1, duration: 0.5});
			img.fade_image = true;
			setTimeout("highlihtUserPresnets('"+el_pos.id+"')", (randomValue(1000,5000)));
		});
	}
	Event.observe(window, "resize", function(){
		if (de_workspace&&de_toys_conteiner&&de_present_conteiner){
			var newPageWidth = $('document_width').getWidth();
			var imageDelta = (currentPageWidth-newPageWidth)/2;
			de_workspace_current_pos.left = parseInt(de_workspace_current_pos.left) - imageDelta;
			Draggables.drags.each(function(drag_toy){
				drag_toy.options.workspace_x1 = de_workspace_current_pos.left;
				drag_toy.options.workspace_y1 = de_workspace_current_pos.top;
				drag_toy.options.workspace_x2 = de_workspace_current_pos.left+de_workspace.getWidth();
				drag_toy.options.workspace_y2 = de_workspace_current_pos.top+de_workspace.getHeight();
				if (drag_toy.element.getStyle('position')=='absolute'){
					drag_toy.element.style.left = parseInt(drag_toy.element.getStyle('left')) - imageDelta + 'px';
				}
			});
			currentPageWidth = newPageWidth;
		}
	});
	var max_single_height=0;
	$('body').select('div[class=block os11]').each(function(el){
		if (el.getHeight()>max_single_height)max_single_height=el.getHeight();
	});
	$('body').select('div[class=c os12]').each(function(el){
		el.style.height=(max_single_height-20)+'px';
	});
});
function saveTree(div_id, load_id, name_id, text_id)
{
	var toys = new Hash;
	Draggables.drags.each(function(drag_toy1) {
		if(drag_toy1.isInWorkspace())
		{
			var pos = drag_toy1.getToyPosition();
			toys.set("toy_x["+drag_toy1.element.identify()+"]", pos.x);
			toys.set("toy_y["+drag_toy1.element.identify()+"]", pos.y);
			toys.set("toy_zindex["+drag_toy1.element.identify()+"]", drag_toy1.element.getStyle("z-index"));
		} else {
			toys.set("toy_x["+drag_toy1.element.identify()+"]", 0);
			toys.set("toy_y["+drag_toy1.element.identify()+"]", 0);
			toys.set("toy_zindex["+drag_toy1.element.identify()+"]", 1);
		}
	});

	toys.set("tree_text", $(text_id).value);
	toys.set("tree_name", $(name_id).value);

	new Ajax.Request("/tree/save", {
		parameters: toys,
		onLoading: function(){
			$(div_id).hide();
			$(load_id).show();
		},
		onComplete: function() {
			$(load_id).hide();
			$(div_id).show();
		}
	});
}
function deleteComment(id, update_menu)
{
	new Ajax.Updater("comment_block_actions_id_"+id, "/congratulation/delete/id/"+id, {
		onLoading: function() {
			$('comment_block_actions_id_'+id).hide();
			$('comment_block_loader_id_'+id).show();
		},
		onComplete: function() {
			$('comment_block_loader_id_'+id).hide();
			$('comment_block_actions_id_'+id).show();
			$('comment_block_id_'+id).morph('background: #F8E3E5;');
			if (update_menu){
				$('user_panel_congratulation_count').innerHTML = parseInt($('user_panel_congratulation_count').innerHTML)-1;
				if($('user_panel_congratulation_count').innerHTML=="0") $('user_panel_congratulation').hide();
			}
		},method:'get'
	});
}

function approveComment(id, update_menu){
	new Ajax.Updater("comment_block_actions_id_"+id, "/congratulation/approve/id/"+id, {
		onLoading: function() {
			$('comment_block_actions_id_'+id).hide();
			$('comment_block_loader_id_'+id).show();
		},
		onComplete: function() {
			$('comment_block_loader_id_'+id).hide();
			$('comment_block_actions_id_'+id).show();
			$('comment_block_id_'+id).morph('background: #E4F8E3;');
			if (update_menu){
				$('user_panel_congratulation_count').innerHTML = parseInt($('user_panel_congratulation_count').innerHTML)-1;
				if($('user_panel_congratulation_count').innerHTML==0) $('user_panel_congratulation').hide();
			}
		},method:'get'
	});
}

function presentSelectImage(id){
	if (!gift_animation){
		if (!$('select_present_size_block').visible()){
			$('select_present_size_block_none').hide();
			$('select_present_size_block_conteiner').style.height = '140px';
			$('select_present_size_block').show();
		}
		if(id!=null) {
			selected_present_image_id = id;
			$("hidden_present_id").value = id;
			$("small").checked = true;
		}
		presentChangeImageSize('32');
	}
}

function presentChangeImageSize(size){
	$('access_code_0').hide();
	$('access_code_32').hide();
	$('access_code_48').hide();
	$('access_code_64').hide();
	$('access_code_128').hide();
	$('access_code_'+size).show();
	
	img1 = $('select_present_image_'+hidden_gift_image);
	img1.className='gift-'+size+'-'+selected_present_image_id;
	img1.style.width = size+'px';
	img1.style.height = size+'px';
	gift_animation = true;
	new Effect.Parallel([new Effect.Fade('select_present_image_'+visible_gift_image,{sync:true}),new Effect.Appear('select_present_image_'+hidden_gift_image,{sync:true})],{afterFinish:function(){
		var k =visible_gift_image;
		visible_gift_image = hidden_gift_image;
		hidden_gift_image = k;
		gift_animation = false;
	}});
}

function highlihtUserPresnets(id){
	new Effect.Opacity($(id), { from: 1.0, to: 0.5, duration: 0.3, queue: { position: 'end', scope: 'user_given_presents' } });
	new Effect.Opacity($(id), { from: 0.5, to: 1.0, duration: 0.3, queue: { position: 'end', scope: 'user_given_presents' } });
	setTimeout("highlihtUserPresnets('"+id+"')", (randomValue(1000,5000)));
}

function randomValue(min, max){
	return Math.floor((max-min-1)*Math.random()) + min;
}

function addPresentOrComment()
{
	new Ajax.Updater('present_add_form_block', (send_present ? "/congratulation/addPresent" : "/congratulation/add"), {
		parameters: $('present_form').serialize(true),
		evalScripts: true,
		onLoading: function() {
			if($('present_from_error')) $('present_from_error').hide();
			$('present_or_comment_add_button').hide();
			$('present_or_comment_add_loading').show();
		},
		onComplete: function() {
			$('present_or_comment_add_loading').hide();
			$('present_or_comment_add_button').show();
			Effect.ScrollTo('send_pay_message');
		},method:'get'
	});
}
function changePresentType(){
	send_present = !send_present;
	if (send_present){
		$('present_or_comment_add_button').value = 'подарить подарок!';
	}else{
		$('present_or_comment_add_button').value = 'отправить бесплатное поздравление!';
	}
	new Effect.toggle('select_present_block','blind');
	new Effect.ScrollTo('send_pay_message');
	return true;
}
function changeNumbers(country_iso,size){
	var tmp = document.createElement('span'),tmp1 = document.createElement('span');
	tmp.innerHTML = countries[size][country_iso].text;
	var item = $('sms_text_'+size);
	while (item.childNodes[0])item.removeChild(item.childNodes[0] );
	item.appendChild(tmp);
	tmp1.innerHTML = countriesLow[size][country_iso] ? 'Если у вас не отправляется SMS на номер '+countries[size][country_iso].number+' отправьте SMS'+countriesLow[size][country_iso].text : '';
	tmp1.className='sms_m_text';
	item = $('sms_low_'+size);
	while (item.childNodes[0])item.removeChild(item.childNodes[0] );
	item.appendChild(tmp1);
}
function opWin(url){ window.open (url, "sociableWindow", "status=0,toolbar=0,location=0,menubar=0"); }