/*
 * ShowMe (for jQuery)
 * version: 1.0 (12/03/2009)
 * @requires jQuery v1.2 or later
 *
  *
 * Licensed under the MIT:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2009 Byss.pl
 *
 */
 
 
(function($) {
  $.showme = function(data) {
		
		init();
		
		href = data.object.attr('href');
		id = data.object.attr('id');
		
		//alert(href);
		
		//if (data.ajax &&
		//   id.substring(0, 16) != "link-participate") {
		//	fillShowmeFromAjax(href, id);
		//} else if (data.ajax &&
		//   id.substring(0, 16) == "link-participate") {
		//   alert(id.substring(0, 16));
		//}
		//alert(id.substring(17));
		if (data.ajax) {
			fillShowmeFromAjax(href, id);
		}
		
		if (data.callback)
			$.showme.applyCallback(data.callback);
		
		
		if (data.image){
			fillShowmeFromImage(href, id);
		}	
		
		$.extend($.showme.settings, data);
		$.showme.settings.showmeHtml = '<div id="' + $.showme.settings.layer +'" style="display: none;"></div>';
		
		//alert($.showme.settings.showmeHtml);
		//alert(id.substring(0, 16));
  }

  /*
   * Public, $.showme methods
   */

  $.extend($.showme, {
    settings: {
			layer				: 'showme',
			opened			: false,
      width      	: 400,
			height		 	: 300,
			title				: 'Message',	
			padding			: 40,
			showmeHtml	: '\
			<div id="showme" style="display: none;"> \
			</div>'
    },
		
		applyCallback: function(callback){
			
				if (getObject('showme').dialog('isOpen')){
					callback.apply();
				}else{
					setTimeout(function() { $.showme.applyCallback(callback); }, 1000);				
				}	
		},
		
    loading: function() {
      init()
    },
		
		/*getWidth: function (obj){
			
			return obj.css('width');
			
		},
		
		getHeight: function (obj){
			
			return obj.css('height');
			
		},
		
		getIdAndTitle: function (data){
			return data.match(/id=["\'](.*)["\']\s*title=["\']([^\>]*)["\']/)
		},*/
		
		showDialog: function (element, width, title){
			
			element.dialog(
			{ 
				autoOpen: false,
				width: width,
				draggable: true, 
				title: title,
				bgiframe: false,
				dialogClass: 'showme-window',				
				resizable: false,
				hide: 'fast',
				zIndex: 6,
				close: function(){
					
					$.showme.settings.opened = false;
					
				},
				open: function (){
					
					$.showme.settings.opened = true;
					element.fadeIn(1500);
					
				},
				closeOnEscape: true 
			}
			);
			
			element.dialog('open');
			
			element.bind('dialogclose', function(){
				
				$.showme.settings.opened = false;
				jQuery(this).dialog('destroy');
				
			});
			
		},

    reveal: function(data) {
			
				_show = getObject($.showme.settings.layer);
				width = getPixelUnit($.showme.settings.width);
				title = $.showme.settings.title;
				
				_show.html(data.content);
				
				if (data.width)
					width = data.width;
				
				if (data.title)
					title = data.title;	
				
				if (data.error)
					jQuery.showDialogError(data.message, width);
				else
					$.showme.showDialog(_show, width, title);
					
			/*if (_error > -1){
				jQuery.showDialogError(data, $.showme.settings.width);
			}
			
			if (_success > -1){
				jQuery.showDialogSuccess(data, $.showme.settings.width);
			}*/
			
    },

    close: function() {
      $(document).trigger('close.showme')
      return false
    }
  })

  /*
   * Public, $.fn methods
   */

  $.fn.showme = function(settings) {
		
		init(settings)
		
    function clickHandler() {
			
			//$.showme.loading(true)
			
			if ($.showme.settings.opened)
					return false

			fillShowmeFromAjax(this.href, this.id)
	    
      return false
    }

    return this.click(clickHandler)
  }

  /*
   * Private methods
   */

  // called one time to setup showme on this page
  function init(settings) {
		
    if ($.showme.settings.inited) return true
    else $.showme.settings.inited = true
			
		if (settings) $.extend($.showme.settings, settings)
    $('body').append($.showme.settings.showmeHtml)			
			
  }
  
  function fillShowmeFromAjax(href, element) {
		
		$.ajax({
			url: href,
			cache: false,
			beforeSend: function(){ activateProgress(getObject(element)); },
			dataType: "json",
			success: function(data) {
			   if (data.title &&
			      data.title.substring(0, 11) == "Application") {
			      if (data.anid > 0) {
			         window.location = "http://" + document.domain + "/applications/edit/" + data.anid;
			      } else {
			         window.location = "http://" + document.domain + "/applications/apply/" + element.substring(17);
			      }
			      deactivateProgress();
			   } else {
			      $.showme.reveal(data);
			      deactivateProgress();
			   }
			}
		});
		
    //$.get(href, function(data) { $.showme.reveal(data, options) })
  }
	
  function fillShowmeFromImage(href, element) {
		
		activateProgress(getObject(element))
		
    var image = new Image()
    image.onload = function() {
      $.showme.reveal('<div id="window-image-preview" title="Preview Image"><img src="' + image.src + '" /></div>')
			deactivateProgress()
    }
		
    image.src = href
  }
	
})(jQuery);

jQuery(document).ready(function() {
 jQuery('a[rel*=showme]').showme(); 
});

