$(function() {
	$('#gallery a').lightBox({
    imageLoading:   '/images/lightbox-ico-loading.gif',
    imageBtnClose:  '/images/lightbox-btn-close.gif',
    imageBtnPrev:   '/images/lightbox-btn-prev.gif',
    imageBtnNext:   '/images/lightbox-btn-next.gif',
    imageBlank:     '/images/lightbox-blank.gif'
  });
});

(function($){
$(document).ready(function(){




$('#cancel-upload-btn').click(function(){
	// reset file upload queue
	$('#upload-file').fileUploadClearQueue();
	//close dialog
	$("#add-image-dialog").dialog("close");
});
	

/* slider control region */	
	// creates slider control
 	$("#content-slider").slider({
    	animate: true,
    	handle: ".content-slider-handle",
    	change: handleSliderChange,
    	slide: handleSliderSlide
  	});
 	
	function handleSliderChange(e, ui)
	{
  		var maxScroll = $("#content-scroll").attr("scrollWidth") - $("#content-scroll").width();
  		$("#content-scroll").animate({scrollLeft: ui.value * (maxScroll / 100) }, 1000);
	}

	function handleSliderSlide(e, ui)
	{
  		var maxScroll = $("#content-scroll").attr("scrollWidth") - $("#content-scroll").width();
  		$("#content-scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
	}
/* end of slider control region */	

	// hides flash message from the screen after the seted time passed
	
	$(this).oneTime(8000, function() {
    	$('.notice').fadeOut(2000);
  	});
	
	//define config object for add image dialog
	var dialogOptsAdd = {
		modal: 			true,
		width: 			380,
		height: 		240,
		autoOpen: 		false,
		dialogClass: 	"ui-dialog",
    	overlay: 		{ opacity: 0.5, background: "black" },
    	resizable:		false,
    	close: function() {
			// select first element in the select list
			$('#select-image option:first').attr('selected', 'yes');
			// set empty image for preview
			$('#image-preview').attr('src', '');
			}
		};
	
	//create the add image dialog
	$("#add-image-dialog").dialog(dialogOptsAdd);
	
	// open add image dialog
	$('#add-image-dlg-open').click(function(){
 		$('#add-image-dialog').dialog('open');
 		return false;
	});
	
	$('#content-holder div').bind('click', function(){
		var thisTarget = $(this).attr("id");
		changeEnterpriseImage(thisTarget);
	});

	/**
	* removes selected image from content holder
	*/	
	$('#remove-image').bind('click', function(){
		var selected_image_val   = $('#enterpriseimage').attr('value');
		
		$('#prod-img-'+selected_image_val).remove();
		
		$("#content-holder .Image").each(function(index) {
			var idx = index + 1;
			$(this).attr('id', 'prod-img-' + idx);
		});
		
		//rebind image click event
		$('#content-holder div').bind('click', function(){
			var thisTarget = $(this).attr("id");
			changeEnterpriseImage(thisTarget);
		}); 
		
		$("#enterpriseimage").attr('value', "");
	});

	/**
	* fires when enterprise image was clicked (selected) in enterprise image holder 
	*/
	function changeEnterpriseImage(thisTarget){
		$("#content-holder .Image").each(function() {
			var thisAttr 	= $(this).attr("id");
			var thisElem    = $("#"+thisAttr);
			thisElem.attr('style', 'border: 1px solid #DDDDDD');
		});
	    
	    // retrieve selected image id
	    var targetStr   = String(thisTarget);
	    var imageId 	= targetStr.replace('prod-img-','');

		var elem       	= $("#"+thisTarget);
    	elem.attr('style', 'border: 1px solid #4F81BD');
    	$("#enterpriseimage").attr('value', imageId);
 	}
});
})(jQuery);	