﻿$(document).ready(function()
{
	$('#afisha_list .item').click(function()
	{
		ShowAfishaFull(this);
	});
	
	$('#gallery_items .item.image').click(function()
	{
		ShowGalleryImage(this);
	});

	$('#gallery_items').css('min-height', Math.max(900, Math.ceil($('#gallery_items .item').length / 4) * 140 + 100));

});





function ShowAfishaFull(sender)
{
	var site = document.getElementById('site')

	var afBackground = document.createElement("div");
	$(afBackground).attr('id','afBackground');
	site.appendChild(afBackground);
	/*
	var afContainer = document.createElement("div");
	$(afContainer).attr('id','afContainer');
	site.appendChild(afContainer);
	*/
	var afImage = document.createElement("img");
	$(afImage).attr('id','afImage');
	$(afImage).attr('src', $(sender).attr('src'));
	$(afImage).attr('loc_x', $(sender).offset().left);
	$(afImage).attr('loc_y', $(sender).offset().top);
	$(afImage).attr('loc_w', $(sender).width());
	$(afImage).attr('loc_h', $(sender).height());
	site.appendChild(afImage);
	$(afImage).css(
		{
			top		: $(sender).offset().top,
			left	: $(sender).offset().left,
			width	: $(sender).width(),
			height	: $(sender).height()
		});

	var max_width = $(sender).attr('img_width');
	var max_height = $(sender).attr('img_height'); 
	if($(sender).attr('img_width') > $(sender).attr('img_height'))
	{
		max_width = Math.min($(sender).attr('img_width'), f_clientWidth() - 200);
		max_height = max_width * max_height / $(sender).attr('img_width'); 
	}
	else
	{
		max_height = Math.min($(sender).attr('img_height'), f_clientHeight() - 100);
		max_width = max_width * max_height / $(sender).attr('img_height'); 

	}
	
	$(afImage).animate(
		{
			top		: Math.max(50, f_scrollTop() + ( f_clientHeight() - max_height) / 2) + 'px',
			left	: (f_clientWidth() - max_width) / 2 + 'px',
			width	: max_width + 'px',
			height	: max_height + 'px'

		}, 300);
	
	$(afBackground).bind('click', HideAfishaFull);
	$(afImage).bind('click', HideAfishaFull);



}

function HideAfishaFull()
{
	$('#afBackground').remove();
	var afImage = $('#afImage');
	$(afImage).animate(
		{
			top		: $(afImage).attr('loc_y') + 'px',
			left	: $(afImage).attr('loc_x') + 'px',
			width	:  $(afImage).attr('loc_w') + 'px',
			height	:  $(afImage).attr('loc_h') + 'px',
			opacity : 0.5
		}, 300, null, function(){$('#afImage').remove();});
}




function ShowGalleryImage(sender)
{
	var site = document.getElementById('site')

	var galBackground = document.createElement("div");
	$(galBackground).attr('id','afBackground');
	site.appendChild(galBackground);
	
	var galTable = document.createElement("table");
	$(galTable).attr('id','galTable');
	$(galTable).attr('align','center');

	$(galTable).html('<tr> \
						<td id="go_prev_cont"><div id="go_prev"/></td> \
						<td id="img_cont"><img id="galImage"/></td> \
						<td id="go_next_cont"><div id="go_next"/></td> \
					</tr> \
					<tr> \
						<td></td> \
						<td id="title_cont" class="item_text"></td> \
						<td></td> \
					</tr> ');
	
		$(galTable).css(
		{
			top		: f_scrollTop() + 50 + 'px'
		});
		
		$('#go_prev, #go_next', galTable).css(
		{
			height	: f_clientHeight() - 100 + 'px'
		});
		$('#galImage', galTable).css('max-height', f_clientHeight() - 100 + 'px');
		$('#galImage', galTable).css('max-width', f_clientWidth() - 100 + 'px');

		
	$('#galImage', galTable).attr('src', $(sender).attr('image'));
	
	site.appendChild(galTable);
}




function Subscribe()
{
	
	$('#news_subscribtion').load('/', {ajax 	: true,
									   name 	: 'subscribe',
									   params 	: 'type='+$('#news_subscribtion').attr('type')+'[,]email='+$('#news_subscribtion #SubscrEmail').val()});
	$('#news_subscribtion').text('Пожалуйста подождите...');
}



