/**
 * Created by JetBrains PhpStorm.
 * User: kkrivlenia
 * Date: 8/26/11
 * Time: 6:28 PM
 * To change this template use File | Settings | File Templates.
 */
(function($){
    var loadingTimer, loadingFrame = 1,
    _animate_loading = function() {
			if (!loading.is(':visible')){
				clearInterval(loadingTimer);
				return;
			}

			$('div', loading).css('top', (loadingFrame * -40) + 'px');

			loadingFrame = (loadingFrame + 1) % 12;
		};//,options = $.extend({}, $.fn.popup.defaults);

    
$.popup = function(data){


    wrap.children().remove();
    wrap.append(data);
    $.popup.center();

    mask.click(function () {
			$.popup.close();
    		return false;
      });
	$(document).keyup(function (e) {
		if(e.keyCode == 27){
				$.popup.close();
		}
	});
     }

    $.popup.center = function(){
        var winH = $(window).height();
        var winW = $(window).width();
        var pTop = Math.max(10+$(window).scrollTop(),winH/2 - 527/2);
         $(wrap).css({
            'width': 450,
            'top': pTop,
            'left': winW/2 - 450/2
            }).fadeIn(200);
    }

    $.popup.showMask =function(){
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
      mask.css({
      'width':'100%',
      'height':maskHeight
     });
     mask.fadeTo(50,0.7);
    }


	$.popup.close = function(){
			if(!(/\/login\//.test(location.href))) {
				 wrap.hide();
           		 mask.fadeOut('fast');
           		 loading.hide();
			} else {
				if((document.referrer.indexOf(location.hostname)) > -1 && document.referrer != '' && document.referrer != location.href) {
					location.href = document.referrer;
				} else {
					//fixed  bug IE
					var url = decodeURIComponent(location.search);
					url = decodeURIComponent(url);
					if(/category/ig.test(url)) {
						var urlWithDomain = url.match(/category=(.+[^\&])$/i);
						if(urlWithDomain.length > 1) {
							var urlWithoutDomain = urlWithDomain[1].replace(/\&domain=.*/ig,'');
							if(/make=.+/ig.test(urlWithoutDomain)) {
								location.href = decodeURIComponent(urlWithoutDomain.replace(/used\/\&make=.+/ig,''));
							} else {
								location.href = '/'+decodeURIComponent(urlWithoutDomain);
							}
						} else {
							location.href = '/';
						}
					}	else if(/addAdvert/.test(location.href)) {
						location.href = '/addAdvert/';
					} else {
						location.href = '/';
					}
				}

			}

	}


    $.popup.init = function(){
        if ($("#popup-wrap").length) {
			return;
		}
        $('body').append(
            tmp	= $('<div id="popup-tmp"></div>'),
			loading	= $('<div id="popup-loading"><div></div></div>'),
			mask	= $('<div id="popup-mask"></div>'),
			wrap = $('<div id="popup-wrap"></div>')
		);
		
    }




    $.popup.showActivity = function(wrapDiv) {
		//wrapDiv.append($('#popup-loading'));
		if(wrapDiv !== undefined)
		{
			var position =  wrapDiv.offset();
			var wrapDivH = +position.top + wrapDiv.height()/2;
            var wrapDivW = +position.left + wrapDiv.width()/2;

			$('#popup-loading').css({
            'position': 'absolute',
            'top': wrapDivH,
            'left': wrapDivW
            });
		}
		clearInterval(loadingTimer);
		loading.show();
		loadingTimer = setInterval(_animate_loading, 66);
	};

    $.popup.hideActivity = function() {
		$('#popup-loading').removeAttr('style');
		loading.hide();
	};

    $.fn.popup = function(options) {
        return this;
    }



  $.fn.popup.defaults = {
  htmlCode: '',
  clickMask: false,//
  maskOpacity: 0.2,//
  width: 434, // popup width
  className: '#popup-wrap'
  };


    $(document).ready(function(){
        $.popup.init()
    })


})(jQuery);

