function selectCatalogCity( select, url )
{
	if( select && select.options.length ){
		if( select.options[select.selectedIndex].value ){
			document.location.href = url + '?city=' + select.options[select.selectedIndex].value; 
		}
	}
}

var menu_hidden = false;

function animateTopMenu( count ){
	
	//$("#menu_1").animate({
	//			opacity: 'toggle'
	//}, 500);
	
	
	if( menu_hidden ){
		for( i=0; i < count; i++ ){
			$("#main_menu_item_" + i).animate({
					opacity: 'toggle'
			}, 300 + (i * 80));
		}
		$("#podmenu").animate({
					opacity: 'toggle'
			}, 300 + (i * 80));
		menu_hidden = false;
	} else {		
		
		
		for( i=(count - 1), j=0; i >= 0; i--, j++ ){
			$("#main_menu_item_" + i).animate({
					opacity: 'toggle'
			}, 300 + (j * 80));
		}
		$("#podmenu").animate({
					opacity: 'toggle'
			}, 300 + (j * 80));
		menu_hidden = true;
	}
	
	
}



function ajaxVote( id )
{
	try{
		if( id ){
			var url = '/vote.html?vmode=1&vote=' + id;
			var xml = new JKL.ParseXML( url );
			var xmldata = xml.parse();
			
			if(xmldata.data.result){
				$('.vote_link').hide();
			} else {
				//some error occurs. let it be
			}
		} else {
			alert('Oh, no!!!! You don\'t send me valid ID!!! Why did you do it?!');
		}
	} catch(e) {
		alert(e);
	}
}

function unroll() {
	var unroll = document.getElementById('_unroll');
	unroll.style.height = '619px';
	document.unroll.SetVariable('stage_height', '619');
}
function roll() {
	var unroll = document.getElementById('_unroll');
	unroll.style.height = '260px';
	document.unroll.SetVariable('stage_height', '260');
}

function toggle_tab( elem_id )
{
	if( elem_id ){
		$('.post_tabs').css({
				display: 'none'
		});
		
		$("#" + elem_id).animate({
					opacity: 'toggle'
		}, 300);
	}
}

var files_attach_count = 0;
var files_attach_max   = 10;

function ajaxImageUpload()
{
	/*
	if(files_attach_count > files_attach_max){
		alert('Вы можете добавить только 10 изображений');
		$('#img_upload_input').hide();
		return;
	} else {
		$('#img_upload_input').show();
	}
	*/

	

	$("#image_loading")
	.ajaxStart(function(){
		$(this).show();
		$("#go1").hide();
	})
	.ajaxComplete(function(){
		$(this).hide();
		$("#go1").show();
	});


	$.ajaxFileUpload
	(
		{
			url:'/file_upload?type=image',
			secureuri:false,
			fileElementId:'image_upload',
			dataType: 'json',
			success: function (data, status)
			{
				
				if( data.error ){
					alert(data.error);
				} else {
					
					files_attach_count++;
					if( files_attach_count == files_attach_max ){
						$('#img_upload_input').hide();
					}
					
					var url = '/post_image_preview?action=preview&id=' + data.image;
					//Adding preview
					var html = '';
					html += '<table id="img_preview_table_' + data.image + '"  style="display: inline; margin: 10px 10px 10px 10px; width: 60px; height: 60px;"><tr>'
					html += '<td>';
					html += '<div style="padding-bottom: 10px; width: 60px; height: 60px;">';
					html += '<img src="' + url + '" alt="" width="' + data.width + '" height="' + data.height + '">';
					html += '</div>';
					html += '<div align="center" style="">';
					html += '<a href="#" style="color: black; font-size: 9px;" onClick="imageDeattach(\'' + data.image + '\'); return false;">удалить</a>';
					html += '</div>';											
					html += '</td>';
					html += '</tr></table>';
					
					$('#img_upload_preview').append(html);
					
					return true;
					
				}
				
				/*
				
				if(data.id)
				{
					files_attach_count++;
					if( files_attach_count == files_attach_max ){
						$('#img_upload_input').hide();
					}
					
					//Adding preview
					var html = '';
					html += '<table id="img_preview_table_' + data.id + '"  style="display: inline; margin: 10px 10px 10px 10px; width: 60px; height: 60px;"><tr>'
					html += '<td>';
					html += '<div style="padding-bottom: 10px; width: 60px; height: 60px;">';
					html += '<img src="/work_preview.php?id=' + data.id + '" alt="" width="60" height="60">';
					html += '</div>';
					html += '<div align="center" style="">';
					html += '<a href="#" onClick="imageDeattach(\'' + data.id + '\'); return false;">удалить</a>';
					html += '</div>';											
					html += '</td>';
					html += '</tr></table>';
					
					$('#img_upload_preview_div').append(html);
					
					return true;
					
				} else {
					if(data.error != ''){
						alert('Во время загрузки произошла ошибка');
					}
					return true;
				}
				
				*/ 
			},
			error: function (data, status, e)
			{
				//alert(e);
			}
		}
	)
	
	return false;

}

function ajaxVideoUpload()
{
	/*
	if(files_attach_count > files_attach_max){
		alert('Only 3 files accepted');
		$('#img_upload_input').hide();
		return;
	} else {
		$('#img_upload_input').show();
	}
	*/
	
	$("#video_loading")
	.ajaxStart(function(){
		$(this).show();
		$("#go1").hide();
	})
	.ajaxComplete(function(){
		$(this).hide();
		$("#go1").show();
	});

	$.ajaxFileUpload
	(
		{
			url:'/file_upload?type=video',
			secureuri:false,
			fileElementId:'video_upload',
			dataType: 'json',
			success: function (data, status)
			{
				
				if(data.image)
				{
					/*
					files_attach_count++;
					if( files_attach_count == files_attach_max ){
						$('#img_upload_input').hide();
					}
					*/
					
					//Adding preview
					var html = '';
					html += '<table id="video_preview_table_' + data.image + '"  style="display: inline; margin: 10px 10px 10px 10px; width: 60px; height: 60px;"><tr>'
					html += '<td>';
					html += '<div style="padding-bottom: 10px; width: 80px;">';
					html += '<img src="/post_video_preview?action=preview&id=' + data.image + '" alt="Файл: ' + data.name + '; Размер: ' + data.size + '" width="80">';
					html += '</div>';
					html += '<div align="center" style="">';
					html += '<a href="#" style="color: black; font-size: 9px;" onClick="videoDeattach(\'' + data.image + '\'); return false;">удалить</a>';
					html += '</div>';											
					html += '</td>';
					html += '</tr></table>';
					
					$('#video_upload_preview').append(html);
					
					return true;
					
				} else {
					if(data.error != ''){
						alert(data.error);
					}
					return true;
				}
				
			},
			error: function (data, status, e)
			{
				//alert(e);
			}
		}
	)
	
	return false;

}

function ajaxMP3Upload()
{
	/*
	if(files_attach_count > files_attach_max){
		alert('Only 3 files accepted');
		$('#img_upload_input').hide();
		return;
	} else {
		$('#img_upload_input').show();
	}
	*/
	
	$("#mp3_loading")
	.ajaxStart(function(){
		$(this).show();
		$("#go1").hide();
	})
	.ajaxComplete(function(){
		$(this).hide();
		$("#go1").show();
	}); 

	$.ajaxFileUpload
	(
		{
			url:'/file_upload?type=mp3',
			secureuri:false,
			fileElementId:'mp3_upload',
			dataType: 'json',
			success: function (data, status)
			{
				if(data.id)
				{
					/*
					files_attach_count++;
					if( files_attach_count == files_attach_max ){
						$('#img_upload_input').hide();
					}
					*/
					
					//Adding preview
					var html = '';
					
					html += '<div style="font-size: 11px; border: 1px solid #969696; padding: 2px 2px 2px 2px; margin: 2px 2px 2px 10px;" id="mp3_upload_preview_' + data.id + '">';
					html += 'Файл: <b>' + data.name + '</b><br>';
					html += 'Размер: <b>' + data.size + '</b><br>';
					html += '<a href="#" style="color: black; font-size: 9px;" onClick="mp3Deattach(\'' + data.id + '\'); return false;">удалить</a>';
					html += '</div>'
					
					
					$('#mp3_upload_preview').append(html);
					
					return true;
					
				} else {
					if(data.error != ''){
						alert(data.error);
					}
					return true;
				}
			},
			error: function (data, status, e)
			{
				//alert(e);
			}
		}
	)
	
	return false;

}


function imageDeattach( id )
{
	var url = '/post_image_preview?action=deattach&id=' + id;
	var params = { id: id };
	
	try{		
		$.ajax({
				url: url, 
				type: 'post',  
				dataType: 'json',
				success: function( data ){
					
					//alert(data.result);
					
					if(data.result){
						
						$('#img_preview_table_' + id).remove();
						$('#img_upload_input').show();
						files_attach_count--;
					}
				}, 
				data: params
				});
	} catch(e){
		//alert(e);
	}
}

function videoDeattach( id )
{
	var url = '/post_video_preview?action=deattach&id=' + id;
	var params = { id: id };
	
	try{		
		$.ajax({
				url: url, 
				type: 'post',  
				dataType: 'json',
				success: function( data ){
					
					//alert(data.result);
					
					if(data.result){
						
						$('#video_preview_table_' + id).remove();
						//$('#img_upload_input').show();
						//files_attach_count--;
					}
				}, 
				data: params
				});
	} catch(e){
		//alert(e);
	}
}

function mp3Deattach( id )
{
	var url = '/post_mp3_preview?action=deattach&id=' + id;
	var params = { id: id };
	
	try{		
		$.ajax({
				url: url, 
				type: 'post',  
				dataType: 'json',
				success: function( data ){
					
					//alert(data.result);
					
					if(data.result){
						
						$('#mp3_upload_preview_' + id).remove();
						//$('#img_upload_input').show();
						//files_attach_count--;
					}
				}, 
				data: params
				});
	} catch(e){
		//alert(e);
	}
}

function imageDelete( id )
{
	var url = '/post_image_preview?action=delete&id=' + id;
	var params = { id: id };
	
	try{		
		$.ajax({
				url: url, 
				type: 'post',  
				dataType: 'json',
				success: function( data ){
					
					//alert(data.result);
					
					if(data.result){
						
						$('#img_preview_table_' + id).remove();
						$('#img_upload_input').show();
						files_attach_count--;
					}
				}, 
				data: params
				});
	} catch(e){
		//alert(e);
	}
}

function videoDelete( id )
{
	var url = '/post_video_preview?action=delete&id=' + id;
	var params = { id: id };
	
	try{		
		$.ajax({
				url: url, 
				type: 'post',  
				dataType: 'json',
				success: function( data ){
					
					//alert(data.result);
					
					if(data.result){
						
						$('#video_preview_table_' + id).remove();
						//$('#img_upload_input').show();
						//files_attach_count--;
					}
				}, 
				data: params
				});
	} catch(e){
		//alert(e);
	}
}

function mp3Delete( id )
{
	var url = '/post_mp3_preview?action=delete&id=' + id;
	var params = { id: id };
	
	try{		
		$.ajax({
				url: url, 
				type: 'post',  
				dataType: 'json',
				success: function( data ){
					
					//alert(data.result);
					
					if(data.result){
						
						$('#mp3_upload_preview_' + id).remove();
						//$('#img_upload_input').show();
						//files_attach_count--;
					}
				}, 
				data: params
				});
	} catch(e){
		//alert(e);
	}
}
