﻿/// <reference path="jquery-1.3.2.min.js" />
/// <reference path="ui/ui.tabs.js" />
/// <reference path="ui/ui_widgets/json2/json2.js" />
/// <reference path="ui/ui_widgets/jquery-cookie/jquery.cookie.min.js" />
/// <reference path="ui/ui_widgets/jquery-cookie/jquery.jsoncookie.js" />
/// <reference path="ui/ui_widgets/timer/jquery.timers.js" />

/*
* Tfk Client Helper JavaScript
* Copyright (c) 2009 TFK 
* Date: 2009 00:00:00
*/
/* NOTE: for pack need use http://dean.edwards.name/packer/ */

///	<summary>
/// Form Helper Method
///	</summary>
function OnCheck(checkBoxCtrlName, buttonCtrlName) {
    if ($("#" + checkBoxCtrlName).attr("checked")) {
        $("#" + buttonCtrlName).removeAttr("disabled", "true");
    }
    else {
        $("#" + buttonCtrlName).attr("disabled", "true");
    }
};

///	<summary>
/// Show/Hide Panel [Timer]
///@hideTime => 3000 seconds
///	</summary>
function ShowAcceptedNotificationMesssage(messageHolderId, hideTime) {
    hideTime = hideTime == null ? 3000 : hideTime; 
    $("#"+messageHolderId).show("slow", function() {
        $("#"+messageHolderId).oneTime(hideTime, "hide", function() {
            $("#"+messageHolderId).hide("slow");
        });
    });
};

//#REGION: jq-ajax
///	<summary>
/// Show/Hide Ajax progress bar
/// Using this div:
///<div id="loading" style="display:none;"><img src="<%= Url.Image("ajax-loader.gif") %>" /></div>
///	</summary>
//[Obsolete]
function AjaxLoader() {
    $("#loading").ajaxStart(function() {
        $(this).show("slow");
    });

    $("#loading").ajaxStop(function() {
        $(this).hide("slow");
    });
};

function InitAjaxLoadingPanel(panelId) {
    $("#" + panelId).ajaxStart(function() {
        $(this).show("slow");
    });

    $("#" + panelId).ajaxStop(function() {
        $(this).hide("slow");
    });
};

//Invoke AJAX request to in URL and Load Partial View
function execActionByAjax(url, param, holder) {
    $("#" + holder).load(url, param);
};

function execActionByAjaxWithEvents(url, param, holder) {
    $("#" + holder).load(url, param, requestIsComplete);
    return requestIsComplete;
};

//#END_REGION jq-ajax

//#REGION: ui-widgets

//[Obsolete; Need to use jqDialog func] Create Dialog and Settings
function Dialog(dialogDiv, searchFunc, cancelFunc, openFunc,
    in_width, in_height) {
    $('#' + dialogDiv).dialog({
        autoOpen: false,
        width: in_width,
        height: in_height,
        resizable: false,
        modal: true,
        draggable: false,
        open: function(event, ui) {
            openFunc();
        },
        buttons: {
            "Cancel": function() {
                cancelFunc();
                $(this).dialog("close");
            },
            "Join": searchFunc
        },
        beforeclose: cancelFunc
    });
};

//Dialog without spec. Events
//Sample: SimpleDialog('edit_photoalbum_dialog', 600, 400);
function SimpleDialog(dialogDiv, in_width, in_height) {
    $('#' + dialogDiv).dialog({
        autoOpen: false,
        width: in_width,
        height: in_height,
        resizable: false,
        modal: true,
        draggable: false,
        buttons: { "Cancel": function() { $(this).dialog("close"); }
        }
    });
};

//Configurate Dialog Buttons Array by 'dialog div name'
function DialogButtons(buttonsListValue, searchFunc) {
    switch (buttonsListValue)
    {
        case 'logon_dialog':
            return null;
            break;
        case 'delete_photoalbum_dialog':
            return { "Cancel": function() {
                $(this).dialog("close");
            },
                "Delete": function() {
                    searchFunc();
                }
            };
            break;
        case 'edit_photoalbum_dialog':
            return { "Cancel": function() {
                $(this).dialog("close");
            },
                "Update": function() {
                    searchFunc();
                }
            };
            break;
        case 'cvv_description_window':
            return { "Close": function() {
                $(this).dialog("close");
            }
           };
           break;
        default:
            return { "Cancel":
                      function() {
                          cancelFunc();
                          $(this).dialog("close");
                      },
                "Search": function() { searchFunc(); }
            };
            break;
    }
};

//[CONFIG DIALOG FUNCTION] Create Dialog and Settings
function jqDialog(dialogDiv, searchFunc, cancelFunc, in_width, in_height) {
    $('#' + dialogDiv).dialog({
        autoOpen: false,
        width: in_width,
        height: in_height,
        resizable: false,
        modal: true,
        draggable: false,
        buttons: DialogButtons(dialogDiv, searchFunc),
        beforeclose: cancelFunc,
        open: function() {
            $(this).parents('.ui-dialog-buttonpane button:eq(0)').focus();
        }
    });
};

//Create Accordion UI windjet
//Sample: Accordion("PublicPageTextHolder", false, true, 0);
function Accordion(accordion_div, ChangeFunction, isCollapsible, isActive) {
    // Accordion
    $("#" + accordion_div).accordion({ header: "h3",
        change: ChangeFunction, collapsible: isCollapsible, active: isActive,
        icons: {
            header: "ui-icon-circle-plus",
            headerSelected: "ui-icon-circle-minus"
        }
    });
};

//Dialog Link
//Sample: DialogLink('dialog_link_edit_photoalbum', 'edit_photoalbum_dialog');
function DialogLink(dialog_link, dialogDiv){
    $('#' + dialog_link).click(function() {
        $('#' + dialogDiv).dialog('open');
        $("#username").focus();
        return false; 
    });
};

function CloseDialogs(dialogDiv) {
    $('#' + dialogDiv).dialog('close');
};

//UI Tabs widjet
function Tabs(tabsDivName, isSortable) {
    if (isSortable) {
        $('#' + tabsDivName).tabs().find(".ui-tabs-nav").sortable({ axis: 'x' });
    }
    else {
        $('#' + tabsDivName).tabs({
            select: function(event, ui) {
                if (ui.tab.hash == "#EditMemberText"
                    || ui.tab.hash == "#EditUserPhotoGallery"
                    || ui.tab.hash == "#UploadPhotosInGallery") {
                    $.cookie("the_tab_cookie", 0); //ui.index
                }
                //If tab Selected default index = 0;
                $.cookie("the_edit_tab_cookie", 0);
            },
            show: function(event, ui) {
                $(this).show(1);
            }
        });
    }
};

//#Tab State Resore
function RestoreTabState(tabId) {
    var tab_cookie_id = parseInt($.cookie("the_tab_cookie")) || 0;
    $('#' + tabId).tabs('select', tab_cookie_id);
};

//#Tab State Save
function SaveTabsState(stateValue) {
    $.cookie("the_tab_cookie", stateValue);
};

//UI DatePicker
//SIMPLE: DatePicker('Birthday');
function DatePicker(trigerName, minValue, maxValue) {
    var options = {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        yearRange: '-100:+100',
        numberOfMonths: 1
    };
    if (typeof (minValue) != 'undefined' && minValue !== null) {
        options.minDate = minValue;
    }
    if (typeof (maxValue != 'undefined') && maxValue !== null) {
        options.maxDate = maxValue;
    }
    $("#" + trigerName).datepicker(options);
};

//#Init hover states on the static widgets (#dialog_link)
function InitStaticUIWidgets() {
    $('ul#icons li').hover(
					function() { $(this).addClass('ui-state-hover'); },
					function() { $(this).removeClass('ui-state-hover'); });

};

function MessageBox(holderId, messageBoxText, height, width) {
    height = (height == null) ? 'auto' : height;
    width = (width == null) ? '550' : width;
    $("#"+holderId).append('<div class="ui-widget" style="vertical-align:top;">'+
                        '<div class="ui-state-highlight ui-corner-all"' +
                            ' style="height:' + height + 'px; width: ' + width + 'px;'
                            +'vertical-align: middle; margin-top: 10px; padding: 0.6em; text-align: left; font-size: 0.8em;">' +
                            '<span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>'
                           +messageBoxText+
                        '</div></div>');
};
//#END INIT STATIC widjets

// Perform common initializations.
$(function(){
    // Initialize accordions by CSS class name
    $(".accordion-form").accordion({
        header: "h3", change: null, collapsible: true, active: 0
    });
});

function Slider(sliderElem, holder, min, max, step) {
    $("#" + sliderElem).slider({
        min: min,
        max: max,
        step: step,
        slide: function(event, ui) { $("#" + holder).val(ui.value); }
    });
    $("#" + holder).val($("#" + sliderElem).slider("value"));
};

function CreateLightbox(selector, globalPath) {
    $(selector).lightbox({
        fitToScreen: true, imageClickClose: false,
        fileLoadingImage: globalPath + 'images/lightbox-ico-loading.gif',
        fileBottomNavCloseImage: globalPath + 'images/closelabel.gif',
        loopImages: true, disableNavbarLinks: false
    });
};

//# Cookie Works Section
function RestoreDataFromCookie(cookieName) {
    var cookie_data = $.cookie(cookieName) || "none";
    return cookie_data;
};

function SaveDataToCookie(data, cookieName) {
    if (data != "") {
        $.cookie(cookieName, data);
    }
};

function DeleteDataCookie(cookieName) {
    $.cookie(cookieName, '', { expires: -1 });
};

function RestoreJSONDataFromCookie(cookieName) {
    cookie_object = $.JSONCookie(cookieName);
    return cookie_object;
};

function SaveJSONDataToCookie(data, cookieName) {
    if (data != "") {
        $.JSONCookie(cookieName, data/*, { path: '/' }*/);
    }
};

function DeleteJSONDataCookie(cookieName) {
    $.removeJSONCookie(cookieName);
};
//#End Cookie Works Section

function carousel_getItemHTML(item) {
    return '<div class="image-carousel-containter"><a href="' + item.url_origin + '" class="lightbox" rel="nyrr"  title="' + item.description + '">'
            + '<img src="' + item.url + '" alt="' + item.description + '" /></a></div>';
};

function HtmlEditor(scriptPath) {
    var test = $('textarea.tinymce').tinymce({
        // Location of TinyMCE script
        script_url: scriptPath,

        // General options
        theme: "advanced",
        plugins: "safari,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",

        // Theme options
        theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,fontselect,fontsizeselect,|,visualchars,nonbreaking",
        theme_advanced_buttons2: "cut,copy,paste|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,|,cite,abbr,acronym,del,ins",
        theme_advanced_buttons3: "insertdate,inserttime,preview,|,forecolor,backcolor,|,print,|,hr,removeformat,|,sub,sup,|,charmap,emotions",        
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "none",
        theme_advanced_resizing: false,
        setup: function(ed) { ed.onInit.add(function() { $("span.tinymce").show(); }); } // /*$("span.tinymce").css('display', ''); window.setTimeout('$("span.tinymce").show()', 1000);*/ };}
    });
}


function DisableSubmitButton() {
    var btn = $('input[type="submit"]');
    var formWithId = $("form[id]");
    if (formWithId.length == 0){
        btn.bind('click', function(eventObject) {
            btn.attr("disabled", "true");
            $("form").submit();
        });
    }
}

function CheckBrowser() {
    if ($.browser.msie) {
        var messageBoxText =
                "<span class='ui-icon ui-icon-alert' style='float: left; margin-right: 0.3em;'></span>" +
                "We are sorry, you appear to be using a version of the browser that is not supported by this site." +
                "You may experience display issues if you continue to use this browser." +
                "We recommend that you switch to using Mozilla Firefox or Microsoft Internet Explorer version 7 or later";
        switch ($.browser.version) {
            case "6.0":
            case "5.0":
                $("body").prepend("<div class='ui-state-error ui-corner-all' style='padding: 4pt 0.9em;height:40px;'>"
                        + messageBoxText + "</div>");
                break;
        }
    }
}