var byteToKb = function($size) 
{
	$gb = Math.ceil($size/1073741824*1000)/1000;
	if(parseInt($gb) > 0)
	{
		return $gb + ' Gb'; 
	}

	$mb = Math.ceil($size/1048576*1000)/1000;	
	if(parseInt($mb) > 0)
	{
		return $mb + ' Mb'; 
	}
	
  	$kb = Math.ceil($size/1024*1000)/1000;
	if(parseInt($kb) > 0)
	{
		return $kb + ' Kb'; 
	}
	
	return $size + ' b';
}

jQuery.fn.center = function() 
{
	var w = $(window);
	this.css("position","absolute");
	this.css("top",(w.height()-this.height())/2+w.scrollTop()+30 + "px");
	this.css("left",(w.width()-this.width())/2+w.scrollLeft() + "px");
	
	
	this.css("display", "block");
	
	return this;
}

jQuery.fn.fullScreen = function() 
{
	this.css("position","absolute");
	$(this).height($(document).height())
	$(this).width($(document).width());
	return this;
}

var Photo = {
	showLinks: function(url, info)
	{
		eval('info =' + info);
		
		$img = '<span><img src="'+info.thumbs.imv.url+'" width="'+info.thumbs.imv.width+'" height="'+info.thumbs.imv.height+'"/></span>';
		
		if(!$('.ajax-loader').is(':visible'))
		{
			$('.popup-wrap').show();
			$('.popup-wrap').fullScreen();
			$('.popup-wrap .bg').fullScreen();
								
			$id = 'popup_photo_links';
			if($('#'+$id).length)
			{
				$('#'+$id).show();
				$('#'+$id).center();
				
				$('#'+$id+' .photo-visual').html($img);
				$('#'+$id+' #link_html').val(info.links.html);
				$('#'+$id+' #link_direct').val(info.links.direct);
			} else
			{
				$.ajax
				({
					url: url,
					data: {popup: $id},
					cache: true,
					success: function(response)
					{
						$('.popup-wrap').append(response);
						$('#'+$id).center();
						
						$('#'+$id+' .photo-visual').html($img);
						$('#'+$id+' #link_html').val(info.links.html);
						$('#'+$id+' #link_direct').val(info.links.direct);
					}
				});
			}
		}
	},

	showShare: function(url, info)
	{
		eval('info =' + info);
		
		$img = '<span><img src="'+info.thumbs.imv.url+'" width="'+info.thumbs.imv.width+'" height="'+info.thumbs.imv.height+'"/></span>';
		
		if(!$('.ajax-loader').is(':visible'))
		{
			$('.popup-wrap').show();
			$('.popup-wrap').fullScreen();
			$('.popup-wrap .bg').fullScreen();
								
			$id = 'popup_photo_share';
			if($('#'+$id).length)
			{
				$('#'+$id).show();
				$('#'+$id).center();
				
				$('#'+$id+' .photo-visual').html($img);
				$('#'+$id+' #link_html').val(info.links.html);
				$('#'+$id+' #link_direct').val(info.links.direct);
			} else
			{
				$.ajax
				({
					url: url,
					data: {popup: $id},
					cache: true,
					success: function(response)
					{
						$('.popup-wrap').append(response);
						$('#'+$id).center();
						
						$('#'+$id+' .photo-visual').html($img);
						$('#'+$id+' #link_html').val(info.links.html);
						$('#'+$id+' #link_direct').val(info.links.direct);
					}
				});
			}
		}
	},
	
	showEdit: function(url, info)
	{
		eval('info =' + info);
		
		$img = '<img src="'+info.thumbs.imv.url+'" width="'+info.thumbs.imv.width+'" height="'+info.thumbs.imv.height+'"/>';
		
		if(!$('.ajax-loader').is(':visible'))
		{
			$('.popup-wrap').show();
			$('.popup-wrap').fullScreen();
			$('.popup-wrap .bg').fullScreen();
								
			$id = 'popup_photo_edit';
			if($('#'+$id).length)
			{
				$('#'+$id).show();
				$('#'+$id).center();
				
				$('#'+$id+' .image-holder .frame').html($img);
				$('#'+$id+' #album-name').val(info.info.name);
				$('#'+$id+' #tags').val(info.info.tags);
				$('#'+$id+' #description').val(info.info.description);
				$('#'+$id+' input[name="image_id"]').val(info.info.image_id);
			} else
			{
				$.ajax
				({
					url: url,
					data: {popup: $id},
					cache: true,
					success: function(response)
					{
						$('.popup-wrap').append(response);
						$('#'+$id).center();
						
						$('#'+$id+' .image-holder .frame').html($img);
						$('#'+$id+' #album-name').val(info.info.name);
						$('#'+$id+' #tags').val(info.info.tags);
						$('#'+$id+' #description').val(info.info.description);
						$('#'+$id+' input[name="image_id"]').val(info.info.image_id);
					}
				});
			}
		}
	},
	
	"delete": function(url, id)
	{
		$.ajax
		({
			url: url,
			cache: false,
			success: function(response)
			{
				eval('response ='+response);
				if(typeof(response.errors.msg) != 'undefined')
				{
					alert(response.errors.msg);
				} else
				{
					$('#file_'+id).remove();
				}
			}
		});
	}
}


var Album = 
{
	itemsDelete: function(url)
	{
		$image = new Array();
		$.each($('#photo').serializeArray(), function(){
			$image.push(this.value);
		});
		
		if(!$('.ajax-loader').is(':visible'))
		{
			$.ajax
			({
				url: url,
				data: {image: $image},
				cache: false,
				type: 'POST',
				success: function(response)
				{
					$.each($image, function(){
						$('#file_'+this).remove();
					});
				}
			});
		}
			
		return false;
	},
	itemsMove: function(url)
	{
		$image = new Array();
		$.each($('#photo').serializeArray(), function(){
			$image.push(this.value);
		});
		
		if(!$('.ajax-loader').is(':visible'))
		{
			$.ajax
			({
				url: url,
				data: {image: $image, move_to: $('select[name="move_to"]').val()},
				cache: false,
				type: 'POST',
				success: function(response)
				{
					$.each($image, function(){
						$('#file_'+this).remove();
					});
				}
			});
		}
			
		return false;
	}
}

$(window).resize(function() 
{
  	$('.popup-wrap').fullScreen();
	$('.popup-wrap .bg').fullScreen();
	//$('.popup-wrap div[class!="bg"]').center();
});


$.ajaxSetup({
	beforeSend: function()
	{
		$('.ajax-loader').css('top', $(window).scrollTop()+10);
		$('.ajax-loader').show();
	},
	complete: function()
	{
		$('.ajax-loader').hide();
	}
});

function ajax_get(e)
{
	$.ajax
	({
		url: $(e).attr('href'),
		cache: true,
		success: function(response)
		{
			eval('var response = '+response);
			if (response.result == true)
			{
				$(e).parent().html(response.msg);
			}
		}
	});
	
	return false;
}

function popup(e)
{
	/* защита от повторного вызова попапа */
	if(!$('.ajax-loader').is(':visible'))
	{
		$('.popup-wrap').children().hide();
		$('.popup-wrap').show();
		$('.popup-wrap').fullScreen();
		$('.popup-wrap .bg').fullScreen();
		$('.popup-wrap .bg').show();
		
		$id = $(e).attr('id');
		$id = 'popup_'+$id;
		if($('#'+$id).length)
		{
			$('#'+$id).show();
			$('#'+$id).center();
		} else
		{
			$.ajax
			({
				url: $(e).attr('href'),
				data: {popup: $id},
				cache: true,
				success: function(response)
				{
					$('.popup-wrap').append(response);

					$('#'+$id).center();
				}
			});
		}
	}
		
	return false;
}

function contextPopup(e)
{
	/* защита от повторного вызова попапа */
	if(!$('.ajax-loader').is(':visible'))
	{
		$('.popup-wrap').show();
		$('.popup-wrap').fullScreen();
		$('.popup-wrap .bg').fullScreen();
							
		$id = $(e).attr('id');
		$id = 'popup_'+$id;
		
		$possition = $(e).offset();
		
		if($('#'+$id).length)
		{
			$('#'+$id).show();
		} else
		{
			$.ajax
				({
					url: $(e).attr('href'),
					cache: true,
					success: function(response)
					{
						$(e).parent().append(response);
						$('#'+$id).show();
					}
				});
		}
	}
		
	return false;
}

/*
function addUpload(e, url)
{
	$count = $('form[name="single_upload"] div[class="row"]').length;
	
	$.ajax
	({
		url: url,
		data: {count: $count},
		type: 'GET',
		cache: true,
		beforeSend: function(){},
		complete: function(){},
		success: function(response)
		{
			$(e).parent().before(response);
		}
	});
		
	return false;
}


function upload(e)
{
	if($(e).hasClass('btn-not-active'))
	{
		return false;
	}
	
	for($i=1; $i<=$('.file').length; $i++)
	{
		eval('upload'+$i+'.startUpload()');
	}
	return false;	
}
*/
function vote(object, object_id, int)
{
	/*
	$.ajax
	({
		url: url,
		data: {count: $count},
		type: 'GET',
		cache: true,
		beforeSend: function(){},
		complete: function(){},
		success: function(response)
		{
			$(e).parent().before(response);
		}
	});
	*/	
}

function share_selected(form_id, e)
{
	$image = new Array();
	$.each($('#'+form_id).serializeArray(), function(){
		$image.push(this.value);
	});
	
	if(!$('.ajax-loader').is(':visible'))
	{
		$('.popup-wrap').show();
		$('.popup-wrap').fullScreen();
		$('.popup-wrap .bg').fullScreen();
							
		$id = $(e).attr('id');
		$id = 'popup_'+$id;
		
		if($('#'+$id).length)
		{
			$('#'+$id).show();
		} else
		{
			$.ajax
				({
					url: $(e).attr('href'),
					data: {image: $image},
					cache: false,
					type: 'POST',
					success: function(response)
					{
						$('.popup-wrap').append(response);
						$('#'+$id).center();
					}
				});
		}
	}
		
	return false;
}


ZeroClipboard.setMoviePath( './js/zeroclipboard/ZeroClipboard10.swf' );
 	
$(document).ready(function()
{
	/*
	jQuery('ul.search-results-list li label img').loadImages({
	    LoadingImage: './images/preloader_7pic_01.gif',
	    FailImage: './images/preloader_7pic_01.gif'
	}, function(){
	    var img = jQuery(arguments[0]);
	    img.attr('width', '100');		
	});
	*/
	$selected = 0;
	
	$('.search-results-list .check').each(function()
	{
		if ($(this).attr('checked') == true) 
		{
			$selected++;
			$(this).parents('li').addClass('selected')
		};
	})
	$('#selected_counter').text($selected);
	
	$('.search-results-list .check').click(function(){
		if ($(this).attr('checked') == true) 
		{
			$selected++;
			$('#selected_counter').text($selected);
			$(this).parents('li').addClass('selected')
		} else 
		{
			$selected--;
			$('#selected_counter').text($selected);
			$(this).parents('li').removeClass('selected')
		}
	})
	
	$('.search-results-list div.checkboxArea').live('click',function()
	{
		$selected--;
		$('#selected_counter').text($selected);
		$(this).parents('li').removeClass('selected');
	});
	
	$('.search-results-list div.checkboxAreaChecked').live('click',function()
	{
		$selected++;
		
		if($selected)
		{
			$('#object_actions').show();
		}
		
		$('#selected_counter').text($selected);
		$(this).parents('li').addClass('selected');
	});
	
	$('#carusel').galleryScroll({
		step:1,
		duration:600,
		holderList: 'div.holder',
		circleSlide:true
	});
	$('.btn-post-subm').mouseenter(function(){
		$(this).addClass('btn-post-subm-hover');
	});
	$('.btn-post-subm').mouseleave(function(){
		$(this).removeClass('btn-post-subm-hover');
	});
	$('.btn-send-new-message').mouseenter(function(){
		$(this).addClass('btn-send-new-message-hover');
	});
	$('.btn-send-new-message').mouseleave(function(){
		$(this).removeClass('btn-send-new-message-hover');
	});
	$('.friend-removal .btn-yes, .popup-request-friend .btn-yes').live('mouseenter', function(){
		$(this).addClass('btn-yes-hover');
	});
	$('.friend-removal .btn-yes, .popup-request-friend .btn-yes').live('mouseleave', function(){
		$(this).removeClass('btn-yes-hover');
	});
	$('.friend-removal .btn-no, .popup-request-friend .btn-no').live('mouseenter', function(){
		$(this).addClass('btn-no-hover');
	});
	$('.friend-removal .btn-no, .popup-request-friend .btn-no').live('mouseleave', function(){
		$(this).removeClass('btn-no-hover');
	});
});
