// JavaScript Document 
$(function(){ 
	
	// innerfade - http://medienfreunde.com/lab/innerfade/   
	$('#fader').innerfade({ speed: 700, timeout: 4000, type: 'sequence', containerheight: '383px' });

	// prettyPhoto - http://www.no-margin-for-errors.com/projects/prettyPhoto-jquery-lightbox-clone/   
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	// preload voting images
	jQuery.preLoadImages("/images/ratings2.jpg", "/images/ratings1.jpg");
	
	// form validate - http://docs.jquery.com/Plugins/Validation
	$("#newsletterForm").validate();
	
	// contact form slider
	$('#forgot').click(function () { 
      $('#forgetful ul.form').slideDown(800); 
      return false;
    });
	
	// voting widgets
	$('.ratings').hover(function(){
		$(this).addClass('reveal');
	}, function(){
		$(this).removeClass('reveal');
	});	
	
	$('input[type=image]').hover(function(){
	    $(this)
	    	.parents('form')
	    	.nextAll()
	    	.find('input[type=image]')
	    	.attr('src', '/images/ratings2.jpg');
	},function(){
	    $(this)
	    	.parents('div.ratings')
	    	.find('input[type=image]')
	    	.attr('src', '/images/ratings1.jpg');
	});
	
	// process vote
	$('form.rat').submit(function(){
		var container = $(this).parents('div.ratings');
		$.post($(this).attr('action'), $(this).serialize(), function(html){
			container.replaceWith(html);
		});
		return false;
	});
	
	$('input.swfupload').each(function(){
		var input = $(this)
			.wrap('<span class="swfupload-control"></span>')
			.replaceWith('<input class="swfupload" type="text" file_post_name="' + $(this).attr('name') + '" />')
			.data('file_post_name', $(this).attr('name'));
	});
	
	/* uploader for profile image */
	$('.profile-image .swfupload-control')
		.each(function(){
			var form = $(this).parents('form')[0];
			var upload_name = $('input', this).attr('file_post_name');
			$(this).swfupload({
				upload_url: $(form).attr('action'),
				file_size_limit : "10240",
				file_types : "*.jpg;*.gif;*.png",
				file_types_description : "Image Files",
				file_upload_limit : "0",
				file_post_name: upload_name,
				flash_url : "/flash/swfupload.swf",
				button_image_url : '/images/btn-browse.jpg',
				button_width : 73,
				button_height : 22,
				button_placeholder : $('input[type=submit]', form)[0],
				post_params: {
					'sf_method' : 'put', 
					'swfupload_session_id': $('input[name*=PHPSESSID]').val(),
					'profile[_csrf_token]': $('input[name="profile[_csrf_token]"]').val()
				}
			});
		})
	    .bind('fileQueued', function(event, file){
	    	$('input', event.target).val(file.name);
	    	$.showLoader('Uploading profile image');
	        $(this).swfupload('startUpload');
	    })
	    .bind('uploadSuccess', function(event, file, image){
	    	$('input', event.target).val('');
	        $('img.avatar').attr('src','/uploads/users/' + image);
	        $.hideLoader();
	    });	
	
	
	/* uploader for entry images */
	$('.edit-image input.swfupload')
		.each(function(){
			jQuery.applySwfuploader(this);
		});		
	
	/* show/hide the update messages in the entry edit page image uploaders */
	$('.edit-image').hover(function(){
		$(this).find('.updateMessage').slideDown('fast');
	}, function(){
		$(this).find('.updateMessage').slideUp('fast');
	});
	
	/* show/hide the commercial pattern details in entry editor */
	$('form #entry_pattern').change(function(){
		if($(this).val().indexOf('commercial') == -1)
		{
		  $(this)
		  	.parents('form')
		  	.find('#entry_pattern_name')
		  	.val('')
		  	.parent()
		  	.slideUp('fast');
		}
		else
		{
		  $(this)
		  	.parents('form')
		  	.find('#entry_pattern_name')
		  	.parent()
		  	.slideDown('fast');
		}
	});
	$('#entry_pattern').change();
	
	

}); 

var upload_count = 0;

jQuery.applySwfuploader = function($this){
	var parentContainer = $($this).parents('div.edit-image');
	var form = $($this).parents('form')[0];
	var upload_name = $('input', $this).attr('file_post_name');
	var is_main_image = parentContainer.attr('id') == 'edit-image-1' ? true : false;
	var image = $('img', parentContainer).attr('src');
	parentContainer.addClass('swfupload-loaded');
	
	$($this).swfupload({
		upload_url: $(form).attr('action'),
		file_size_limit : "10240",
		file_types : "*.jpg;*.gif;*.png",
		file_types_description : "Image Files",
		file_upload_limit : "0",
		file_post_name: 'entry_image[image]',
		flash_url : "/flash/swfupload.swf",
		button_image_url : image,
		button_width : is_main_image ? 400 : 118,
		button_height : is_main_image ? 600 : 177,
		button_placeholder : $('img', form)[0],
		button_window_mode: 'TRANSPARENT',
		post_params: {
			'sf_method' : 'put', 
			'swfupload_session_id': $('input[name*=PHPSESSID]', form).val(),
			'ordering': $('input[name="entry_image[ordering]"]', form).val(),
			'entry_id': $('input[name="entry_image[entry_id]"]', form).val(),
			'entry_image[ordering]': $('input[name="entry_image[ordering]"]', form).val(),
			'entry_image[_csrf_token]': $('input[name="entry_image[_csrf_token]"]', form).val()
		}
	});
	$($this)
		.bind('fileQueued', function(event, file){
	    	$('input', event.target).val(file.name);
	    	upload_count++;
	    	$.showLoader('Uploading ' + upload_count + ' image/s');
	        $(this).swfupload('startUpload');
	    })
	    .bind('uploadSuccess', function(event, file, html){
	    	upload_count--;
	    	if(upload_count > 0)
	    	{
	    		$.showLoader('Uploading ' + upload_count + ' image/s');
	    	}
	    	else
	    	{	
	    		$.hideLoader();
	    	}	    	
	    	var swfitem = $(event.target)
		    	.parents('div.edit-image')
		    	.html(html)
		    	.find('input.swfupload');
	    	jQuery.applySwfuploader(swfitem);
	    });	
}

jQuery.showLoader = function(msg){
	$('#loading').show().find('.message').text(msg);
}

jQuery.hideLoader = function(){
	$('#loading').hide().find('.message').text('');
};

/* image preloader */
(function($) {
	  var cache = [];
	  // Arguments are image paths relative to the current page.
	  $.preLoadImages = function() {
	    var args_len = arguments.length;
	    for (var i = args_len; i--;) {
	      var cacheImage = document.createElement('img');
	      cacheImage.src = arguments[i];
	      cache.push(cacheImage);
	    }
	  }
	})(jQuery);
 