$(document).ready(function() {
		//References
		var pages = $("#menu a");
		var loading = $("#loading");
		var content = $("#content");
		var contentabuse = $("#content .report_abuse");

		//show loading bar
		function showLoading(){
			$('#content .report_abuse').click(reportAbuse);
			$('#content .report_delete').click(reportDelete);
			$('#content .db_follow').nyroModal(); //comments popup
			$('#content .followuser').click(userFollow);


			//$('#content #db_follow').click(function(e) {
			//	e.preventDefault();
			//	$.nyroModalManual({
			//		url: 'demoSent.php'
			//	});
			//	return false;

			//});


			loading
				.css({visibility:"visible"})
				.css({opacity:"1"})
				.css({display:"block"})
			;
		}
		//hide loading bar
		function hideLoading(){
			loading.fadeTo(10, 0);
			$('#content .report_abuse').click(reportAbuse);
			$('#content .report_delete').click(reportDelete);
			$('#content .db_follow').nyroModal(); //comments popup
			$('#content .followuser').click(userFollow);
		};


		//Manage click events
		pages.click(function(){
			//show the loading bar
			showLoading();

			//Load content
			var pageNum = this.id;
			var targetUrl = "index.php?option=com_comment&format=raw&layout=default_comments&page=" + pageNum + "&" + $("#myForm").serialize() + " #content";
			content.load(targetUrl, hideLoading);

		});

			var targetUrl = "/index.php?option=com_comment&format=raw&layout=default_comments&page=1&" + $("#myForm").serialize() + " #content";
			showLoading();
			content.load(targetUrl, hideLoading);

	//**************************************************
	// Comments END
	//**************************************************


}); //end it here

function resetFields(){
	//$('#commentsform #name').val('Name');
	$('#commentsform #comment_comment').val('Write a comment');
	//$('#commentsform #email').val('Email');
	//$('#commentsform #captcha').val('Verification Code');
}


function reportAbuse(e) {

	e.preventDefault();
	var commentId = $(this).attr('id');
	//alert(commentId);
	$.ajax({
		type: 'POST',
		url: '/includes/report_abuse.php',
		dataType: 'json',
		data: {commentId: commentId}});

	$(this).closest('.box').remove();

	//Load content
	var pageNum = this.id;
	var targetUrl = "index.php?option=com_comment&format=raw&layout=default_comments&page=" + pageNum + "&" + $("#myForm").serialize() + " #content";
	content.load(targetUrl, hideLoading);

	//});
}

function reportDelete(e) {

	e.preventDefault();
	var commentId = $(this).attr('id');
	//alert(commentId);
	$.ajax({
		type: 'POST',
		url: '/includes/report_delete.php',
		dataType: 'json',
		data: {commentId: commentId}});

	$(this).closest('.box').remove();

	//});
}

function userFollow(e) {

	e.preventDefault();

	// get rating function
		var idfollow = $('.followuser').attr('id');
		var userid = $('.followuser').attr('href').split("-")[1];

		$.ajax({
			type: "GET",
			url: "/includes/do_follow.php",
			data: "do=getfollow&id="+idfollow+"&user="+userid,
			cache: false,
			async: false,
			success: function(result) {
				// apply follow element
				$("#content").refresh();
			},
			error: function(result) {
				alert("some error occured, please try again later");
			}
		});
}


var formDetails = new Array();


function clearFormField(form){
	var formName = $(form).attr('id');
	formDetails[formName] = new Array();

	$(form).find('input[type=text],textarea').each(function() {
		var formName = $(this).closest('form').attr('id');
		var fieldName = $(this).attr('id');
		//save original input (if any)
		formDetails[formName][fieldName] = $(this).attr('value');

		$(this).focus(function() {
			var formName = $(this).closest('form').attr('id');
			var fieldName = $(this).attr('id');
			if($(this).val() == formDetails[formName][fieldName])
			{
				$(this).val('');
			}
		}).blur(function() {
			if($(this).val() == '')
			{
				var formName = $(this).closest('form').attr('id');
				var fieldName = $(this).attr('id');
				$(this).val(formDetails[formName][fieldName]);
			}
		});
	});
}

function  refresh() {
	var sURL = unescape(window.location.pathname);
	window.location.href = sURL;
	clearTimeout();
}

//**************************************************
// Comments END
//**************************************************






















function startSignUp() {

	//check if cookie/session has been set.
	$.getJSON('/includes/check_signup.php', function(data) {
		if(data.success)
		{
			alert('success');

		}
		else
		{
			//$('#add_sub_user').nyroModal();
			//http://www.whatsfordinner.dev/index.php?option=com_frontend&view=signup&format=ajax
			alert('error');

			$.nyroModalManual({
				url: "/index.php?option=com_frontend&view=signup&format=ajax",
				ajax: {data: params, type: 'post'},
				width: 750,
				height: 350,
				minWidth: 400,
				minHeight: 300,
				resizeable: true,
				autoSizeable: true
		});


		}
	});

}


      function startSignUpFormValidation() {
    		//setup validation for form
    	  //return false;
    	  $(".submit_signup").click(function() {
    		  alert('validate');

    		  	//check the fields for validation
    			var firstname 		= $("#firstname").val();
    			var lastname 	    = $("#lastname").val();
    			var email 			= $("#email").val();
    			var password 		= $("#password").val();
    			var verification 	= $("#verification").val();

    			var signupData = 'firstname='+ firstname + '&lastname=' + lastname + '&email=' + target + '&password=' + password + '&action=' + action;
    			alert(signupData);

    			return false;

    	  });
    	  /*
    		$('#frm').closest('form').validate({
    			errorElement:'div',
    			rules: {
    			firstname: "required"// simple rule, converted to {required:true}
    		      },
    		        messages: {
    		          comment: "Please enter a comment."
    		        },
    	 		submitHandler: submitForm,
    	 		invalidHandler: invalidForm
    	    });
    	    */
    	}


    //because form dynamically changes we need to reset the height of the modal window
      function invalidForm(form) {
      	$.fn.colorbox.resize();
      }




      function submitForm(form) {

    		$(form).ajaxSubmit({
    			dataType:'json',
    			success: function(data) {

    			if(data.message == "Friend Referred."){
    				if(referral_count == 4){
    					$.fn.colorbox.close();
    					referral_count = 0;
    				  }
    				else{
    					referral_count=referral_count+1;
    					$('<p>Friend Referred</p>').insertBefore('#formelement').hide().slideDown('slow').delay(2000).slideUp('slow');

    				}
    			}

    			if(data.success)
    			{
    				alert('hello');
    					$('.popup #formdetails,.popup #friendform,.popup #smsdetails,.popup #notesform').hide();
    					if (data.message != '')
    					{
    						$('.popup .message p').html(data.message);
    						$('.popup .message').show();
    					}
    					else
    						$('.popup #thanks,').show();

    			 	}
    				else
    				{
    					$('.popup').hide();
    					$('.popup .message p').html(data.message);
    					$('.popup .message').show();
    				}

    		 	}
    		});
    	}















//cook book - start

                       $('a.cookbook').live("click", function(e){
                        //	e.preventDefault();
                            var rel = $(this).attr('rel');
                            var url = '/index.php?option=com_recipe&view=cookbook'+rel;
                            var button_text = $(this).text();
                            var id_arr = $(this).attr('id');
                            id_arr = id_arr.split('_');
                            var do_what = id_arr[0];
                            id = id_arr[1];
                            if(do_what == 'cookbookadd' || do_what == 'cookbookdelete'){
                                    url += "&task="+do_what;

                                    $.get(url, function(data){
                                            var data = data.split('_');
                                            if(data[0] == 'Success'){
                                                    if(do_what == 'cookbookadd'){
                                                            $("a#cookbookadd_"+id).replaceWith('<a href="javascript:void(0);" id="cookbookdelete_'+id+'" rel="'+rel+'" class="remove_from_cookbook cookbook">Remove Recipe</a>');
                                                    }else{
                                                            $("a#cookbookdelete_"+id).replaceWith('<a href="javascript:void(0);" id="cookbookadd_'+id+'" rel="'+rel+'" class="add_to_cookbook cookbook">Add to Cookbook</a>');
                                                    }
                                       }
                                    });
                            }
                        });

//cook book - end
