/////////////////////////////////////////////////////////////////////////////////////////////////
// jPopup
// Popup windows library
// Copyright by Andrew Skoolkin (c) 2008, askulkin@pantera.kz
/////////////////////////////////////////////////////////////////////////////////////////////////

var jPopup = {
	popup_id: 'jpopup',
	fader_id: '',
	initialized: false,
	width: 0,
	height: 0,
	top: 0,
	left: 0,
	content: '',
	content_padding: 0,
	content_width: 0,
	content_height: 0,
	header_height: 25,
	footer_height: 0,
	
	animation: 'fade_out', // slide_up | fade_out
	allow_close: true,
	
	overlay: { enabled: true, effect: 'fade', speed: 'normal', hide_select: true, cursor: 'auto' },
	labels: { close: 'Close' },
	
	// Create overlay DIV
	create_overlay: function() {
		height = document.body.scrollHeight + 'px';
		this.fader_id = this.popup_id + '_fader';
		
		overlay = jQuery('#' + this.fader_id);
		if (overlay.length == 0) {
			jQuery('<div id="' + this.fader_id + '"></div>').appendTo("body");
			overlay = jQuery('#' + this.fader_id);
		}
		
		jQuery(overlay).css({ display: 'none', background: 'black', height: height, width: '100%', top: '0px', left: '0px', position: 'absolute', zIndex: '10000' });
		
		if (jQuery.browser.msie) {
			jQuery(overlay).css('opacity', 50);
			jQuery(overlay).css('filter', 'alpha(opacity=50)');
		} else {
			jQuery(overlay).css('opacity', '0.4');
			jQuery(overlay).css('-moz-opacity', '0.4');
		}
	},
	
	//
	get_template: function() {
		//template = '<div id="popup_header"><img src="/includes/img/jpopup-close.gif" alt="' + this.labels.close + '" style="cursor: pointer; float: right;" onclick="jPopup.hide()"></div><div id="popup_content"></div><div id="popup_footer"></div>';
	},
	
	// Create popup header
	create_title: function(title) {
		this.content = '<div id="header">' + title + '<img src="/includes/img/jpopup-close.gif" alt="' + this.labels.close + '" style="cursor: pointer; float: right;" onclick="jPopup.hide()"></div>';
	},
	
	// Create popup window
	create: function() {
		jQuery('<div id="' + this.popup_id + '" class="jpopup"><div id="body">' + this.content + '</div></div>').appendTo("body");
		//jQuery('#' + this.popup_id).click( function() { jPopup.hide(); } );
	},
	
	// Show popup window
	show: function() {
		jQuery('#' + this.popup_id).css({ width: jPopup.width, top: jPopup.top, left: jPopup.left, position: 'absolute', zIndex: '15000' });
		jQuery('#' + this.popup_id).show();
	},
	
	// Hide popup window and overlay
	hide:  function() {
		if (this.allow_close) {
			switch (this.animation) {
				case 'slide_up':
					jQuery('#' + this.popup_id).fadeIn(0).animate( { top: (jPopup.top + 50) + 'px' }, 300).animate({top: '-' + jPopup.height + 'px' }, 500, function() { 
						jQuery('#' + jPopup.popup_id).remove();
						jPopup.fade_out();
					});
				break;
				case 'fade_out':
					jQuery('#' + this.popup_id).fadeOut("fast", function() { 
						jQuery('#' + jPopup.popup_id).remove();
						jPopup.hide_overlay();
					});
				break;
			}
		}
	},
	
	// Resize popup window
	resize: function(content_width, content_height, show_footer) {
		this.content_width = content_width;
		this.content_height = content_height;
		
		if (this.content_padding > 0) {
			this.width = this.content_width + (this.content_padding * 2) + 10;
			this.height = this.content_height + (this.content_padding * 2) + this.header_height;
		} else {
			this.width = this.content_width;
			this.height = this.content_height;
		}
	
		if (show_footer) {
			this.height+= this.footer_height;
		}
		
		//size = { clientHeight: document.body.clientHeight, offsetHeight: document.body.offsetHeight, bodyHeight: jQuery("body").height(), documentScrollHeight: document.body.scrollHeight, documentBodyHeight: jQuery(document.body).height() };
		
		if (jQuery.browser.msie) {
			this.top = parseInt((document.documentElement.clientHeight / 2) - (this.height / 2)) + document.documentElement.scrollTop;	//document.body.scrollTop
		} else {
			this.top = parseInt((innerHeight / 2) - (this.height / 2)) + pageYOffset;
		}
		
		this.left = parseInt((jQuery("body").width() / 2) - (this.width / 2));
	},
	
	// Show overlay
	show_overlay: function() {
		if (this.overlay.enabled) {
			if (this.overlay.hide_select) jQuery('select').css('visibility', 'hidden');
			
			if (this.fader_id == '') this.create_overlay();
			
			jQuery("#" + this.fader_id).css("height", jQuery(document).height() + 'px'); //document.body.scrollHeight
			
			if (this.overlay.effect == 'fade') {
				jQuery("#" + this.fader_id).fadeIn(this.overlay.speed);
			} else {
				jQuery("#" + this.fader_id).css("display", "");
			}
			
			jQuery("body").css("cursor", this.overlay.cursor);
		}
		
		return false;
	},
	
	// Hide overlay
	hide_overlay: function() {
		if (this.overlay.enabled) {
			if (this.overlay.hide_select) jQuery('select').css('visibility', 'visible');
			
			if (this.overlay.effect == 'fade') {
				jQuery("#" + this.fader_id).fadeOut(this.overlay.speed);
			} else {
				//jQuery("#" + this.fader_id).css("display", "none");
				jQuery("#" + this.fader_id).hide();
			}
		
			//jQuery("#" + this.fader_id).css("display", "none");
			jQuery("body").css("cursor", "auto");
		}
	},
	
	// Show popup image
	show_image: function(settings) {
		if (settings.src == undefined || settings.src == '') return false;
		
		this.content = '';
		this.allow_close = (settings.allow_close == undefined) ? true : settings.allow_close;
		if (settings.animation != undefined) this.animation = settings.animation;
		title = (settings.title == undefined) ? '' : settings.title;
		
		this.resize(settings.width, settings.height, true);
		this.show_overlay();
		
		this.create_title(title);
		
		this.content+= '<div style="background: url(/includes/img/indicator.gif) no-repeat; background-position: 50% 50%;"><img src="' + settings.src + '" width="' + this.content_width + '" height="' + this.content_height + '" alt="' + title + '" id="photo"></div>';
		if (settings.text != undefined) this.content+= '<div id="text">' + settings.text + '</div>';
		
		this.create();
		this.show();
		
		return false;
	},
	
	show_alert: function(title, text, width, height, animation, allow_close) {
		this.content = '';
		
		this.allow_close = (allow_close == undefined) ? true : allow_close;
		
		this.resize(width, height, false);
		this.show_overlay();
		
		this.create_title(title);
		
		this.content+= '<div id="alert_text">' + text + '</div>';
		if (this.allow_close) this.content+= '<div align="center"><div id="alert_button" onclick="jPopup.hide()">' + this.labels.close + ' </div></div>';
		
		if (animation != undefined) this.animation = animation;
		
		this.create();
		this.show();
		
		return false;
	},
	
	show_mp3player: function(src, options) {
		
		options.width = 500;
		options.height = 150;
		
		options.player_width = 470;
		options.player_height = 70;
		
		player = '<object type="application/x-shockwave-flash" data="includes/mp3player.swf" height="' + options.player_height + '" width="' + options.player_width + '"><param name="wmode" VALUE="transparent" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="movie" value="includes/mp3player.swf" /><param name="FlashVars" value="way=' + src + '&amp;swf=includes/mp3player.swf&amp;w=' + options.player_width + '&amp;h=' + options.player_height + '&amp;time_seconds=0&amp;autoplay=0&amp;q=1&amp;skin=orange&amp;volume=70&amp;comment=" /></object>';
		
		this.content = '<div class="jpopup-header"><div>' + options.title + '</div><img src="includes/jpopup-close.gif" alt="' + this.labels.close + '" onclick="jPopup.hide()"></div><div class="jpopup-content">' + player + '</div>';
		
		this.resize(options.width, options.height, false);
		this.show_overlay();
		
		this.create();
		this.show();
		
		return false;
	},
	
	show_dialog: function(options) {
		// title, html, width, height, animation, allow_close
		
		this.content = '';
		
		this.allow_close = (options.allow_close == undefined) ? true : options.allow_close;
		if (options.animation != undefined) this.animation = animation;
		
		this.resize(options.width, options.height, false);
		this.show_overlay();
		
		if (options.title != undefined) {
			this.create_title(options.title);
		}
		
		if (options.content != undefined) {
			this.content+= options.content;
			this.create();
			this.show();
		} else if (options.ajax != undefined) {
			
			//this.content+= options.ajax;
			
			this.create();
			//alert(options.ajax);
			
			jQuery.get(options.ajax, function(data) {
			  jQuery('#body').html(data);
			  jPopup.show();
			  //alert(data);
			});
			
		}
		
		return false;
	}
}
