var DailySip = function()
{
	var ID=null;
	var PhotoChanged = false;

    return {
    	
    	Search: function(searchText)
    	{
   			if( $.trim(searchText) == "" )
   			{
				alert('Please enter search text');
				return 0;
   			}
   			else
   			{
   				var data = '/dailysip/search/' + searchText;

   				window.location.href = data;

/*   				$.ajax(
				{
					type: "POST",
					url: "/dailysip/search/search",
					data: "action=search"+ data,
					success: function (r)
					{
					//	$(document).text("<center>Hello</center>");
					}
				});  */
   			}	
    	},

		SaveNote: function(user_id, daily_sip_id)
		{
   			if( $.trim($('#note').val()) == "" )
   			{
				$('#note').parent().addClass('error');
				alert('Please enter some note');
				$('#note').focus();
				return 0;
   			}
   			else
   			{
   				var data = "&user_id="+ user_id + "&note="+ Base64.encodeAndEscape($('#note').val());

   				$.ajax(
				{
					type: "POST",
					url: window.location.pathname,
					data: "action=save_note&id="+ daily_sip_id + data,
					beforeSend: Util.StartWaiting,
					success: function (r)
					{
						Util.StopWaiting();

						r = eval(r);

						if( r.code == 0 )
						{
	    					$('#add-note-form').hide();
 		   					$('#note').val('');
                            $('div.leaveNote').after(r.res);
	    					alert("Your note have been successfully saved");
						}
						else
						{
							alert(r.msg);
						}
					}
				});
   			}
		},
        

	DeleteComment: function(id)
	{
	   if( confirm("Are you sure to remove this comment?") )
		{
	$.ajax({
		type: "POST",
		url: window.location.pathname,
		beforeSend: Util.StartWaiting,
		data: "action=delete_comment&comment_id=" + id,
		success: function (r)
		{
			r = eval(r);

			Util.StopWaiting();

			if( r.code == 0 )
			{
				$("#comment"+id).remove();
			}
            alert(r.msg);
		}
	});
    }
	},


	SaveComment: function(id, a)
	{
	var note = $(a).parent().parent().prev().find('textarea:first');

	if( note.val() == '' )
	{		alert('Please enter some comment');
		return false;	}
        
	$.ajax({
		type: "POST",
		url: window.location.pathname,
		beforeSend: Util.StartWaiting,
		data: "action=save_comment&comment_id=" + id + "&comment="+ Base64.encodeAndEscape(note.val()),
		success: function (r)
		{
			r = eval(r);

			Util.StopWaiting();
            
			if( r.code == 0 )
			{
				$(a).parent().parent().parent().parent().find('div.comment > p:first').html(note.val().replace(/\n/g, '<br>'));
				$(a).parent().parent().parent().hide();
			}
		}
	});
	},
    
SaveDailysipNote: function(daily_sip_id,a)
		{
			
			if( $.trim($('#note'+daily_sip_id).val()) == "" )
   			{
				$('#note').parent().addClass('error');
				alert('Please enter some note');
				$('#note').focus();
				return 0;
   			}
   			else
   			{
   				var data = "&note="+ Base64.encodeAndEscape($('#note'+daily_sip_id).val());
          		$.ajax(
				{
					type: "POST",
					url: window.location.pathname,
					data: "action=save_note&id="+ daily_sip_id + data,
					beforeSend: Util.StartWaiting,
					success: function (r)
					{
						Util.StopWaiting();

						r = eval(r);
                        if( r.code == 0 )
						{
							$(a).parent().parent().parent().prev().find('div.note:first').empty().html(tospaces($('#note'+daily_sip_id).val()));
					        $(a).parent().parent().parent().hide();
					        Util.StopWaiting();
	    					alert("Your note have been successfully saved");
						}
						else
						{
							alert(r.msg);
						}
					}
				});
   			}
		},
     
		RemoveNote: function(id, a)
		{
			if( confirm("Are you sure to delete this sip?") )
			 {
				$.ajax(
				{
					type: "POST",
					url: window.location.pathname,
					data: "action=delete_sip&id="+ id,
					
					beforeSend: Util.StartWaiting,
					success: function (r)
					{
						
						Util.StopWaiting();

						r = eval(r);
                       if( r.code == 0 )
						{
	     					 $(a).parent().parent().next().remove();
						     $(a).parent().parent().remove();
							 Util.StopWaiting();
						}
						else
						{
								alert(r.msg);
						}
					}
				});
			}
		},
		SaveChanges: function(type)
		{ 
		//below changes made for implementing tinymce dailysip
		var body="";
		body = document.getElementById('dailysipbody');
						
		var content = tinyMCE.activeEditor.getContent();
			body.value = content;
		//changes end for implementing tinymce
			var data = "";

			if( $('#'+ type +'-form #title').val() == "" )
			{
				$('#'+ type +'-form #title').parent().addClass('error');
				$('#'+ type +'-form #title').focus();
				window.scrollTo(0, 0);
				alert('Please enter title');
				return false;
			}
			else
			{
				$('#'+ type +'-form #title').parent().removeClass('error');
				data += "&title="+ Base64.encodeAndEscape($('#'+ type +'-form #title').val());
			}
			
			
				data += "&Tags="+ Base64.encodeAndEscape($('#'+ type +'-form #Tags').val());
			
			var publishDate = $('#publishDate'+ ( type == 'edit' ? '3' : '' ));

			if( publishDate.val() == "" )
			{
				publishDate.parent().addClass('error');
				publishDate.focus();
				alert('Please choose publish date');
				return false;
			}
			else
			{
				publishDate.parent().removeClass('error');
				data += "&publishDate="+ publishDate.val();
			}

   			if( $('#'+ type +'-form #urlAlias').val() == "" )
			{
				$('#'+ type +'-form #urlAlias').parent().addClass('error');
				$('#'+ type +'-form #urlAlias').focus();
				alert('Please choose url alias');
				return false;
			}
			else
			{
				var response = $.ajax(
				{
					type: "POST",
					url: window.location.pathname,
					async:false,
					beforeSend: Util.StartWaiting,
					data: "action=check_alias&alias="+ $('#'+ type +'-form #urlAlias').val()
				}).responseText;

				Util.StopWaiting();

				if( $('#'+ type +'-form #urlAlias').val() != $('#'+ type +'-form #urlAlias1').val() && response == 1 )
				{
					$('#'+ type +'-form #urlAlias').parent().addClass('error');
					$('#'+ type +'-form #urlAlias').focus();
					alert('This url alias already exists, please choose another one');
					return false;
				}
				else
				{
					$('#'+ type +'-form #urlAlias').parent().removeClass('error');
					data += "&urlAlias="+ $('#'+ type +'-form #urlAlias').val();
				}
			}
			data += "&browser_title="+ Base64.encodeAndEscape($('#'+ type +'-form #browser_title').val());
			data += "&metadescription="+ Base64.encodeAndEscape($('#'+ type +'-form #metadescription').val());
			data += "&primary_category="+ $('#'+ type +'-form #primary_category_term').val();
			data += "&subTitle="+ $('#'+ type +'-form #sub_title').val();
			if( $('#'+ type +'-form #category_term').val()  == "" || $('#'+ type +'-form #category_term').val()  == null)
				{
					$('#'+ type +'-form #category_term').parent().addClass('error');
					$('#'+ type +'-form #category_term').focus();
					alert('Please select a Category.');
					return false;
				}
				else
				{
					$('#'+ type +'-form #category_term').parent().removeClass('error');
					data += "&category_term="+ $('#'+ type +'-form #category_term').val();
				}
			data += "&snippet="+Base64.encodeAndEscape($('#'+ type +'-form #snippettext').val());

			//below changes made for implementing tinymce dailysip
			if(type =='add'){
				if( $('#'+ type +'-form #dailysipbody').val() == "" )
				{
				$('#'+ type +'-form #dailysipbody').parent().addClass('error');
				$('#'+ type +'-form #dailysipbody').focus();
				alert('Please enter text of email');
				return false;
				}
				else
				{
				$('#'+ type +'-form #dailysipbody').parent().removeClass('error');
				data += "&dailysipbody="+Base64.encodeAndEscape($('#'+ type +'-form #dailysipbody').val());
				}
			}
			
						
			//changes end for implementing tinymce in dailysip
			data += "&picture="+ $('#'+ type +'-form #picture'+ ( type == 'edit' ? '1' : '' )).attr('src').replace(/(.*?)\/([^/]+)$/,'$2');

			if( ID ==null || ID=="")
			{
				data += "&add=1";
			}
			else
			{
				data +="&id="+ID;
			}

			if( PhotoChanged )
			{
				data += "&picture_changed_ind=1";
				PhotoChanged = false;
			}

			//alert(data); return false;

			$.ajax(
			{
				type: "POST",
				url: window.location.pathname,
				data: "action=save_changes&id="+ ID + data,
				beforeSend: Util.StartWaiting,
				success: function (r)
				{
					Util.StopWaiting();

					r = eval(r);

					if( r.code == 0 )
					{
    					$('#search #title').val($('#title').val());
						DailySips.Search(1, 10);
						alert("Daily sip changes have been successfully stored");
						toggle_tab(2, $('#search-tab'));
					}
					else
					{
						alert(r.msg);
					}
				}
			});
		},

		ChangePhoto: function(iframe)
		{
			image = iframe.contentWindow.document.dailysipbody.innerHTML;

			if( image != "" )
			{
				$('#picture').attr('src', '/sites/default/modules/bottlenotes/images/dailysip/'+ image);
				PhotoChanged = true;
			}
		},

		Remove: function(id, a)
		{
			if( confirm("Are you sure to delete this sip?") )
			{
				$.ajax(
				{
					type: "POST",
					url: window.location.pathname,
					data: "action=remove_sip&id="+ id,
					beforeSend: Util.StartWaiting,
					success: function (r)
					{
						Util.StopWaiting();

						r = eval(r);

						if( r.code == 0 )
						{
	     					$(a).parent().parent().remove();
						}
						else
						{
							alert(r.msg);
						}
					}
				});
			}
		},
                PreviewDailysip: function(type)
                {
                 
                    var body="";
                 
                    body = document.getElementById('dailysipbody');

                    var content = tinyMCE.activeEditor.getContent();
                    body.value = content;

                   var data = "";
                    if( $('#'+ type +'-form #title').val() == "" )
			{
			
                                $('#'+ type +'-form #title').parent().addClass('error');
				$('#'+ type +'-form #title').focus();
				window.scrollTo(0, 0);
				alert('Please enter title');
				return false;
			}
			else
			{
                             
				$('#'+ type +'-form #title').parent().removeClass('error');
				data += "&title="+ Base64.encodeAndEscape($('#'+ type +'-form #title').val());
			}
                            
                       
				data += "&Tags="+ Base64.encodeAndEscape($('#'+ type +'-form #Tags').val());

			var publishDate = $('#publishDate'+ ( type == 'edit' ? '3' : '' ));

			if( publishDate.val() == "" )
			{
				publishDate.parent().addClass('error');
				publishDate.focus();
				alert('Please choose publish date');
				return false;
			}
			else
			{
				publishDate.parent().removeClass('error');
				data += "&publishDate="+ publishDate.val();
			}

   			if( $('#'+ type +'-form #urlAlias').val() == "" )
			{
				$('#'+ type +'-form #urlAlias').parent().addClass('error');
				$('#'+ type +'-form #urlAlias').focus();
				alert('Please choose url alias');
				return false;

                        }
                        else{
				$('#'+ type +'-form #urlAlias').parent().removeClass('error');
				data += "&urlAlias="+ $('#'+ type +'-form #urlAlias').val();
			}

                        data += "&picture="+ $('#'+ type +'-form #picture'+ ( type == 'edit' ? '1' : '' )).attr('src').replace(/(.*?)\/([^/]+)$/,'$2');
                        if( PhotoChanged )
			{
				data += "&picture_changed_ind=1";
				PhotoChanged = false;
			}
		                     
			//data += "&category="+ $('#'+ type +'-form #category').val();
			data += "&subTitle="+ $('#'+ type +'-form #sub_title').val();
                        if(type =='add'){
				if( $('#'+ type +'-form #dailysipbody').val() == "" )
				{
				$('#'+ type +'-form #dailysipbody').parent().addClass('error');
				$('#'+ type +'-form #dailysipbody').focus();
				alert('Please enter text of email');
				return false;
				}
				else
				{
				$('#'+ type +'-form #dailysipbody').parent().removeClass('error');
				data += "&dailysipbody="+Base64.encodeAndEscape($('#'+ type +'-form #dailysipbody').val());
				}
			}
                        
                       $.ajax(
                         {
				type: "POST",
				url: window.location.pathname,
				data: "action=dailysip_preview&id="+ ID + data,
				beforeSend: Util.StartWaiting,
				success: function (r)
				{
                                        Util.StopWaiting();
                                                                           
                                        window.open('/preview/dailysip/'+ $('#'+ type +'-form #urlAlias').val(),'prev_win');                                       
					
				}
			});

                },
		ChangePhoto1: function(iframe)
		{
			image = iframe.contentWindow.document.body.innerHTML;

			if( image != "" )
			{
				$('#picture1').attr('src', '/sites/default/modules/bottlenotes/images/dailysip/'+ image);
				PhotoChanged = true;
			}
		},
	CreateNew:function(num, a)
	{
	    ID=null;
		$('#title').val('');
		$('#sub_title').val('');
		$('#publishDate').val('');
		$('#urlAlias').val('');
		$('#urlAlias1').val('');
		$('#Tags').val('');
		$('#browser_title').val('');
		$('#snippettext').val('');
		//$('#category').val('0');
		$('#headerdiv').html('New Daily Sip');
		 $('#commentid').html('');
		tinyMCE.activeEditor.setContent('');
$('#add-form #title').parent().removeClass('error');
$('#add-form #urlAlias').parent().removeClass('error');
$('#add-form #dailysipbody').parent().removeClass('error');
$('#add-form #publishDate').parent().removeClass('error');
//$('#add-form #category').parent().removeClass('error');
$('#add-form #sub_title').parent().removeClass('error');
		toggle_tab(1, a);
	},
		

		Show: function(id)
		{
			ID = id;
            $('#add-form #title').parent().removeClass('error');
            $('#add-form #urlAlias').parent().removeClass('error');
            $('#add-form #dailysipbody').parent().removeClass('error');
	   		$.ajax(
			{
				type: "POST",
				url: window.location.pathname,
				data: "action=get_details&id="+ ID,
				beforeSend: Util.StartWaiting,
				success: function (r)
				{
					Util.StopWaiting();

					r = eval(r);

					if( r.code == 0 )
					{ 
				          var ds=r.dailysip;
				          var path = "http://bottlenotes.cachefly.net/dailysip/"+ds.ImageURL;
						 $('#title').val(ds.Title);
						 $('#sub_title').val(ds.SubTitle);
						 $('#publishDate').val(ds.PublishDate);
						 $('#urlAlias').val(ds.UrlAlias);
						 $('#urlAlias1').val(ds.UrlAlias);
						 $('#Tags').val('');
						$('#Tags').val(ds.Tags);
						 $('#browser_title').val('');
						  $('#browser_title').val(ds.BrowserTitle);
						 $('#metadescription').val('');
						 $('#metadescription').val(ds.MetaDesc);
						 $('#snippettext').val('');
				 		$('#snippettext').val(ds.SnippetText);
						 $('#primary_category_term').val(ds.CategoryID);
						 $('#category_term').val(ds.TaxonomyTermId);
						 $('#picture').attr('src',path);
     				    $('#headerdiv').html('Edit Daily Sip');
					   $('#commentid').html(r.res);
						$('#search').hide();
					    $('#add-form').show();
						 tinyMCE.activeEditor.setContent(ds.Body);
                    	
					}
					else
					{
						alert(r.msg);
					}
				}
			});
		}
		
    }
}();
