var selindex=0;
var secSelect=0;

$(document).ready(function(){

    $(window).bind("load",function(){
        RefreshPosts();
    });
	
    $('.txtbox').val("");
    //$('.txtbox').eq(1).val("dd-mm-yyyy");
	
    $('#pge_mask').FullWindow();
    $('#pge_mask').fadeTo(100,0.8);
    $('#comment_box').centreLI();
    $('#login_box').centreLogIn();

    $('.navLst li a').corner('tr tl 5px').click(function(){
        secSelect=$('.navLst li a').index(this);
        if(secSelect==0){$('.secHead').text("Current Offers")}
        else{$('.secHead').text("Current Events")}

        RefreshPosts();
        return false;
    });


    if($('#postinfo').length>0){
     $.ajax({
       type: "POST",
       url: "scripts/php/process.php",
       data: "func=returnDte",
       success: function(data){
         splStr=data.split("{spl}");
         $('.dteFrm').html(splStr[0]);
         $('.dteTwo').html(splStr[1]);
       }
     });
    }
	
    $(window).resize(function(){
        $('#pge_mask').FullWindow();
        $('#comment_box').centreLI();
        $('#login_box').centreLogIn();
    });
	
	
    $('#ulo').click(function(){
        $.post("scripts/php/process.php",{
            func: "logout"
        },function(){
            window.location="default.php";
        });
    });
	
    $('#postinfo').submit(function(){
        if(CheckForVals())
        {
            expStr=selindex+"{spl}"+$('#postinfo input[type="text"]').val()+"{spl}";
            $('#postinfo select').each(function(){expStr=expStr+$(this).val()+"{spl}";});
            expStr=expStr+$('#postinfo textarea').val()+"{spl}";
			
            if($('.buttonformat').eq(0).val()=="Update Post")
            {
                //UPDATE
                 $.ajax({
                   type: "POST",
                   url: "scripts/php/process.php",
                   data: "func=update&inpstr="+expStr,
                   success: function(data){
                        if(data){
                            selindex="";
                            $('.buttonformat').eq(0).val("Submit Post");
                            $('#postinfo input[type="text"], #postinfo textarea').val("");
                            $('#returnresult').text("Record updated successfully.");
                            RefreshPosts();
                        }
                        else{$('#returnresult').text("Unable to update record.");}
                   }
                 });
            }
			
            //new post
            else if($('.buttonformat').eq(0).val()=="Submit Post")
            {
                 $.ajax({
                   type: "POST",
                   url: "scripts/php/process.php",
                   data: "func=addto&inpstr="+expStr,
                   success: function(data){
						if(data){
                            $('#postinfo input[type="text"], #postinfo textarea').val("");
                            $('#returnresult').text("Record added successfully.");
                            RefreshPosts();
                        }
                        else{$('#returnresult').text("Unable to add record.");}
                   }
                 });
            }
        }
        else
        {
            $('#returnresult').text("Please ensure all fields are filled in correctly.");
        }
        return false;
    });
	
	
	
	
    $('.buttonformat').click(function(){
        if($(this).val()=="No")
        {
            $('#pge_wrap').css("display","none");
        }
        else if($(this).val()=="Yes")
        {
            $.post("scripts/php/process.php",{
                func: "removerec",
                recordid: selindex
            },function(data){
                $('#pge_wrap').css("display","none");
                RefreshPosts();
            });
        }
    });
	
	
	
    $('.buttonformat').eq(1).click(function(){
        $('.buttonformat').eq(0).val("Submit Post");
    });
	
    $('#deletepost').submit(function(){
        return false;
    });
	

    $('#li_prompt').submit(function(){
        $('#returnresult').text("Processing...");
        if(CheckForVals())
        {
            i1=$('.txtbox').eq(0).val();
            i2=$('.txtbox').eq(1).val();
            $.post("scripts/php/chkli.php",{
                func: "chklog",
                f1: i1,
                f2: i2
            },function(data){
                if(data=="true")
                {
                    $('#returnresult').text("Redirecting....");
                    window.location="home.php";
                }
                else
                {
                    $('#returnresult').text("Please ensure all fields are filled in correctly.");
                }
            });
        }
        else
        {
            $('#returnresult').text("Please ensure all fields are filled in correctly.");
        }
        return false;
    });

});

//Jquery functions
(function($) {
    $.fn.centreLI = function(){
        w_height=$(window).height()/2;
        w_width=$(window).width()/2;
		
        li_height=$(this).height()/2;
        li_width=$(this).width()/2;
		
        li_top=w_height-li_height;
        li_left=w_width-li_width;
		
        $(this).css({
            "top": li_top+"px",
            "left": li_left+"px"
        });
    };
	
    $.fn.centreLogIn = function(){
        w_height=$(window).height()/2;
        li_height=$(this).height()/2;
        li_top=w_height-li_height;
		
        $(this).css("top",li_top+"px");
    };
	
    $.fn.FullWindow = function(){
        w_height=$(window).height();
        w_width=$(window).width();
        $(this).css({
            "width": w_width+"px",
            "height": w_height+"px"
        });
    };
	
})(jQuery);

//javascript functions

function CheckEmail(inputemail) {
    AtPos = inputemail.indexOf("@");
    StopPos = inputemail.lastIndexOf(".");

    if (AtPos == -1 || StopPos == -1)
    {
        return false;
    }
    else
    {
        return true;
    }

}

function CheckForVals()
{
    inp1=$('#postinfo input[type="text"]').val();
    inp2=$('#postinfo textarea').val();

    if(inp1!="" && inp2!=""){return true;}
    else{return false;}
}

function RefreshPosts()
{
    fleArr=["loadoffers","loadevents"];

    $('#bloginfo').html("<p>Refreshing Posts</p>");
    $('#bloginfo').css("background-position","center center");	

    $('#bloginfo').load("posts.php?le="+fleArr[secSelect]+" #viewposts", function(){
        $('#bloginfo').css("background-position","-250px center");
        $('#returnresult').text("");
			
        $('li.ico_remove').click(function(){
            selindex=$(this).children().attr("rel");
            $('#pge_wrap').css("display","block");
            $('#comment_box').centreLI();
        });
			
        $('li.ico_edit').click(function(){
            selindex=$(this).children().attr("rel");
            $.post("scripts/php/process.php",{
                func:"editrec",
                recordid:selindex
            },function(data){
                splString=data.split("{spl}");
                dte1=splString[2].split("-");
                dte2=splString[3].split("-");
                
                $('#postinfo input[type="text"]').val(splString[0]);
                $('#postinfo textarea').val(splString[1]);
                $('#postinfo select').eq(0).val(dte1[2]);
                $('#postinfo select').eq(1).val(dte1[1]);
                $('#postinfo select').eq(2).val(dte1[0]);
                $('#postinfo select').eq(3).val(dte2[2]);
                $('#postinfo select').eq(4).val(dte2[1]);
                $('#postinfo select').eq(5).val(dte2[0]);
            });

								
            $('.buttonformat').eq(0).val("Update Post");
        });
    });
}