/*
 *ready to proccess the add new page
 **/
$(document).ready(function() {
    
    var websiteUrl = $('#website_url').val();
    $('error').hide();
    
    var template_name = $('#template_name').val();
    if(template_name!=""){
        $('#show-template-list-link').empty();
        $('#show-template-list-link').append(template_name);
    }

    setFeaturedAreaMessage();
    //checking if urls of the editing page is seotoaster draft pages
    var pUrl = $('#pageurl').val();
    if(pUrl == 'draft-pages') {
	$('#lbl-is-category').hide();
	$('#lbl-staticmenu, #lbl-nomenu, #lbl-mainmenu').hide();
	$('#notav').show();
   }

    if($('#pageiscategory').attr('checked')==true){
        $('#pageselectcategory').attr("disabled",true);
    }

    $('#pageiscategory').click(function()
    {
        if($('#pageiscategory').attr('checked')==true){
            $('#pageselectcategory').attr("disabled",true);
        }else
        {
            $('#pageselectcategory').attr("disabled",false);
        }
    });


    $('#template-list-div').hide();
    $('#fa-list-div').hide();
   /* isFLoad
    isTLoad*/
    $('#show-template-list-link').click(function() {
        var isLoaded = $('#isTLoad').val();
        $("#createpage").hide();
        if(isLoaded==0){
            $('#template-list-div').load(websiteUrl+'sys/backend_template/list');
            $('#isTLoad').val(1)
        }
        $("#template-list-div").show();

    });


   $('#show-fa-list-link').click(function() {
        var isLoaded = $('#isFLoad').val();
        $("#createpage").hide();
         if(isLoaded==0){
        $('#fa-list-div').load(websiteUrl+'sys/backend_featuredarea/managefa');
        fillFeaturedCheckBoxes();
        $('#isFLoad').val(1);
        }
        $("#fa-list-div").show();
    });


	$('#pageis404').click(function() {
		
		//checking that 404 page exists
		if(true == $(this).attr('checked')) {
			jQuery.ajax({
				type: "post",
				url: websiteUrl + "sys/backend_page/checknotfound",
				success: function(response) {
					if(response == 'fail') {
						//alert('404 Page does not exists. You should create it.');
					}
					else {
						var conf = confirm('Are you sure you want to replace ' + websiteUrl + response + '.html 404 Page?');
						if(conf) {
							jQuery.ajax({
								type: "post",
								url: websiteUrl + "sys/backend_page/updatenotfound",
								data: 'pageUrl=' + response + '&newVal=' + 0,
								success: function(response) {

								}
							})
						}
						else {
							$('#pageis404').attr('checked', false);
						}
					}
				}
			})
		}
	})
});



function setFeaturedAreaMessage()
{
    $('#show-fa-list-link').empty();
    var  resultedtSring =  $("#featured_ids").val();
    //count how many checkboxes were selected
    var splitted=resultedtSring.split(":");
 
    if((splitted.length)>1){
        $('#show-fa-list-link').append('Yes, '+ (splitted.length-1) +' Times.');
    }else
    {
        $('#show-fa-list-link').append('Not Yet.');
    }
}

function fillFeaturedCheckBoxes()
{
    var str = $("#featured_ids").val();
    
    var splitted=str.split(":");
  
    for(i=0;i<splitted.length-1;i++)
    {
        if(parseInt(splitted[i])){
            $('#facheck-'+splitted[i]).attr('checked',true);
        }
    }

}


function showFaLink()
{
    hideFaMessages();
    $("#createpage").hide();
    $("#fa-list-div").show();
}

//this method is adjust id of selected tempolate to hidden field
function selectTemplate(templateId,templateName)
{
    $("#createpage").show();
    $("#template-list-div").hide();
    $('#template_id').val(templateId);
    $('#template_name').val(templateName);
    $('#show-template-list-link').empty();
    $('#show-template-list-link').append(templateName);
}

function backToManagePageLink()
{
    $("#createpage").animate({
        "height": "toggle",
        "opacity": "toggle"
    }, {
        duration: "slow"
    });

    $("#template-list-div").hide();

    
}

function backToManageTemplateLink()
{
    $("#createpage").animate({
        "height": "toggle",
        "opacity": "toggle"
    }, {
        duration: "slow"
    });

    $("#template-list-div").hide();
}

//this method is proccess url to avoid sppear spaces and put a delimiter '-' between words
function proccessUrl(url)
{
    var ltrim = /^\s+/g;
    var rtrim = /\s+$/g;

    url = url.replace(ltrim,'');
    url = url.replace(rtrim,'');
    if(url != '')
    {
        url = url+'.html';
        return replaceSpace(url,'-');
    }else
    {
        return false;
    }

}
//method puts delimiter '-' istead spaces
function replaceSpace(str,delimiter)
{
    var trim = /\s+/g;
    return str.replace(trim,delimiter);
}

function deleteTemplate(id)
{
    var websiteUrl = $('#website_url').val();
    if(id)
    {
        $.ajax({
            type : 'POST',
            url:''+websiteUrl+'sys/backend_ajax/deletetemplate/',
            data: ({
                id : id
            } ),
            success: function(data){
                if(data>0)
                {
                    $('#template-list-div').load(websiteUrl+'sys/backend_template/list/tmp/editpage');
                    $('#template_id').val(data);
                    $('#template_name').val('default');
                    $('#show-template-list-link').val('default');
                }
                else
                {
                    alert('There is a problem with deleting this template.');
                }
            }
        });
    }

}

function hidePageMessages()
{
    $('#page-errors').hide();
}

function hideFaMessages()
{
    $('#fa-success').hide();
    $('#fa-errors').hide();
    $('#fa-exists').hide();
    $('#fa-empty').hide();
    $('#fa-lenght').hide();
    $('#add-success').hide();
    $('#delete-success').hide();
    $("#featured_name").val("");
}