﻿//Set jQuery plugins globals
jQuery.watermark.options.className = 'watermark';

jQuery.fn.cmsTooltip = function (contentKey, width) {
    var self = this;
    var tooltip = jQuery(document.createElement('span')).appendTo(self);
    tooltip.addClass('map_tooltip');
    tooltip.css({ display: "none" });
    if (width) {
        tooltip.css({ width: width });
    }
    // This calculates position on initialize. 
    // It would be better to move it into hover, but there was no success on that
    if (this.position().left + tooltip.width() > jQuery("body").width()) {
        var newLeft = jQuery("body").width() - tooltip.width() - 30;
        tooltip.css({ left: newLeft });
    }
    var loaded = false;
    this.hover(
        function () {
            if (loaded) {
                tooltip.css({ display: "block" });
            } else {
                $.ajax({
                    url: 'TooltipHandler.ashx?q=' + contentKey,
                    dataType: "html",
                    success: function (data) {
                        tooltip.html(data);
                        loaded = true;
                        tooltip.css({ display: "block" });
                    }
                });
            }
        },
	    function () {
	        setTimeout(function () {
	            tooltip.css({ display: "none" });
	        }, 1500);
	    }
    );

    return this;
}

jQuery.fn.preventDoubleSubmit = function () {
    var alreadySubmitted = false;
    return jQuery(this).submit(function () {
        if (typeof(Page_IsValid ) != "undefined") {
            if (!Page_IsValid) {
                return;
            }
        }

        if (alreadySubmitted)
            return false;
        else
            alreadySubmitted = true;
    });
};

function initSmallGoogleMap(gmap) {
    gmap.enableScrollWheelZoom();
    gmap.enableDoubleClickZoom();

    var uiOptions = new GMapUIOptions(new GSize(100, 100));

    uiOptions.maptypes.satellite = false;
    uiOptions.maptypes.hybrid = false;
    uiOptions.maptypes.physical = false;
    uiOptions.controls.maptypecontrol = false;
    uiOptions.controls.menumaptypecontrol = false;

    gmap.setUI(uiOptions);

};

function initDefaultGoogleMap(gmap) {
    gmap.enableScrollWheelZoom();
    gmap.enableDoubleClickZoom();
    gmap.setUIToDefault();
    //gmap.addMapType(G_SATELLITE_MAP);
    //gmap.addControl(new GLargeMapControl3D());
    //gmap.addControl(new GMapTypeControl());
};

jQuery.extend(server, {

    LoadSavedOffers: function (e, trigger) {
        zajaxm.call({
            async: false,
            assembly: "CentrSource.Consumer.WebApp",
            methodClass: "consumer_library_master",
            methodName: 'GetSavedOffers',
            args: null,
            onError: function () {
                //alert("Error while trying to perform request.");
            },
            onOk: function (response) {
                $(e.target).find('.menu_content').html(response);
            }
        });
    },

    LoadLastViewedOffers: function (e, trigger) {
        zajaxm.call({
            async: false,
            assembly: "CentrSource.Consumer.WebApp",
            methodClass: "consumer_library_master",
            methodName: 'GetLastViewedOffers',
            args: null,
            onError: function () {
                //alert("Error while trying to perform request.");
            },
            onOk: function (response) {
                $(e.target).find('.menu_content').html(response);
            }
        });
    }

});

//function positionCsToolbar() {
//    var toolbar = jQuery("#centrsource_toolbar");
//    var win_y = jQuery(window).height();
//    var scroll_y = jQuery(window).scrollTop();
//    var cs_toolbar_y = toolbar.height();

//    toolbar.css({ 'top': ((win_y - cs_toolbar_y) + scroll_y) + "px", 'position': 'absolute' });
//}

var menuTimer;
jQuery(document).ready(function () {
    // ipad/iphone doesn't support css position fixed and has some issues with positioning on the bottom of the page,
    // so we disable CS toolbar because of that
    if ((/iphone|ipad/gi).test(navigator.appVersion)) {
        jQuery("#centrsource_toolbar").hide();
        //        positionCsToolbar();
        //        jQuery(window).scroll(positionCsToolbar);
    }

    jQuery("form").preventDoubleSubmit();

    jQuery(".centrsource_toolbar a.trigger").click(function () {
        var next = jQuery(this).next();
        if (!this.hasContent) {
            jQuery(next).addClass('loading');
            next.trigger('loadContent');
            this.hasContent = true;
            jQuery(next).removeClass('loading');
        }

        jQuery(".centrsource_toolbar .menu_slider").each(function (a) {
            if (this != next[0]) {
                jQuery(this).slideUp();
            }
        });

        jQuery(next).slideToggle('fast', function () {
            jQuery(this).prev().toggleClass('selected');
        });
    });

    // Categories dropdown menu
    jQuery('#dropCategory').hover(
                            function () {
                                clearTimeout(menuTimer);
                                jQuery('#categoryPanel').slideDown('fast').addClass('hovered');
                            },
                            function () {
                                menuTimer = setTimeout(function () {
                                    hideMenuPanel();
                                }, 500);
                            }
                        );

    jQuery('#categoryPanel').hover(
                            function () { clearTimeout(menuTimer); },
                            function () { menuTimer = setTimeout(function () { hideMenuPanel(); }, 500); }
                            );

    jQuery('.main_menu li:not(.dropdown)').hover(
                            function () { hideMenuPanel(); }
                        );

    jQuery(".custom_dropdown").click(function () {
        jQuery(".user_dropdown").slideToggle("fast");
        jQuery("span.arrow").toggleClass("active");
    });
});
function hideMenuPanel() {
    jQuery('#categoryPanel').slideUp('fast');
};


