// JavaScript Document
initPage();

BRANDITCODE = "../code/brandit.aspx?";

function loadPage() 
{
	initTabs();
	uidQS = "&uid=" + userID;

    loadPublishedFiles();
    loadPDFAvailable();
    new loadUserLogosCallBack().invoke();

    $("#brandit #setupBrand").load("../fragments/branditeditor.html" + GetCacheBlock(true));

    $("#brandit #branditButton").click(function() {
        $("#messages").css("visibility", "visible");
        $("#messages").html("Branding Started. Please wait... <img src='../media/images/ajax/progress_bar_2.gif' align='absmiddle'>");
        var files = "";
        var selIndustry = $("#brandit .content select#selIndustries").val();
        selIndustry = selIndustry.substr(selIndustry.lastIndexOf("/") + 1)

        if (selIndustry != "" && selIndustry != "none") {
            //alert(selIndustry);
            files = "Sales_Sheet=" + selIndustry;
        }

        files += ";Spec_Sheet=";
        $("#brandit .content #files input.pdffile:checked").each(function() {
            var filename = $(this).val();
            //Must include full path because of multi-lingual features
			//filename = filename.substr(filename.lastIndexOf("/") + 1);
			//filename = filename.replace("../build/","build/");
            files += filename + ",";
        });

        var isChecked = $("#makeBooklet:checked").length;
        var oqs = "&mb=" + isChecked + "&bfn=" + $("#bookletFileName").val();

        //console.log(files);
        //return;
        $.get(BRANDITCODE + "brandit=1&files=" + files + GetCacheBlock() + uidQS + oqs, function(data) {
            if (data == "File Exist") {
                alert("Booklet file name exist already. Please enter a different name. Or delete existing PDF.");
                $("#messages").html("");
            }
            else {
                $("#messages").html(data);
                loadPublishedFiles();
            }
        });
    });

    $("#brandit .content #myfiles .deleteButton").live('click', function()
	{
        //Popup Confirm Messwage
        var q = BRANDITCODE + "delete=../" + $(this).attr('id') + uidQS + GetCacheBlock();
        $.get(q, function(data) {
            //Load files again
			//alert(data);
            loadPublishedFiles();
        });
    });


    //handle Industries drop down
    $("select#selIndustries").live('change', function() {
        if ($(this).val() != "none") {
            var industry = new String($(this).val());
            industry = industry.replace("../media/pdf/brandit/Sales_Sheet/", "").replace(".pdf", "").replace(/industry/i, "");

            if ($("input#autoSelectSpecs:checked").length > 0) {
                //Get AutoSelect Specs
                var q = BRANDITCODE + "getautosel=" + industry + uidQS;
                $.get(q, function(data) {

                    var msg = "Related specsheets have been selected below based ";
                    msg += "on the industry you just selected.\n You can unselect ";
                    msg += "them if you do not need to include them.";
                    //alert(msg);
                    relspecs = data.split(";");
                    if (relspecs.length > 0) {
                        //Clear all checkboxes
                        $("#brandit input.pdffile").removeAttr("checked");

                        //Begin Autoselecting specs					
                        for (spec in relspecs) {
                            //alert("searching for: " + relspecs[spec]);
                            $("#brandit input.pdffile[title='" + relspecs[spec] + "']").attr("checked", "1");
                        }

                        //Enable publishing controls
                        enableDisablePublishingControls(true);
                    }
                });
            }
            else {
                //Enable publishing controls
                enableDisablePublishingControls(true);
            }

        }
        else {
            //Clear all checkboxes
            $("#brandit input.pdffile").removeAttr("checked");
            //Enable publishing controls
            enableDisablePublishingControls(false);
        }
        return true;
    });

    $("#brandit .content input.pdffile").live('click', function() {
        //Popup Confirm Messwage		
        enableDisablePublishingControls(/*$("select#selIndustries").val() != "none"*/);
        return true;
    });

    $("#brandit .content input#checkAllSpecs").live('click', function() {

        $("#brandit .content #files input.pdffile").each(function() {
            if ($("#brandit .content input#checkAllSpecs").attr("checked") == "1") {
                $(this).attr("checked", "1");
            }
            else {
                $(this).removeAttr("checked");
            }
        });

        enableDisablePublishingControls($("select#selIndustries").val() != "none");
    });

    $("#brandit #uploadButton").live('click', function() {

        /*if( $("#brandit .fileToUpload").text() == "")
        {
        alert("Please browse for a file.");
        }*/    
        if ($("#brandit .fileToUpload").val().search(/GIF|JPG|JPEG|BMP|PNG/i) >= 0) {
            //alert("hey");
            //ajaxFileUpload(new loadUserLogosCallBack());
        }
        else {
            alert("File extension is invalid.\n File must be of the following types:\n GIF, JPG, JPEG, BMP, PNG");
        }
    });

}

function enableDisablePublishingControls(industrySelected) {
    var checked = $("#brandit .content #files input.pdffile:checked").length;

    if (checked > 0) {
        if (checked >= 1) {
            $("#brandit #branditButton").removeAttr("disabled");

            $("#brandit #labelSelSpecsCount").text(" (" + checked + " specsheets selected)");
            $("#brandit #makeBookletPanel").show();
        }
        else {
            $("#brandit #labelSelSpecsCount").text("");
            $("#brandit #makeBookletPanel").hide();
        }
    }
    else {

        if (industrySelected) {
            $("#brandit #branditButton").removeAttr("disabled");
        }
        else {
            $("#brandit #branditButton").attr("disabled", "yes");
        }

        $("#brandit #labelSelSpecsCount").text("");
        $("#brandit #makeBookletPanel").hide();
    }
}

function loadPublishedFiles() {
    $("#myfiles").html("Loading files. Please wait...");
    $.get(BRANDITCODE + "getfiles=1" + GetCacheBlock() + uidQS, function(data) {
        $("#brandit #myfiles").html(data);
    });
}

function loadPDFAvailable() {
    $("#files").html("Loading files. Please wait...");
    var q = BRANDITCODE + "getpdfsv2=1" + GetCacheBlock() + uidQS;
    $.get(q, function(data) {
        $("#files").html(data);

        //$("#files #treeview1").treeview();
    });
}

function loadUserLogosCallBack() {
    this.invoke = function() {
        $("#brandit #logos").html("Loading files. Please wait...");
        $.get(BRANDITCODE + "getlogos=1" + GetCacheBlock() + uidQS, function(data) {
            $("#brandit #logos").html(data);
        });
    }
}

function initTabs()
{
	var tabs=new ddtabcontent("tabs");
	tabs.setpersist(true);
	tabs.setselectedClassTarget("link"); //"link or "linkparent"
	tabs.init();
}
