/* 	Easy Gallery js function
	Author: 	Eli & Kelvin
	you can remove this credit message after you purchase it at codecanyon
	please visit our online portfolio for more awesome items
*/

var g_edit = false; // to control user to edit one row only


function applyFrontendEffect()
{
	
	var p_left = (b_width - s_width) / 2;
	var p_top = (b_height - s_height) / 2;
	
	$('.sg_thumb').css({'left':p_left  +'px','top':p_top  +'px','height':s_height +'px','width':s_width +'px','opacity':po});
	
	$('.sg_photo').css({'margin-top':(p_top * -2) + pp +'px','margin-left':(p_left * -2) + pp +'px','height':b_height +'px','width':b_width +'px'});
	
	$('.sg_gallery_wrapper').css({'margin-top':gwt +'px','margin-left':p_left - pp + gwl +'px'});
	
	
	//enlarge thumbnail and show pic title
	$('.sg_thumb')
		.hover(function(){
			$(this)
				.addClass("sg_thumb_shadow")
				.stop()
				.animate({
					width: b_width,
					height: b_height,
					top: 0,
					left: 0,
					opacity: 1
				},300);
			$(this).css("z-index" , "1001");
			
		var pT = $(this).parent().children('.sg_tooltip').html();	
		//$('body').append('<div class="sg_tooltip_box">' + pT + '</div>');	
				
		$('.sg_tooltip_box').fadeIn('slow');
		$('.sg_tooltip_box').fadeTo('10',1);
		
		$(this).parent().mousemove(function(e) {
		
		 e=e || window.event;
	
		$('.sg_tooltip_box').css({'top':e.pageY+30,'left':e.pageX-5});
		});
		
			
		},
		function(){
			$(this)
				.removeClass("sg_thumb_shadow")
				.stop()
				.animate({
					width: s_width,
					height: s_height,
					top: p_top +'px',
					left: p_left +'px',
					opacity: po
				},100);
			$(this).css("z-index" , "1");
			
			$('body').children('.sg_tooltip_box').remove();
		});
		
			
		
		
		
	//show hide category list					   
			
		$("ul.sg_category_list").hide(); 
		$(".sg_category .sg_box .sg_current a").click( 
			function () {
				$("ul.sg_category_list").slideToggle("normal");
				return false;
			}
		);	
		
		
	// Search form style control:
		$('input.textfield').focus(function() {  
			$(this).parent().css("background-position", "0 -21px");  
			});  
		$('input.textfield').blur(function() {  
			$(this).parent().css("background-position", "0 0");  
			});  

	
		
		//Original soruce from: http://colorpowered.com/colorbox/
		$("a[rel='colorbox']").colorbox({slideshow:CBss , slideshowSpeed:CBspeed ,title: function(){
    		//var CBpT = $(this).attr('pictitle');
    		//return CBpT;
		}});
		 
		
}

function validateName(str)
{
        var re = /['":<>\\\/\|\*\?"]/;
        if (re.test(str)) {
                alert(invalid_name_msg);
                return false;
        }else{
                return true;
        }
}

function deleteImage(image_id)
{
	if(confirm(remove_img_msg))
	{
		
		$.post("includes/php/ajax.php", {task: 'delete_image',ImageID:image_id},  
			function(data, textStatus){
				document.form1.submit();
			}
				
		);
	}
}

function cropImage(image_id)
{
	if(g_edit==true)
	{
		alert(editing_warn_msg);	
		
	}
	else
	{
		window.location="crop.php?image_id="+image_id;	
	}
}

// go to next page
function ajax_gopage(page, obj)
{
	obj.pageNo.value=page;
	document.form1.submit();
}

// change record view per page
function ajax_changeRecordPerPage(obj,size)
{
	obj.pageNo.value=1;
	obj.recordPerPage.value=size;
	document.form1.submit();	
}


function editImage(image_id,status)
{
	
	if(g_edit==true)
	{
		alert(editing_warn_msg);	
		
	}
	else
	{
		displayAjaxLoading(image_id+'_ul');
		
		var ul = image_id+'_ul';
		
		$.post("includes/php/ajax.php", {task: 'edit_image',ImageID:image_id},  
			function(data, textStatus)
			{

				$('#'+ul).html(data);
				
				var btn_html = '<input name="button" class="sg_action_btn sg_submit" type="button" onClick="saveImageInfo(\''+image_id+'\');" value="Save"/>';
				btn_html +='<input class="sg_action_btn sg_cancel" type="button" onClick="cancelEdit(\''+image_id+'\','+status+');" value="Cancel"/>';
				
				var btn = image_id+'_btn';
				$('#'+btn).html(btn_html);
				
				// set it to edit mode, prevent user to edit other row
				g_edit = true;
			}
				
		);		
	}
		
	
}

function displayAjaxLoading(div)
{
	$('#'+div).html('<div class="sg_admin_loading">Loading .......</div>');
}

function cancelEdit(image_id,image_status)
{
	// set edit mode to false 
	g_edit = false;
	
	var ul = image_id+'_ul';
	var btn = image_id+'_btn';
	displayAjaxLoading(ul);

		
	$.post("includes/php/ajax.php", {task: 'cancel_edit',ImageID:image_id},
			function(data, textStatus)
			{
				$('#'+ul).html(data);
				
				var btn_html = (image_status==1)?'<a href="javascript:hideImage(\''+image_id+'\')" class="sg_image_show" title="Hide"></a>':'<a href="javascript:showImage(\''+image_id+'\')" class="sg_image_hide" title="Show"></a>';
				btn_html += '<a href="javascript:editImage(\''+image_id+'\')" class="sg_eidt_btn" title="edit"></a>';
				btn_html += '<a href="javascript:cropImage(\''+image_id+'\')" class="sg_image_crop" title="crop image"></a>';
				btn_html += '<a href="javascript:deleteImage(\''+image_id+'\')" class="sg_del_btn" title="delete"></a>';
				$('#'+btn).html(btn_html);
			
			}
				
		);	
}

function resetPaginationValue()
{
	$('#pageNo').val(1);
	$('#recordPerPage ').val(10);
	
}

function saveImageInfo(image_id)
{	
	var pageNo = ($('#pageNo').length)?$('#pageNo').val():'';
	var image_name 	= $('#'+image_id+'_img_name').val();
	var category_radio	= image_id+'_category[]';
	var category_id = [];
	$('input:checkbox[name='+category_radio+']:checked').each(function() {
	   category_id.push($(this).val());
	   
     });
    var description = $('#'+image_id+'_description').val();

	
	var valid = validateName(image_name);
	
	if(valid==true)
	{
		$.post("includes/php/ajax.php", {task: 'save_image_info',image_id:image_id,image_name:image_name,'category_id[]':category_id,pageNo:pageNo,description:description},
			function(data, textStatus)
			{
				g_edit = false;
				var msg = 'Image updated';
				$('#msg').val(msg);
				document.form1.submit();
			}
				
		);
	}
	
}

function updateCategoryEnterEvent(e)
{
	// if press enter in textbox
	if(e.keyCode == 13)
	{
		addNewCategory();
	}
}

function addNewCategory()
{
	var new_category = ($('#new_folder').length)?$('#new_folder').val():'';
		var valid = validateName(new_category);
		if(valid==true)
		{
			g_edit = false;
						
			$.post("includes/php/ajax.php", {task: 'add_new_category',new_category:new_category},  
				function(data, textStatus){
					if(data==1)
						var msg = 'Category added';
					else
						var msg = 'Failed to add category';
					$('#msg').val(msg);
					document.form1.submit();
				
				}
					
			);
		}	
}

function addCategoryRow(jQtable)
{
	if(g_edit==true)
	{
		alert(editing_warn_msg);	
	}
	else
	{
		jQtable.each(function(){
	        var $table = $(this);
	        // Number of td's in the last table row
	        var td_count = $('tr:last td', this).length;
	        var tr_count = $('tr', this).length ;
	        
	        // if there is no folder, remove the row that said "No file"
	        if(td_count==1)
	        {
				removeLastRow();
				tr_count = 1;
			}
	        
	        var css = tr_count%2==0?'class="alt_row"':'';
	        
	        var tds = '<tr '+css+' id="new_row">';
	        tds += '<td>&nbsp;</td><td><input type="text" name="new_folder" id="new_folder" class="pic_name" onKeyDown="updateCategoryEnterEvent(event);" onBlur="addNewCategory();"/></td><td>--</td><td>--</td><td><a href="javascript:removeThisRow();" class="del_btn" title="delete"></a></td>';
	        tds += '</tr>';
	        
	        if($('tbody', this).length > 0){
	            $('tbody', this).prepend(tds);
	        }else {
	            $(this).prepend(tds);
	        }
	    });
	    g_edit = true;
    }
}

function deleteCategory(cat_id)
{ 	
	if(confirm(remove_cat_msg))
	{
		$.post("includes/php/ajax.php", {task: 'delete_category',cat_id:cat_id},  
				function(data, textStatus)
				{
					$('#msg').val(data);
					document.form1.submit();
				}
			);
	}
}

function loadNoticeBoxMsg(msg)
{	
	$('#notice_box').html(msg);
}

$('#category_table tr').click(function () {
    $(this).remove();

    return false;
});



function removeLastRow(row)
{
	$('#category_table tr:last').remove();
	
	g_edit=false;
}

function removeThisRow()
{
	
	$('#new_row').remove();
	g_edit=false;
}

function uploadImageCatSelected()
{
	var cat_id		= $('input:checkbox[name=cat_id[]]:checked').val();
	var image		= $('#image').val();
	if(typeof(cat_id)=='undefined')	
		return false
	else
		return true;	
}
function uploadImage()
{
	if(uploadImageCatSelected()==false)
		alert(select_category_msg);
	else	
	{
		$('#uploading').toggle();	
		$('#uploading').html(upload_image_msg);
		document.form1.submit();	
	}
}

function addUpload()
{
	$('#upload_file_td').append('<input type="file" name="file[]" id="file" /><br />');
}

function hideImage(image_id)
{
	if(confirm(confirm_hide_image))
	{
		displayAjaxLoading(image_id+'_ul');
		$.post("includes/php/ajax.php", {task: 'hide_image',ImageID:image_id},  
			function(data, textStatus){
				cancelEdit(image_id,0)
			}
					
			);
	}
}

function showImage(image_id)
{
	if(confirm(confirm_show_image))
	{
		displayAjaxLoading(image_id+'_ul');
		$.post("includes/php/ajax.php", {task: 'show_image',ImageID:image_id},  
			function(data, textStatus){
				cancelEdit(image_id,1)
			}
					
			);
	}
}

function hideCategory(cat_id)
{
	if(confirm(confirm_hide_category))
	{
		
		$.post("includes/php/ajax.php", {task: 'hide_category',CatID:cat_id},  
			function(data, textStatus){
				document.form1.submit();
			}
					
			);
	}
}

function showCategory(cat_id)
{
	if(confirm(confirm_show_category))
	{
		
		$.post("includes/php/ajax.php", {task: 'show_category',CatID:cat_id},  
			function(data, textStatus){
				document.form1.submit();
			}
					
			);
	}
}
