function ShareYM()
{
	window.location=encodeURI('ymsgr:im?msg='+(window.location.href));
}

function NewsletterRegister(id){
	$.ajax({
	  type: 'POST',
	  url: "/newsletter-suscribe",
	  data: $("#newsletter_suscribe_frm").serialize(),
	  success: function(m){
		  alert(m);
		  $("#newsletter_suscribe_frm :text").val("");
		  }
	});	
	return false;
}
function SearchUnder(id){
	if( $('#keywords_'+id).val()=='Cauta reteta' ){
		$('#keywords_'+id).val("");
	}
}

function carousel_initCallback(carousel)
{
	$("#"+carousel.options.id+' .jcarousel-control div').mouseover(function() {															  	
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr( "id" )),false);															  	
		return false;
	});
	
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
	
	$('#'+carousel.options.id+' .jcarousel-control div').hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});	
	
};

function carousel_itemVisibleInCallback(carousel,list,idx)
{
	$("#"+carousel.options.id+' .jcarousel-control').children(".white_border").removeClass("white_border");
	$("#"+carousel.options.id+' .jcarousel-control').children(".selected").removeClass("selected");
	$("#"+carousel.options.id+' .jcarousel-control').children("#"+idx).addClass( "selected" );
	$("#"+carousel.options.id+' .jcarousel-control').children("#"+(idx-1)).addClass( "white_border" );
}

/* --------------------------------------------------------- */
 
function Print(url){
	window.open (url,"print","scrollbars=yes,menubar=0,resizable=1,width=500"); 
}

function ShowFilters(){
	TogClass(".more","more plus","more minus");
	Tog(".search_filters");
}
function AddToFavorites(obj,user_id,recipe_id){
	
	$.ajax({
	  type: "GET",
	  url: "/recipes/add_to_favorites/"+user_id+"/"+recipe_id,
	  success: function(response){
			var json = jQuery.parseJSON(response);
			
			if( json.redirect ){
				document.location.href = json.redirect;
			}else{
				$(obj).removeClass(json.removeclass).addClass(json.addclass);
				if( json.error ){
					alert(json.error);				 
				}
			}
			 
		}
	});

}


/* ------------------------------------------ */
function Tog(what){
	//$(what).toggle("slow");
	//$(what).slideToggle('slow', function() {
		// Animation complete.
	//});
	$(what).animate({height: 'toggle', opacity: 'toggle'}, "slow");
}
function TogClass(what,class1,class2){
	$(what).toggleClass(class1).toggleClass(class2);
}

function FormFocus(obj) {
	if(obj.value==obj.defaultValue) obj.value='';
}

function FormBlur(obj) {
	if(obj.value=='') obj.value=obj.defaultValue;
}

function FormSubmit(form){
	  document.getElementById(form).submit(); return false;
}
function nl2br (str, is_xhtml) {
    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br>';
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
function br2nl(str) {
	var out = str;
	
	out = out.replace(/<br>/mg, "\n");
	out = out.replace(/<br\/?>/mg, "\n");
	out = out.replace(/<br\s*\/?>/mg, "\n");
	out = out.replace(/<BR>/mg, "\n");
	out = out.replace(/<BR\/?>/mg, "\n");
	out = out.replace(/<BR\s*\/?>/mg, "\n");

	out = out.replace(/\n\n/mg, "\n");	
	
	return out;
}
function value(v){
	if(v) 
		return v;
	return;
}
function fieldobj(f){
	eval("var x = document."+f+".value;");
	return x;		
}

/* ------------------------ autocomplete */
function myautocomplete(field,urlget){	
	$("#"+field).autocomplete(urlget, {
		width: 320,
		max: 50,
		selectFirst:true,
		highlight: false,
		scroll: true,
		delay:0,
		scrollHeight: 300,
	
		multiple: false,
		dataType: "json",
		extraParams: {},
		parse: function(data) {
			return $.map(data, function(row) {
											
				return {
					data: row,
					value: row.name,
					result: row.name
				}
			});
		},
		formatItem: function(item) {
			return item.name;
		}
	}).result(function(e, item) {

	});
	
	
}			  
/* autocomplete ------------------------ */
								  
/* --------------------- comments */
Comment = {
		settings: {
			item_id:0,
			item_type:1,
			comment_parent:0,
			page:1
		},									
		init: function(data) {
			Comment.settings.item_id = data.item_id;
			Comment.settings.item_type = data.item_type;
			Comment.settings.comment_parent = data.comment_parent;
			Comment.settings.page = data.page;
		},
		get: function(page) {
		
			var page = (page==undefined)?1:page;
			$("#comments_list").load("/comments/getcomments/"+Comment.settings.item_id+"/"+Comment.settings.item_type+"/"+page);
			
		},
		add: function() {

			  var datastring = $("#frmComment").serialize();
			  
			  $.ajax({
				  type: "POST",
				  url: "/comments/addcomment",
				  data: datastring,
				  success: function(response){

						Comment.parseResponse(response);

					 }
				});
			
		},
		parseResponse: function(response) {
		
			var resp = jQuery.parseJSON(response);
		
			if(resp.error){
				$('#comment_error').text(resp.error);
			}else{
				$("#ratevalue").val(0);
				$("#comment_body").val("");
				Comment.get(1);				
			}
			
		}
	}
/* comments --------------------- */


/* ------------------------ RecipePhoto */
RecipePhoto = {
		settings: {
			recipe_id:0
		},									
		init: function(data) {
			$("#photo_step_3").css({display:"none"});
		},
		getcrop: function() {

			$("#photo_crop").append("<iframe style=\"background:#F7F4ED\" name=\"photoframe\" id=\"photoframe\" scrolling=\"no\" frameborder=\"0\" src=\"/recipes/photo_crop/"+RecipePhoto.settings.recipe_id+"\" width=\"429\" height=\"600\"></iframe>");
			
		},
		startMediaUpload: function() {
			
			var msgPic = "";
			if(msgPic == ""){
				$('#photo_error').html("<img src=\"/assets/images/loading/ajax-loader2.gif\" />");
				return true;
			}else{
				alert(msgPic);		
				return false
			}		
			
		},
		completeMediaUpload: function(response) {
			
			RecipePhoto.parseResponse(response);
			$("#makecrop").css({display:"block"});
		
		},
		parseResponse: function(response) {
			var resp = jQuery.parseJSON(response);
		
			var err = '';
			if(resp.error){
				err += resp.error;
			}else{
				hiframe(600);
				window.location.reload( true );		
			}
				
			if(resp.upload)
				err += resp.upload;
		
			$('#photo_error').text(err);		
		}
	}
/* RecipePhoto ------------------------ */


/* ------------------------ RecipeClick */
RecipeClick = {
	init: function(data) {
		$(document).ready(function() {
		
			$("#recipe_frm *").each(function() {
				if( $(this).hasClass('hover') ){
					$(this).bind('click', function(obj) {
					  RecipeClick.hover(this);
					});
			   }
			});
			
		});
	},
	hover: function(obj) {
		RecipeClick.remove();
		
		var parent = $(obj).parent();
		
		if( parent.get(0).tagName.toLowerCase() == "fieldset" ){
			parent.addClass('recipe_hover');
		}else{
			RecipeClick.hover(parent);
		}		
	},
	remove: function(obj) {
		if( $("#recipe_frm fieldset").hasClass('recipe_hover') ){
			$("#recipe_frm fieldset").removeClass('recipe_hover');
		}
	}
}
/* RecipeClick ------------------------ */















function Search(){
	var qvalue = document.frmSearch.q.value;
	if (qvalue=='')
	{
		alert('Va rugam introduceti cuvantul cautat');
		return false
	}
	return true;
}




/* sort table */
function sortTable(tableId, nosort, ajaxurl){
	$(document).ready(function() { 	
	//$(function(){
	  $("#"+tableId+"").tableDnD({ 
			cursor: "move", 
			items: "tr:not(."+nosort+")", 
			onDrop: function(){
				//updatesortable(tableId, ajaxurl);
				
					var datastring = $("#"+tableId+"").tableDnDSerialize();
					
					$.ajax({
					  type: "POST",
					  url: ajaxurl,
					  data: datastring,
					  success: function(msg){
							 //alert( "Data Saved: " );
						   }
					});
				
			} 
		  });
	});

}

/* sort list */
function sortLi(liid, ajaxurl, reloadid, reloadurl){
	if(ajaxurl==undefined) ajaxurl=false;
	if(reloadid==undefined) reloadid=false;
	if(reloadurl==undefined) reloadurl=false;
	
	$(document).ready(function() { 
	  $("#"+liid).sortable({ 
		cursor: 'move', 
		opacity: 0.6, 
		placeholder: 'ui-state-highlight',
		update : function () { 
		  
		  if(ajaxurl)
		  {
			  
			  var datastring = $("#"+liid+"").sortable('serialize');
			  
			  $.ajax({
				  type: "POST",
				  url: ajaxurl,
				  data: datastring,
				  success: function(msg){
						 //alert( "Data Saved: " );
						  if(reloadid){
						 	$("#"+reloadid).load(reloadurl);
						  }
					   }
				});
			  
		  }
		  
		  
		} 
	  });
	}); 
}



function captchaReload(id){
	var randomnumber=Math.floor(Math.random()*101);
	document.getElementById(id).src = "/captcha.jpg?"+randomnumber;
	document.frmComment.captcha.value="";
}

function switchtools(){
	var classtools = new Array();
	$(document).ready(function() {
		$(".tools").children("a").each(function(k) {
			classtools[k] = this.className;
			this.onmouseover= function(){ 
				this.className += "_on";
			}	
			this.onmouseout= function(){ 
				this.className = classtools[k];
			}	
		});
	});	
}

