/* popup */
jQuery.fn.myPopup = function(_options){
	// defaults options
	var _options = jQuery.extend({
		duration: 700,
		linkOpenName: '.more-button,.go-more',
		linkCloseName: 'a.close',
		onShow: null,
		divFader: 'fader'
	},_options);

	return this.each(function(){
		var _hold = jQuery(this);
		var _speed = _options.duration;
		var _IE = ((navigator.appName.indexOf('Microsoft Internet Explorer') != -1) && (parseInt(navigator.appVersion) < 9)) ? true : false;
		var links = _hold.find(_options.linkOpenName);
		var _fader = jQuery('<div class="'+_options.divFader+'"></div>');
		var popup;
		jQuery('#wrap').append(_fader);
		_fader.css({
			position: 'absolute',
			top: '0px',
			left: '0px',
			zIndex: 998,
			background: '#999999',
			opacity: 0.7
		});
		function init(_obj){
			popup = jQuery("div."+_obj);
			var btnClose = popup.find(_options.linkCloseName);

			if (_IE){
				jQuery('select').css({visibility: 'hidden'});
				popup.find('select').css({visibility: 'visible'});
			}
			var w = jQuery('body').width();
			var h = jQuery(window).height();
			var _offset = jQuery(document).scrollTop();
            if(_obj == '#phones')
                var ret = 100;
            else
       			var ret = _offset+(h/2) - popup.outerHeight(true)/2 - 200;
			if (ret < 0) ret = 0;
			popup.css({
				top: ret,
				left: w/2 - popup.outerWidth(true)/2
			}).hide();
			_fader.css({
				width: w,
				height: jQuery('body').height()
			}).fadeIn(300, function(){
				popup.fadeIn(300,function(){
					this.link = links;
					if(jQuery.isFunction(_options.onShow)){
						_options.onShow.apply(this);
					}         
				});
			});
			jQuery(window).resize(function(){
				w = jQuery('body').width();
				h = jQuery(window).height();
				popup.css({
					left: w/2 - popup.outerWidth(true)/2
				});
				_fader.css({
					width: w,
					height: h
				});
			});
			popup.get(0).close = function(){
				popup.css({left: '-9999px'});
				_fader.hide();
				if (_IE) jQuery('select').css({visibility: 'visible'});
				jQuery(window).unbind('resize');
			}
			btnClose.click(function(){
				popup.get(0).close();
				return false;
			});

			_fader.click(function(){
				popup.css({left: '-9999px'});
				_fader.hide();
				if (_IE) jQuery('select').css({visibility: 'visible'});
				jQuery(window).unbind('resize');
				return false;
			});
		}
		links.bind('click',function(){
			if (jQuery(this).attr('href')){
				init(jQuery(this).attr('href'));
			}
			else{
				init(jQuery(this).attr('title'));
			}
			return false;
		});
	});
}

jQuery(document).ready(function(){jQuery('body').myPopup({
	});
});

