jQuery(document).ready(function(){
	jQuery('#left-recipes-menu').find('.floating-menu').parent('li').floatingMenu({
		menuSelector:'div.floating-menu',
		beforeShowFn:function(caller){
			jQuery(caller).children('a').addClass('floating-menu-title');			
			if(jQuery.browser.msie && parseInt(jQuery.browser.version) < 8){
				var callerOffsets = jQuery(caller).position();
				var callerHeight = jQuery(caller).height();
				$(caller).find('div.floating-menu').css({'left':callerOffsets.left,'top':callerOffsets.top+callerHeight});
			}	
		},
		beforeHideFn:function(caller){
			jQuery(caller).children('a').removeClass('floating-menu-title');
		}
	});
	
	jQuery('#js-main-sort-block').show();

    jQuery('#category-sorting-block').show();
	
	jQuery('#rating-box').find('a#vote-star').mouseenter(
            function(){
                jQuery(this).parent().prev('.rating-stars-white').find('.rating-stars-yellow').addClass('hidden-rating');
            }
        ).mouseleave(
            function(){
                jQuery(this).parent().prev('.rating-stars-white').find('.rating-stars-yellow').removeClass('hidden-rating');
            }
        ).ratingAjax({
            'afterAjaxFn':function(caller, success, response){
			if(success && response){
				var parentBlock = jQuery(caller).parents('#rating-box').get(0);
				var evalScript = false;
				try{evalScript = jQuery.parseJSON(response).script;}catch(e){}
				if(evalScript){
					jQuery(parentBlock).find('.rating-box-preloader-block').hide();
					jQuery(parentBlock).find('.rating-block-details-data').show();
					jQuery(parentBlock).find('.vote-box').show();
					(function(){try{eval(evalScript)}catch(e){}}).bind(parentBlock)();
				}else
					jQuery(parentBlock).replaceWith(response);
			}else
				jQuery.ratingAjax.defaultAfterAjaxResponse(caller);
		}
	});
	
	jQuery("#post-photos a[rel^='gallery']").prettyPhoto({theme:'default'});
	
	jQuery("form[id=writeComment]").each(function(){
		jQuery(this).submit(function(){
			var canSubmit = true;
			jQuery(this).find('[rel=formData]').each(function(){
				var currentElement = jQuery(this);
				if(jQuery.trim(currentElement.val()).length == 0 || (currentElement.attr('name') == 'comment[authorEmail]' && !/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(currentElement.val()))){
					currentElement.addClass('incorrect-field')
					canSubmit = false;
				} else currentElement.removeClass('incorrect-field');
			});
			return canSubmit;
		}).find('input[id="comment[reset]"]').click(function(){
			jQuery(jQuery(this).parents('form').get(0)).find('[rel=formData]').val('').removeClass('incorrect-field')
		});
		jQuery(this).find('a[id="refresh-comment-security"]').click(function(){
			var imageSrc = jQuery(this).parents('tr').find('td:eq(0) img').attr('src');
            var newImageSrc = imageSrc.split('?');
			jQuery(this).parents('tr').find('td:eq(0) img').attr('src',newImageSrc[0]+'?'+Math.random());
            jQuery(this).parents('tr').find('td:eq(1) input').val('').focus();
		}).show();
	});	
	jQuery("a[id=upload-recipe-picture]").uploadPhotoAjax('#article-upload-block');			
});

Function.prototype.bind = function(arguments){ 
 if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
    var __method = this, args = jQuery.makeArray(arguments), object = args.shift();
    return function() {
      return __method.apply(object, args.concat( jQuery.makeArray(arguments)));
    }
}
