
$.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len   = elems.length;
      
  elems.bind('load',function(){
      if (--len <= 0){ callback.call(elems,this); }
  }).each(function(){
     // cached images don't fire load sometimes, so we reset src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
        this.src = src;
     }  
  }); 

  return this;
};

// now, the image functions
addSlideshowImage = function(imageid) {
	if (!imageid) {imageid = '';}
	$.ajax({
	    type: "POST",
	    url: cmshomepath+ "zones/slideshowadmin.cfm",
	    data: "action=form&zoneid="+ zoneid +"&id="+imageid,
	    success: function(msg){
	    	$('#'+dialogWindowName).html(msg);
	    	//$('#'+dialogWindowName).dialog('close');
	    	 $('#slideshowUploadImage').uploadify({
				'uploader': cmshomepath + 'swf/uploadify.swf',
    			'script': cmshomepath + 'zones/slideshowadmin.cfm?jsessionid='+$('#jsessionid').val(),
    			'folder': '/cmsimages', //this is irrelevant.  Set in request.cms.cmsfilefolder
    			'cancelImg': cmshomepath + 'images/cancel.png',
    			'scriptData': {'action':'slideshowuploadimage','convert':0,'zoneid':zoneid},
    			'fileDesc': 'Image files only (jpg,gif,png)',
    			'fileExt': '*.jpg;*.jpeg;*.gif;*.png;',
    			'buttonText': 'Select Image',
    			'auto': 'true',
    			onComplete: function(event, queueID, fileObj, response, data) {
    				if (response.indexOf('ERROR:') != -1) { 
    					alert(response);
    				}
    				else {
    					$('#slideshowImageResponse').remove();
    					$('#imagepath').val(response);
    					$('#slideshowUploadImageUploader').hide().after('<div id="slideshowImageResponse"><img src="'+cmsimagefolder + '/thumb/' + response + '" /> <a href="javascript:cancelSlideshowImageUpload();">remove this image</a></div>');
    				}
    			},
    			onCancel: function(event, queueID, fileObj, data) {
    				$('#slideshowUploadImage').uploadifyClearQueue();
    			}
			});
	    	skinIt('#'+dialogWindowName);
	    	return false;
	    },
	    error: function(XMLHttpRequest, textStatus, errorThrown) {
	    	alert(errorThrown+'--'+textStatus);
	    	return false;
	    }
	});
	
};
addSlideshowImageFormSubmit = function(){
	if ($('#title').val() == '') {alert('Please provide an image title.'); return false;};
	var postData = $('#addImageForm').serialize();
	$.ajax({
	    type: "POST",
	    url: cmshomepath + "zones/slideshowadmin.cfm",
	    data: "action=submit&zoneid="+ zoneid +"&"+postData,
	    success: function(msg){
	    	$('#'+dialogWindowName).html(msg);
	    },
	    error: function(XMLHttpRequest, textStatus, errorThrown) {
	    	alert(errorThrown+'--'+textStatus);
	    	return false;
	    }
	});
};
photoSlideshow = function() {
	$.ajax({
	    type: "POST",
	    url: cmshomepath + "zones/slideshowadmin.cfm",
	    data: "zoneid="+ zoneid,
	    success: function(msg){
	    	$('#'+dialogWindowName).html(msg);
	    	$('button').button();
	    	skinIt($('#'+dialogWindowName));
	    	
	    },
	    error: function(XMLHttpRequest, textStatus, errorThrown) {
	    	alert(errorThrown+'--'+textStatus);
	    	return false;
	    }
	});
}
delSlideshowItem = function(id) {
    var x = window.confirm('Are you sure you want to delete this image?');
    if (x) {	
    	$.ajax({
	    	type: "POST",
	    	url: cmshomepath + "zones/slideshowadmin.cfm",
	    	data: "action=delete&zoneid="+ zoneid +"&id="+id,
	    	success: function(msg){
	    		$('#'+dialogWindowName).html(msg);
	    		return false;
	    	},
	    	error: function(XMLHttpRequest, textStatus, errorThrown) {
	    		alert(errorThrown+'--'+textStatus);
	    		return false;
	    	}
	    });
    }
    else return false;
};
cancelSlideshowImageUpload = function() {
	$.ajax({
	   type: "POST",
	   url: cmshomepath + "zones/slideshowadmin.cfm",
	   data: "action=deleteimage&filename="+$('#imagepath').val(),
	   success: function(msg){
	   		//$('#newsCurrentFile').show();
	   		//$('#pdfConvertLine').show();
	   		$('#imagepath').val('');
	   		$('#slideshowUploadImageUploader').show();
	   		$('#slideshowImageResponse').remove();
	   }
	});
};
slideshowzonesettings = function() {
	$.ajax({
	    type: "POST",
	    url: cmshomepath + "zones/slideshowadmin.cfm",
	    data: "action=slideshowzonesettings&zoneid="+ zoneid,
	    success: function(msg){
	    	$('#'+dialogWindowName).html(msg);
	    	skinIt('#'+dialogWindowName);
	    	return false;
	    },
	    error: function(XMLHttpRequest, textStatus, errorThrown) {
	    	alert(errorThrown+'--'+textStatus);
	    	return false;
	    }
	});
};
reorderList = function() {
	$.ajax({
	    type: "POST",
	    url: cmshomepath + "zones/slideshowadmin.cfm",
	    data: "action=reorderList&zoneid="+ zoneid,
	    success: function(msg){
	    	$('#'+dialogWindowName).html(msg);
	    	skinIt('#'+dialogWindowName);
	    	$( "#reorder"+zoneid).sortable({
	    		update: function(event, ui) {
					$('#return'+zoneid).html('');
					var newOrder = $(this).sortable('toArray').toString();
					$.ajax({
					   type: "POST",
					   url: cmshomepath + "zones/slideshowadmin.cfm",
					   data: "action=reordersubmit&neworder="+newOrder+"&zoneid="+ zoneid,
					   success: function(msg){
					     $('#return'+zoneid).html(msg);
					   }
					 });
				}
	    	});
	    	//alert('test');
	    	return false;
	    },
	    error: function(XMLHttpRequest, textStatus, errorThrown) {
	    	alert(errorThrown+'--'+textStatus);
	    	return false;
	    }
	});
};
slideshowzonesettingssubmit = function(){
	if($('#slideshowwidth').val() == '') {
		alert('Please provide a width for the slideshow.');
		return false;
	}
	var postData = $('#settingsForm').serialize();
	$.ajax({
	    type: "POST",
	    url: cmshomepath + "zones/slideshowadmin.cfm",
	    data: "action=slideshowzonesettingssubmit&zoneid="+ zoneid +"&"+postData,
	    success: function(msg){
	    	//alert(msg);
	    	photoSlideshow();
	    	skinIt('#'+dialogWindowName);
	    	return false;
	    },
	    error: function(XMLHttpRequest, textStatus, errorThrown) {
	    	alert(errorThrown+'--'+textStatus);
	    	return false;
	    }
	});
	return false;
};
//var cycleranonce = 0;
loadSlideshow = function(divid,showNav,navColor,duration,transition) {
	var thisshow = $('#slideshowzone'+divid);
	var navbar = 'none';
    //$('#slideshowzone'+divid+' img.slidefirst').imagesLoaded(function() {  //waits for the first image to load
    
    	//if (!cycleranonce) {
    		//cycleranonce = 1;
    		navbar = 'cycleNavBar'+divid;
    		if (showNav == 1 && !$('#'+navbar).length) {
				$(thisshow).before('<div id="'+navbar+'" class="slideshowNavBar">');
			}
    		$(thisshow).cycle({
    		    fx: transition,
    		    speed: 1000,
    		    timeout: duration*1000,
    		    pager: '#'+navbar
    		    });           
    	//} 
   // });
};  
(function($){var a=$.ui.mouse.prototype._mouseMove;$.ui.mouse.prototype._mouseMove=function(b){if($.browser.msie&&document.documentMode>=9){b.button=1};a.apply(this,[b]);}}(jQuery));
