var OverLay = 
    {
    init: function(){
        var keywrd = $('.mn_kywrd').text();
        $(".mn_olayHover").hover(function () {
		
            $(this).append('<span class="mn_olayImgHover"></span>');
            var merch = $(this).get(0).firstChild.className;
            var getImg = $(this).get(0).firstChild.firstChild;
        
            if ($(getImg).length && getImg.nodeType == 1){
                var imgPos = $(getImg).position();
                var imgX = $(getImg).width() / 2 + imgPos.left;
                $('.mn_olayImgHover').css("left", imgX);
            }
            else {
                var elPos = $(this).offset();
                var elWidth = $(this).width();
                var getPos = (elWidth / 2) + elPos.left
            }
            $(".mn_loadAjax").ajaxStart(function() {
                $(this).show();
            }).ajaxStop(function() {
                $(this).hide();
            });
            $('.mn_olayImgHover').click(function() {	
                OverLay.callJax(merch, keywrd);
            });
        },                
        function () { // hover off
            $(this).find("span:last").remove();
		  
        });
      
        // if hover isn't used
        $('.mn_activeCoups').click(function(){
            var classN = this.className.split(' ');
            var merchID = classN[1];
            var origin = classN[0];
            OverLay.callJax(merchID, keywrd, origin);
            return false;
        });
      
    },
    
    callJax: function(id, keywrd, clickStart){
        $.ajax({
            type: 'GET',
            url: '/mo__' + id + '__.htm?isajax=1&keywords='+keywrd,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $("#mn_olayResults").html('There was a problem with your request, please try again.').show().vCenter();
            },
            success: function(response) {
                $("#mn_olayResults").html(response).show().vCenter();
                OverLay.overLay(clickStart); 
            }
        });
        
        
    },
      
    overLay: function(clickOrigin){
        var offers = $('#mn_olayTabOffers');
        var details = $('#mn_olayTabDetails');
        var offersF = $(".mn_olayOffersFrame");
        var detailsF = $(".mn_olayDetailsFrame");
	
        if (clickOrigin == "mn_activeCoups"){
            $(details).removeClass("mn_olayTabActive");
            $(offers).addClass("mn_olayTabActive");
            $(detailsF).hide();
            $(offersF).css('display','block');
        }
        $(offers).click(function(){
            $(".mn_olayTabActive").removeClass("mn_olayTabActive");
            $(this).addClass("mn_olayTabActive");
            $(detailsF).hide();
            $(offersF).css('display','block');
            return false;
        })
			
        $(details).click(function(){
            $(".mn_olayTabActive").removeClass("mn_olayTabActive");
            $(this).addClass("mn_olayTabActive");
            $(offersF).hide();
            $(detailsF).show();
            return false;
        })
		  
        $(".mn_xClose").click(function() {
            $(".mn_olayWrapper").remove();
            return false;
        })
    }
};


//vertical position
(function($){
    $.fn.vCenter = function(options) {
        var pos = {
            sTop : function() {
                return window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||	document.body.scrollTop;
            },
            wHeight : function() { 
                return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
            }
        };
        return this.each(function(index) {
            if (index == 0) {
                var $this = $(this);
                var elHeight = $this.height();
                var elTop = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);
                $this.css({
                    position: 'absolute',
                    marginTop: '0',
                    top: elTop
                });


            }
        });
    };

})(mnjQuery); 
