
function changeForeignDiv(){

    var value = $(this).val();
    var fieldName = $(this).attr("name");

    if(value.indexOf("foreign") != -1){
        $(this).next(".foreignDiv").load('actions/showTables.php?fieldName='+fieldName);
    }else if(value.indexOf("photo_upload") != -1){
        $(this).next(".foreignDiv").load('actions/showPhotoUploadOptions.php?fieldName='+fieldName);
    }else if(value.indexOf("thumbnail") != -1){
        $(this).next(".foreignDiv").load('actions/showThumbnailOptions.php?fieldName='+fieldName);
    }else{
        $(this).next(".foreignDiv").html('');
    }

}

function changeForeignFieldDiv(){

    var table = $(this).val();
    var fieldName = $(this).attr("fieldName");

    if(table != ''){
        $(this).next(".foreignFieldDiv").load('actions/showFields.php?table='+table+'&fieldName='+fieldName);
    }else{
        $(this).next(".foreignFieldDiv").html('');
    }

}

$(document).on("change","#install select",changeForeignDiv);
$(document).on('change',"#install .foreignTable",changeForeignFieldDiv);
//$(document).ready(function() {
//   $('#install select').change(function() {
//        var value = $(this).val();
//        if(value == 'foreign'){
//            $(this).next(".foreignDiv").load('actions/showTables.php');
//        }
//   });
//});

$(function(){

        $('#coolMenu').find('> li').hover(function(){
                $(this).find('ul')
                .removeClass('noJS')
                .stop(true, true).slideToggle('fast');
        });

});

$(document).ready(function(){
        $("input[name='tooltipCheckbox']").change(function(){

                $(this).closest(".entry").find(".tooltipEntry").toggle("fast");
        });

        // Advanced Search ; 
        // Remove the required and the validator attributes
        $(".advanced-search input,.advanced-search select,.advanced-search textarea").each(function(){
                $(this).removeAttr("required"); 
                $(this).removeAttr("data-bvalidator"); 
                var fieldName = $(this).attr("name");
                $(this).attr("name",'data[Search]['+fieldName+']');
        });
        
        $(".advancedSearchBtn").click(function(){
                $(".advanced-search").toggle("fast");
        });

});



$(document).ready(function() {

        var docheight = $(document).height();
        $("#menu_container").height(docheight);

        $(window).resize(function() {
                var winheight = $(window).height();
                if(winheight > docheight){
                    $("#menu_container").height(winheight);
                } else {
                    $("#menu_container").height(docheight);
                }
        });

        $('#select-all').click(function(event) {
                if(this.checked) {
                    // Iterate each checkbox
                    $(':checkbox').each(function() {
                            this.checked = true;
                    });
                }else{
                    $(':checkbox').each(function() {
                            this.checked = false;
                    });
                }
        });
        $('#inverse').click(function(event) {
                $(':checkbox').not('#select-all').each(function() {
                        if(this.checked == false){
                            this.checked=true;
                        }else{
                            this.checked=false;
                        }
                });
        });


        $("#delete").click(function(event){

                var $form = $("form");
                event.preventDefault();
                if(confirm("Are you sure you want to delete?")){



                    $("#saveResult").fadeIn("slow");

                    $("#saveResult").html("Deleting...");

                    var datas = $form.serialize();
                    var  url = $form.attr( 'action' );

                    $.ajax({
                            url: url,
                            type: 'POST',
                            dataType: 'html',
                            data: datas,
                            success: function(response, textStatus, XMLHttpRequest) {
                                if (!response){

                                    $("#saveResult").html("There was an error!").delay(2000).fadeOut("slow");
                                    return false;
                                }
                                else {
                                    $("#saveResult").html(response).delay(2000).fadeOut("slow");
                                    if(response == 'Deleted!'){
                                        $("input[type=checkbox]:checked").not("#select-all").each(function(){
                                                $(this).closest('tr').fadeOut();
                                        });
                                    }
                                }
                            },
                            error: function(XMLHttpRequest, textStatus, errorThrown) {
                                $("#saveResult").html("There was an error!");
                                if (typeof console != 'undefined')
                                    console.dir(XMLHttpRequest);
                                return false;
                            }
                    });


                }
        });


        //$("#generateForm").live("submit",startUpload);
        $(document).on('click',"#save", function(event){
                var $form = $("#generateForm");
                var myvalidator = $form.bValidator();
                event.preventDefault();


                $form.trigger("submit");

        });
        $(document).on('click',".deleteFile", function(event){

                var $deleteLink = $(this);

                var rowId = $(this).attr("data-row-id");
                var tableName = $(this).attr("data-table-name");
                var fieldName = $(this).attr("data-field-name");
                var url = $(this).attr("data-url");

                $("#saveResult").fadeIn("slow");

                $("#saveResult").html("Deleting...");

                event.preventDefault();

                $.ajax({
                        url: url,
                        type: 'POST',
                        dataType: 'html',
                        data: {
                            table_name : tableName,
                            id : rowId,
                            field_name : fieldName
                        },
                        success: function(response, textStatus, XMLHttpRequest) {
                            if (response == 'fail'){

                                $("#saveResult").addClass("error").html("There was an error!").delay(2000).fadeOut("slow");
                                return false;
                            }
                            else {
                                $("#saveResult").addClass("success").html('File deleted!').delay(2000).fadeOut("slow");
                                $deleteLink.closest("div.field").find(".previewFileContainer").fadeOut();
                            }
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            $("#saveResult").html("There was an error!");
                            if (typeof console != 'undefined')
                                console.dir(XMLHttpRequest);
                            return false;
                        }
                });

                // $form.trigger("submit");

        });


        //Function that saves the form
        //Function is used for frontEnd and backEnd saving
        //Validates the form fields with bValidator
        /*
        function saveGenerateForm(event){
        var $form = $("#generateForm");
        var myvalidator = $('#generateForm').bValidator();
        event.preventDefault();
        if(myvalidator.data('bValidator').isValid()) {


        $("#saveResult").fadeIn("slow");

        $("#saveResult").html("Submitting...");

        var datas = $form.serialize();
        var  url = $form.attr( 'action' );
        $.ajax({
        url: url,
        type: 'POST',
        dataType: 'html',
        enctype: 'multipart/form-data',
        data: datas,
        success: function(response, textStatus, XMLHttpRequest) {
        if (!response){

        $("#saveResult").html("There was an error!").delay(2000).fadeOut("slow");
        return false;
        }
        else {
        if(response == 'Created!'){
        $("#save").hide();
        }
        $("#saveResult").html(response).delay(2000).fadeOut("slow");


        }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        $("#saveResult").html("There was an error!");
        if (typeof console != 'undefined')
        console.dir(XMLHttpRequest);
        return false;
        }
        });

        }
        }
        */


});

function initMCE(){
    tinyMCE.init({
            // General options
            mode : "textareas",
            editor_selector : "mceEditor",
            editor_deselector : "mceNoEditor",
            relative_urls : false,
            remove_script_host : false,
            convert_urls : true,
            //        content_css : "/css/styles.css",
            forcePasteAsPlainText : true,

            theme : "advanced",
            plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,images",

            // Theme options
            //theme_advanced_buttons1 : "images,save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
            //            theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
            //            theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
            //            theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
            theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontsizeselect,|,cleanup,|,code,",
            theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,images,|,insertdate,inserttime,preview,|,forecolor,backcolor",
            theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,ltr,rtl,|,fullscreen",
            //theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
            theme_advanced_statusbar_location : "bottom",
            theme_advanced_resizing : true,

            // Skin options
            skin : "o2k7",
            skin_variant : "silver",
            // Drop lists for link/image/media/template dialogs
            template_external_list_url : "js/template_list.js",
            external_link_list_url : "js/link_list.js",
            external_image_list_url : "js/image_list.js",
            media_external_list_url : "js/media_list.js",

            // Replace values for the template plugin
            template_replace_values : {
                username : "Some User",
                staffid : "991234"
            }

    });
}

jQuery(document).ready(function($) {
        $('.ConfirmDelete').click(function() {
                return confirm('Delete record?');
        });
});


//Ajax UPLOAD SCRIPTS

function startUpload(event){
    var $form = $("#generateForm");
    var myvalidator = $('#generateForm').bValidator();

    if(myvalidator.data('bValidator').isValid()) {
        $("#saveResult").fadeIn("fast");

        $("#saveResult").html("Submitting.....");
        if(!isLiveEdit()){
            if(($(".modal").length == 0 )){
                $("body").append("<div class='modal'></div>").find(".modal").fadeIn();

            }else{
                $(".modal").fadeIn();
            }
        }
    }
}


function isLiveEdit(){
    if($("#liveEditContainer").length > 0 ){
        return true;
    }else{
        return false;
    }
}

function stopUpload(success,tableName){
    var result = '';
    if(!isLiveEdit()){
        $(".modal").delay(3000).fadeOut();
    }

    if (success == 1){
        if(isLiveEdit()){
            $("#saveResult").html("Saved! Refreshing").delay(1000).fadeOut("slow");

            setTimeout(function() {
                    $("#liveEditContainer").fadeOut("fast");
                    window.location.reload(true);
                }, 1000);
        }else{
            $("#saveResult").removeClass("error").addClass("success").html("Saved!").delay(3000).append(" Redirecting you...").fadeOut("slow",redirect(tableName));
        }
    }else{
        $("#saveResult").addClass("error").html("Error saving.<br /> "+success).delay(3000).fadeOut("slow");
    }
    return true;
}



function updateCoords(c)
{
    $('#x').val(c.x);
    $('#y').val(c.y);
    $('#w').val(c.w);
    $('#h').val(c.h);
};

function checkCoords()
{
    if (parseInt($('#w').val())) return true;
    alert('Please select a crop region then press submit.');
    return false;
};

$(document).ready(function(){


        $('.date-pick').livequery(function(){

                Date.firstDayOfWeek = 0;
                Date.format = 'yy-mm-dd';

                $( ".date-pick" ).datepicker({dateFormat:"yy-mm-dd"});
        });
        /*
        $('.url').livequery(function(){
        $(".url").each(function(){
        var value = $(this).val();
        if(value == ""){
        $(this).val("http://");
        }
        });

        });
        */
        //Handles the time field
        $(document).on('change',"select[fieldType='time']", function(){
                var fieldName = $(this).attr('fieldName');
                var hours = $("select[name='"+fieldName+"_cms_hour']").val();
                var mins = $("select[name='"+fieldName+"_cms_minute']").val();
                var secs = $("select[name='"+fieldName+"_cms_seconds']").val();
                $("input[name='"+fieldName+"']").val(hours+":"+mins+":"+secs);
        });



        //Image crop


        $(document).on('click',".cropSubmit",function(){
                if (  $("#x").val() == '' || $("#y").val() == '' || $("#w").val() == '' || $("#h").val() == ''){
                    return false;
                }
                $("#saveResult").fadeIn("fast");
                $("#saveResult").html("Submitting.....");


                var $form = $(this).closest("form");
                var datas = $form.serialize();
                var  url = $form.find("[name='cropUrl']").val();

                $.ajax({
                        url: url,
                        type: 'POST',
                        dataType: 'html',
                        data: datas,
                        success: function(response, textStatus, XMLHttpRequest) {
                            if (!response || response == '0'){
                                $("#saveResult").html("There was an error!").delay(2000).fadeOut("slow");
                                return false;
                            }
                            else {
                                var croppedImageUrl = response;
                                var croppedImage = "<img src='"+croppedImageUrl+"' />";
                                $("#saveResult").html("Image was successfully cropped!<br />"+croppedImage+"<br /><br />").delay(3000).fadeOut("fast");
                                $("#thumb_gen").hide(500);
                                $("#thumbContainer").html(croppedImage);
                                jcrop_api.release();
                                $("#x").val('');
                                $("#y").val('');
                                $("#w").val('');
                                $("#h").val('');
                            }
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            $("#saveResult").html("There was an error!");
                            if (typeof console != 'undefined')
                                console.dir(XMLHttpRequest);
                            return false;
                        }
                }); 
        });




        $(".loadMoreForm").submit(function(e){
                e.preventDefault();

                var classes = $(".loadToThis").find("li:first").attr("class");
                var loadMoreHtml = $(".loadToThis .loadMore").closest("li").html();
                var url = $(this).attr("action");
                var data = $(this).serialize();

                $.post(url, data ,
                    function(result){
                        $(".loadToThis").html('');
                        $.each(result,function(k,v){
                                $(".loadToThis").append('<li class="'+classes+'"><a href="'+v.link+'">'+v.displayField+'</a></li>')
                        });

                        $(".loadToThis").append('<li>'+loadMoreHtml+'</li>');

                        $( "#loadMoreModal" ).dialog("close");

                        initTooltips();
                    }, "json");

        });

        $(".bulkEditForm").submit(function(e){
                var $form = $(this);
                $form.find("input[type='submit']").val('Saving...');
                e.preventDefault();
                var url = $(this).attr("action");
                var data = $(this).serialize();
                $("#bulkEditContainer .result").html('Saving...');
                $.post(url, data ,
                    function(result){

                        $form.find("input[type='submit']").val('Save');
                        if(result.status == 'success'){
                            $("#bulkEditContainer .result").addClass('success');
                            $("#bulkEditContainer .result").html('Saved.');
                        }else{
                            $("#bulkEditContainer .result").addClass('error');
                            $("#bulkEditContainer .result").html(result.error);
                        }

                    }, "json");
        });



        // END IMage crop

        $(document).on('click',".loadMore",function(){

                $(".loadToThis").each(function(){
                        $(this).removeClass("loadToThis");
                });
                $(this).closest("ul").addClass("loadToThis");

                var tableName = $(this).attr('data-table-name');
                $(".orderBySelect").html("<option value=''>Loading...</option>");
                $.get("actions/getTableFields.php?tableName="+tableName+"",
                    function(data) {
                        $(".orderBySelect").html("<option value=''>Please select a field</option>");
                        $.each(data,function (key,value){
                                $(".orderBySelect").append("<option value='"+key+"'>"+value+"</option>");
                        });

                    }, "json");

                var $cur = $(this);

                var foreignKey = $(this).attr('data-foreign-key');
                var foreignKeyId = $(this).attr('data-foreign-key-id');

                $( "#loadMoreModal [name='tableName']").val(tableName);
                $( "#loadMoreModal [name='foreignKey']").val(foreignKey);
                $( "#loadMoreModal [name='foreignKeyId']").val(foreignKeyId);

                $( "#loadMoreModal" ).dialog({
                        height: 280,
                        width: 370,
                        modal: true,
                        title : 'Load more records',
                });
        });
        $(document).on('click',".bulkEditBtn" ,function(){
                $("#bulkEditContainer .result").html('');
                $(".modal").toggle();
                $("#bulkEditContainer").toggle("normal");
        });
        $(document).on('click',".modal,.closeBulkEditBtn",function(){
                $(".modal").hide();
                $("#bulkEditContainer").hide("normal");
        });
        $(document).on('change',"#select-all", function(){
                $("#list input[name='ids[]']").each(function(){
                        var value = $(this).val();
                        if($(this).is(':checked')){
                            $("#cmsgen_rowsToEdit [value='"+value+"']").attr("selected","selected");
                        }else{
                            $("#cmsgen_rowsToEdit [value='"+value+"']").removeAttr("selected");
                        }
                });
        });
        $(document).on('change',"#list input[name='ids[]']", function(){
                var value = $(this).val();
                if($(this).is(':checked')){
                    $("#cmsgen_rowsToEdit [value='"+value+"']").attr("selected","selected");
                }else{
                    $("#cmsgen_rowsToEdit [value='"+value+"']").removeAttr("selected");
                }
        });
});
