/* 
	author: istockphp.com
*/
var popupStatus = 0; // set value
jQuery(function($) {
	
	_image_update_script = "update_image_caption.php";
	
	var location = window.location.pathname;
	console.log(location);

	$('button.update-image').click(function(e) {
		imgRecordKey = $(this).data('img-id');
		imgCaption = $('#cap_'+imgRecordKey).val();
		updateCaption(imgRecordKey, imgCaption);
		e.preventDefault();
	});

var append = 0; 
	$('button.update-menu').click(function(e) {
		catRecordKey = $(this).data('cat-id');

		$('#curCatId').val(catRecordKey);

		var catName = $('#catName_'+catRecordKey).val();
		var catDesc = $('#catDesc_'+catRecordKey).val();
		var catActive = document.getElementById('catActive_'+catRecordKey).checked;
		if ($('#catType_'+catRecordKey).length != 0) {
			var catType = $('#catType_'+catRecordKey).val();
		} else {
			var catType = '0';
		}
		var catDelete = document.getElementById('catDelete_'+catRecordKey).checked;

		if (catDelete == true) {
			response = confirm('Are you sure you want to delete this category?');
			if(!response) {
				return false;
			}
		}

		postData = {recordKey: catRecordKey, action: 'update', category: catName, description: catDesc, chkactive: catActive, cattype: catType, chkdelete: catDelete};
		request = $.ajax({
			url: 'update_cat_settings.php',
			type: 'post',
			data: postData
		});

		request.done(function(response, textStatus, jqXHR) {
			if (response != '1') {
				alert('Error updating menu settings.');
				return false;
			} else {
				$('#catName-'+catRecordKey).html(catName);
				disablePopup();
				if(catDelete == true) {
					$('#cat-'+catRecordKey).hide();
				}
				if(append == 0){
	    			$('#warning-li').remove();
	    			$('.admin-left-sidebar').append('<li id="warning-li"><font color=red>Warning: Menu Export Required</font></li>');
	    			$("#logout-li").attr("onclick", "return warningAction(1,1)");
	    			append++;
	    		}
				return true;
			}
		});

		request.fail(function(jqXHR, textStatus, errorThrown) {
			console.error(
				'The following error occured: '+
				textStatus, errorThrown
			);
			return false;
		});


		e.preventDefault();
	});

	$('a.popup_cat_img').click(function(e) {
		disablePopup();
		openPopup('catImgPopup');
	});

	$('.close.imgPopup').click(function(e) {
		disablePopup();

		curPopupId = $('#cur-popup-id').val();
		if (curPopupId.length > 0) {
			openPopup('popup_'+curPopupId);
		}

		e.preventDefault();
	});

	$('a.saveCatImg').click(function(e){
		catRecordKey = $('#cur-popup-id').val();
		newFileName = $(this).data('img-filename');

		postData = {recordKey: catRecordKey, action: 'update-img', newImage: newFileName};
		request = $.ajax({
			url: 'update_cat_settings.php',
			data: postData,
			type: 'post'
		});

		request.done(function(response, textStatus, jqXHR) {
			if (response != '1') {
				alert('Error updating menu header image.');
				return false;
			} else {
				disablePopup();
				
				var newHeaderImgContent = "<div id='menu-wrapper'><div align=right><img src='/images/cat-headers/"+newFileName+"'></div></div>";
				$('#catHeaderImg_'+catRecordKey).html(newHeaderImgContent);

				openPopup('popup_'+catRecordKey);
				return true;
			}
		});

		request.fail(function(jqXHR, textStatus, errorThrown) {
			console.error(
				'The following error occured: '+
				textStatus, errorThrown
			);
			return false;
		});
	});
	
	/* event for close the popup */
	$("div.close").hover(
					function() {
						$('span.ecs_tooltip').show();
					},
					function () {
    					$('span.ecs_tooltip').hide();
  					}
				);
	
	$("div.close,div.close-popup").click(function() {
		disablePopup();  // function close pop up
	});
	
	$(document.body).on("keydown.popup", ".toPopDiv", function(ev) {
		// (window.console && console.debug && console.debug(ev));
		
		if (ev.keyCode == 27 && !ev.isDefaultPrevented()) {
			// test for open sub-dialogs
			if ($(":ui-dialog:visible").length != 0) {
				return;
			} else {
				ev.preventDefault();
				disablePopup();
			}
		}
	});
	
	/*$(this).keydown(function(event) {
		if (popupStatus != 1) {
			return;
		}
		if (event.which == 27) { // 27 is 'Ecs' in the keyboard
			(window.console && console.debug && console.debug($("ui-dialog:visible")));
			// if ($("ui-dialog:visible").length != 0) {
			// 	debugger;
			// } else {
			// 	debugger;
			// }
			if ($.ui && $(":ui-dialog:visible").length !== 0) {
				// assume a sub-dialog has been opened and should handle it's own ESC key.
				return;
			} else {
				disablePopup();  // function close pop up
			}
		}  	
	});*/
	
	$("div#backgroundPopup").click(function() {
		disablePopup();  // function close pop up
	});
	
	$('a.livebox').click(function() {
		alert('Hello World!');
	return false;
	});
	
	
	 /************** start: functions. **************/
	function loading() {
		$("div.loader").show();  
	}
	function closeloading() {
		$("div.loader").fadeOut('normal');  
	}
	
	
	
	var caption_updates = $(".update-image");
	
	$.each(caption_updates,function(k,v){
		var capId = $(this).attr("id");
		var capNum = capId.match(/\+d/);
	});

	$(document).on('click', '.popup', function(e) {
		idNum = $(this).data('popup-id');
		$('#cur-popup-id').val(idNum);

		openPopup("popup_"+idNum);

		// e.preventDefault();
		return false;
	});
	// $('.popup').click(function(e) {
	// 	idNum = $(this).data('popup-id');
	// 	$('#cur-popup-id').val(idNum);

	// 	openPopup("popup_"+idNum);

	// 	// e.preventDefault();
	// 	return false;
	// });

	function openPopup(popupId) {
		loading(); // loading
		setTimeout(function(){ // then show popup, deley in .5 second
			if(popupStatus == 0) { // if value is 0, show popup
				closeloading(); // fadeout loading
				$('#'+popupId).fadeIn(0500); // fadein popup div
				$("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
				$("#backgroundPopup").fadeIn(0001);
				popupStatus = 1; // and set value to 1
			}	
		}, 500); // .5 second
	}
	
	function disablePopup() {
			if(popupStatus == 1) {
				var popDivs = $(".toPopDiv");
				
				$.each(popDivs,function(){
					$(this).fadeOut("normal");
				});
				
				//$("#toPopup_"+idNum[0]).fadeOut("normal");  
				$("#backgroundPopup").fadeOut("normal");
				popupStatus = 0;  // and set value to 0
		}
	}	

	function updateCaption(imgRecordKey, newCaption) {
		var postData = {'recordkey': imgRecordKey, 'image_caption': newCaption};
		
		request = $.ajax({
			url: _image_update_script,
			type: 'post',
			data: postData
		});

		request.done(function(response, textStatus, jqXHR) {
			if (response != '1') {
				alert('Error updating caption.');
				return false;
			} else {
				disablePopup();
				return true;
			}
		});

		request.fail(function(jqXHR, textStatus, errorThrown) {
			console.error(
				'The following error occured: '+
				textStatus, errorThrown
			);
			return false;
		});
	}

	
	/************** end: functions. **************/
}); // jQuery End

function popupLoading() {
	$("div.loader").show();  
}
function popupCloseloading() {
	$("div.loader").fadeOut('normal');  
}
function openPopup(popupId) {
	popupId = 'popup_' + popupId;
	popupLoading(); // loading
	setTimeout(function(){ // then show popup, deley in .5 second
		if(popupStatus == 0) { // if value is 0, show popup
			popupCloseloading(); // fadeout loading
			$('#'+popupId).fadeIn(0500); // fadein popup div
			$("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
			$("#backgroundPopup").fadeIn(0001);
			popupStatus = 1; // and set value to 1
		}	
	}, 500); // .5 second
}

function disablePopup() {
	if(popupStatus == 1) {
		var popDivs = $(".toPopDiv");
		
		$.each(popDivs,function(){
			$(this).fadeOut("normal");
		});
		
		//$("#toPopup_"+idNum[0]).fadeOut("normal");  
		$("#backgroundPopup").fadeOut("normal");
		popupStatus = 0;  // and set value to 0
	}
}

function loadPopupIframe(inEl, inUrl) {
	
	$('#warning-li').remove();
	$("#logout-li").removeAttr("onclick");
	
	var popupId = $(inEl).data('popup-id');
	var iframe = $('#popup_'+popupId+' iframe');
	iframe.load(function() {
		var $contents = iframe.contents();
		$contents.scrollTop($contents.height());
		
		$('#popup_'+popupId+' div.close-popup').show();
	});
	iframe.attr('src', inUrl);
}
