 /**
 * PopUp Overlay - jQuery plugin
  * version: 1.2.1 (200/09/25)
 * @requires jQuery v1.2.2 or later and jQuery.dimensions plugin
 * @author Ivan Lazarevic
 
 * @param background - background, yes/no
 * @param color - background color, for background:yes
 * @param opacity - background opacity, for background:yes
 * @param position - position of overlay div, center/fixed/offset
 * @param offsetTop - for offset positioning, for position: offset
 * @param offsetLeft - for offset positioning, for position: offset 
 * @param zAuto - auto count z-Index, yes/no
 * @param select - show/hide select fields for ie6
 * @param action - on click or mouseover, could not be used with background
 * @param onopen - name of function which execute on popup open
 * @param onclose - name of function which execute on popup close
 * @example  $("#popup").popup( { background:'no', color: 'green', opacity: '0.2', position: 'center', offsetTop: 10, offsetLeft: 20, zAuto: 'yes', select: 'show' , onopen: 'openme', onclose: 'closeme' } );
 
**/
(function($) {
	var opts = new Array;
	var level = new Array;
	var lastClicked = new Array;
	
	$.fn.popUp = $.fn.popup = function(options){
	
	init = function(el){

		lastClicked[el.id] = false;
		level[el.id] = 0;
		opts[el.id] = $.extend({}, $.fn.popup.defaults, options);

		if (opts[el.id].zAuto=='yes'){
			$('body').children().each(function(){
        		var l = parseInt($(this).css('z-index'));
				if (l>level[el.id]) level[el.id] = l;
			});

		}

		$(el).css({position: "absolute", display: "none"});
		$("body").prepend(el);

		if (opts[el.id].background=='yes'){
			$("body").prepend("<div id='"+el.id+"Back' style='display: block'></div>");
			$("#"+el.id+"Back").css({ backgroundColor:opts[el.id].color, opacity:opts[el.id].opacity, width: "100%", height: $(document).height(), position: "absolute", top: "0px", left: "0px", display: "none"});
			if (level[el.id]>0){
				$("#"+el.id+"Back").css({zIndex: level[el.id]+1 });
			}
		}

		if (opts[el.id].position=='center'){ $(el).css({left: "50%", marginLeft: -$(el).width()/2}); }
		if (level[el.id]>0){ $(el).css({zIndex: level[el.id]+2});}
		
		lnkOpen = "."+el.id+"Open";
			
		$(lnkOpen).each( function(i){ this.id = this.className + i })	
		
		if (opts[el.id].action=='click'){
			$(lnkOpen).click(function(){ doPopUp(el,this.id); });
			$("."+el.id+"Close").click(function(){ hidePopUp(el); });
		} else if (opts[el.id].action=='mouseenter') {
			$(lnkOpen).mouseenter(function(){ doPopUp(el,this.id); });	
			$(lnkOpen).mouseleave(function(){ hidePopUp(el,this.id); });			
		} else {
			$(lnkOpen).mouseover(function(){ doPopUp(el,this.id); });	
			$(lnkOpen).mouseout(function(){ hidePopUp(el,this.id); });
		}
		
	}
	
	
	doPopUp = function(el,clickPlace){

		lastClicked[el.id] = clickPlace;
		$(el).css('visibility','hidden');
        $(el).show(function(){
			if (opts[el.id].position=='center'){
				pu_top = $(window).scrollTop()+($(window).height()-$(el).height())/2;
				if (pu_top<0) pu_top=0;
				$(el).css('top',pu_top);
			}
			if (opts[el.id].position=='offset'){
				lnkOpenPositon = $("#"+clickPlace).offset();
				$(el).css('top',lnkOpenPositon.top+opts[el.id].offsetTop);
				$(el).css('left',lnkOpenPositon.left+opts[el.id].offsetLeft);
			}
	
			/* hide select if browser is ie6 */
			if (opts[el.id].select=='hide'){
				jQuery.each(jQuery.browser, function(i, val) {
					if(i=="msie" && jQuery.browser.version.substr(0,1)=="6"){
						$("select").css('visibility','hidden')
					}
				});
			}
			$(el).css('visibility','');
        });
			
		
		if (opts[el.id].background=='yes'){
			$("#"+el.id+"Back").css({ height: $(document).height()});
			$("#"+el.id+"Back").show();
		}
		
		if (opts[el.id].onopen!='') onOpenClose(opts[el.id].onopen,clickPlace);
		
	};
	
	hidePopUp = function(el,bb){
		if (opts[el.id].background=='yes'){
			$("#"+el.id+"Back").hide();
		}
		if (opts[el.id].select=='hide'){
			$("select").css('visibility','visible');
		}
		$(el).hide();
		if (opts[el.id].onclose!='') onOpenClose(opts[el.id].onclose,lastClicked[el.id]);
	}
	
	onOpenClose = function(funcName,clickPlace){
		if (typeof funcName == 'string' && eval('typeof ' + funcName) == 'function') {
			eval(funcName + "('" + clickPlace + "')"); 
		}		
	}
	
	
	this.each (
		function()
		{ init(this); }
	)
	
};

	$.fn.popup.defaults = {	
		background: 'yes',
		color: 'black',
		opacity: '0.5',
		position: 'center',
		offsetTop: 5,
		offsetLeft: 5,
		zAuto: 'no',
		select: 'show',
		action: 'click',
		onopen: '',
		onclose: ''
	};
	

})(jQuery);


// make sure the $ is pointing to JQuery and not some other library
(function($){
    // add a new method to JQuery

    $.fn.equalHeight = function() {
       // find the tallest height in the collection
       // that was passed in (.column)
        tallest = 0;
        this.each(function(){
          //if (window.console) console.log($(this).height());
            thisHeight = ($(this).height() + 34 );
            if( thisHeight > tallest)
                tallest = thisHeight;
        });

        // set each items height to use the tallest value found
        this.each(function(){
            $(this).height(tallest);
        });
    }
})(jQuery);


