﻿$(document).ready(function () {
    $("#divMainDialog").dialog({
        autoOpen: false,
        title: "כותרת",
        modal: true,
        resizable: false,
        closeOnEscape: false,
        close: function (event, ui) {
            $(this).html("<div class='loadingImage'>");
        }
    });

    $("#divConfirmDialog").dialog({
        autoOpen: false,
        title: "אישור",
        modal: true,
        resizable: false,
        width: 400,
        height: 150
    });
    $("#btnDialogNo").click(function () {
        $("#divConfirmDialog").dialog("close");
    });

    // Log all jQuery AJAX requests to Google Analytics
    $(document).ajaxSend(function (event, xhr, settings) {
        if (typeof _gaq !== "undefined" && _gaq !== null) {
            _gaq.push(['_trackPageview', settings.url]);
        }
    });
});

function updateDialog(title, height, width) {
    //update the dialog settings
    var mainDialog = $("#divMainDialog");
    mainDialog.dialog("option", "title", title);
    mainDialog.dialog("option", "height", height);
    mainDialog.dialog("option", "width", width);
    mainDialog.dialog("option", "position", "center");

    //show ajax LoadingImage in the dialog
    mainDialog.html("<div class='loadingImage'>");

    mainDialog.dialog("open");

    return mainDialog;
}

function closeDialog() {
    var mainDialog = $("#divMainDialog");
    mainDialog.dialog("close");
}

function changeDialogSize(height, width) {
    var mainDialog = $("#divMainDialog");

    var windowHeight = $(window).height();
    var windowWidth = $(window).width();

    //if the selected size is bigger than the window - limit it to the window size
    if (height > windowHeight) {
        height = windowHeight - 20;
    }
    if (width > windowWidth) {
        width = windowWidth - 50;
    }

    mainDialog.dialog("option", "height", height);
    mainDialog.dialog("option", "width", width);
    mainDialog.dialog("option", "position", "center");
}

function showFullImageInDialog(src) {
    //open new dialog window
    var mainDialog = updateDialog("", 170, 500);

    var image = $("<img src='" + src + "' id='fullScreenImage' class='hidden' />");

    //when the image has been loaded - update the dialog size
    image.load(function () {
        var width = $(this).width();
        var height = $(this).height();

        var windowWidth = $(window).width();
        var windowHeight = $(window).height();

        //if image size is bigger than the window
        if (width + 50 > windowWidth) {
            width = windowWidth - 50;
            $(this).css("width", width + "px");
        }
        if (height + 90 > windowHeight) {
            height = windowHeight - 90;
            $(this).css("height", height + "px");
        }

        var dialogWidth = width + 50;
        var dialogHeight = height + 90;
        changeDialogSize(dialogHeight, dialogWidth);

        mainDialog.find("img").show().css("width", "100%");
        mainDialog.find(".loadingImage").hide();
    });

    mainDialog.append(image);
}

function showYoutubeInDialog(code) {
    //open new dialog window
    var mainDialog = updateDialog("", 170, 500);

    var youtubeFrame = $('<iframe width="640" height="360" src="http://www.youtube.com/embed/' + code + '?autoplay=1" frameborder="0" allowfullscreen></iframe>');

    changeDialogSize(410, 680);

    mainDialog.empty();
    mainDialog.append(youtubeFrame);
}

function openConfirmDialog(message, successFunction) {
    //open the "confirmDialog" and set relevant message to the user
    $("#divConfirmDialog").dialog("open");
    $("#divDialogMessage").html(message);
    $("#divDialogMessage").css("width", $("#divDialogMessage").width()).css("float", "none");

    //define the click event of the "Yes" button
    $("#btnDialogYes").unbind("click");
    $("#btnDialogYes").click(successFunction);
    $("#btnDialogYes").click(function () { $("#divConfirmDialog").dialog("close"); });
}

function fitIntoContainer() {
    var imageHeight = $(this).height();
    var imageWidth = $(this).width();

    var container = $(this).parents(".imageContainer");
    var containerWidth = container.width();
    var containerHeight = container.height();

    //if the container is Horizontal
    if (containerWidth > containerHeight) {
        $(this).css("width", containerWidth + "px");
    }
    //if the container is Vertical
    else if (containerHeight > containerWidth) {
        $(this).css("height", containerHeight + "px");
    }
    //if the container width and height are the same (eg. 200*200)
    else {
        if (imageWidth > imageHeight) {
            $(this).css("height", containerHeight + "px");
        }
        else {
            $(this).css("width", containerWidth + "px");
        }
    }

    //offset the image inside the container
    var imageNewHeight = $(this).height();
    var marginTop = (imageNewHeight - containerHeight) / 2;
    $(this).css("marginTop", "-" + marginTop + "px");

    //offset the image inside the container
    var imageNewWidth = $(this).width();
    var marginRight = (imageNewWidth - containerWidth) / 2;
    $(this).css("marginRight", "-" + marginRight + "px");

    $(this).show();
}

//Optional parameter includeMargin is used when calculating outer dimensions
(function ($) {
    $.fn.getHiddenDimensions = function (includeMargin) {
        var $item = this,
        props = { position: 'absolute', visibility: 'hidden', display: 'block' },
        dim = { width: 0, height: 0, innerWidth: 0, innerHeight: 0, outerWidth: 0, outerHeight: 0 },
        $hiddenParents = $item.parents().andSelf().not(':visible'),
        includeMargin = (includeMargin == null) ? false : includeMargin;

        var oldProps = [];
        $hiddenParents.each(function () {
            var old = {};

            for (var name in props) {
                old[name] = this.style[name];
                this.style[name] = props[name];
            }

            oldProps.push(old);
        });

        dim.width = $item.width();
        dim.outerWidth = $item.outerWidth(includeMargin);
        dim.innerWidth = $item.innerWidth();
        dim.height = $item.height();
        dim.innerHeight = $item.innerHeight();
        dim.outerHeight = $item.outerHeight(includeMargin);

        $hiddenParents.each(function (i) {
            var old = oldProps[i];
            for (var name in props) {
                this.style[name] = old[name];
            }
        });

        return dim;
    }
} (jQuery));

//This function get "form" element (or "input" elements) and serialize the values into JSON object
$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

// Return a helper with preserved width of cells
var fixHelper = function (e, ui) {
    ui.children().each(function () {
        $(this).width($(this).width());
    });
    return ui;
};

function getYoutubeCode(url) {
    //if the YouTube code is long (with "<object>..." or with "http:\\youtube.com...") - take only the code string
    if (url.indexOf('object') >= 0) {
        var start = url.indexOf('/v/') + 3;
        var length = url.indexOf('&') - (url.indexOf('/v/') + 3);
        url = url.substr(start, length);
    }
    else if (url.indexOf('youtube') >= 0) {
        if (url.indexOf('&') >= 0) {
            var start = url.indexOf('=') + 1;
            var length = url.indexOf('&') - (url.indexOf('=') + 1);
            url = url.substr(start, length);
        }
        else {
            url = url.substr(url.indexOf('=') + 1);
        }
    }

    return url;
}
