// JavaScript Document
var holderheight;
var holderwidth;
var hide;
var speed = 200;
var	holderheight = 444;
var	holderwidth = 558;
	
	

function createdisplay(holderwidth,holderheight,ajax,url,autohide){
	opacity = 0.6;

    if(!$chk(holderwidth)){
	    holderwidth=100;
    }
    if(!$chk(holderheight)){
	    holderheight=100;
    }
	    
    if(!$chk(speed)){
	    speed=400;
    }

    if(!$chk(autohide)){
	    autohide=false;
    }

    if(!$chk(ajax)){
	    ajax=0;
    }

    if(!$('dOverlay')){ // create the divs
	    overlay = new Element('div', {'id': 'dOverlay'}).injectInside(document.body);
        var winscroll = window.getScrollSize(); 
	    overlay.setStyles({'top': 0, 'height': winscroll.y, 'opacity':'0', 'width':winscroll.x});	
	    holder = new Element('div', {'id': 'dHolder'}).injectInside(document.body);
		current = setUpPostion(false);
		currentx = current.x().round();
		currenty = current.y().round();
		$('dHolder').setStyles({'top': currenty + "px", 'left': currentx + "px", 'opacity':'0', 'width':'0px', 'height':'0px'});
    }// end create the divs


	$('dOverlay').removeEvents();
    $('dOverlay').addEvent('click', function(e) { //finds 'id=dOverlay' and add an onclick event
   /// console.log('adding event');
	    e = new Event(e).stop();
	    if(window.styleActive == 0){	
		    if(this.style.opacity >= opacity){
			    clearTimeout(hide);
				
				current = setUpPostion(false);
											    
			    holderfx.start({
				    'height':0, 
				    'width':0,
				    'top':current.y,
				    'left':current.x,
				    'opacity':0
			    }).chain(function(){
				    var d = detectMacXFF(); //mac firefox fix
				    if (d) {
					    $('dOverlay').style.visibility = "hidden";
						$('dHolder').empty();
				    }else{
					    overlayfx.start({'opacity': [opacity, 0]});
						$('dHolder').empty();
				    }
			    });
		    }
	    }//end of if
    });
		
    var overlayfx = new Fx.Morph('dOverlay', {
		 duration:speed, 
		 wait:true, 
		 onStart: function(){	
			//alert('1');
			window.styleActive = 1;
			$('dOverlay').removeEvent('click');
			//, function(e)
			//{ e = new Event(e).stop(); }
		 }  
	 });

	
    var holderfx = new Fx.Morph('dHolder', {duration:speed, wait:true, onComplete: function(){ 
    	window.styleActive = 0; 
    	tempEl = $('dHolderContent');
    	tempEl.set('morph', {duration: speed});
    	tempEl.morph({opacity: 1});
    }} );
	//console.log('this one running');
	current = setUpPostion(true);
    overlayfx.start({'opacity': [0, opacity]}).chain(function(){
		    // executes immediately after completion of above effect
		    holderfx.start({
			    'height':holderheight, 
			    'width':holderwidth,
			    'top':current.y,
			    'left':current.x,
			    'opacity':1
		    });
    });

	el = 'dHolder';
    if(ajax > 0){
    	tempEl = new Element('div', {'id': 'dHolderContent'}).injectInside($(el));
    	tempEl.set('morph', {duration: speed});
    	tempEl.morph({opacity: 0});
	    $(el).addClass('loading');
	    loadajax(url,tempEl);
    }

    overlayfx.start({
		'margin-left': 5,
		'background-color': '#666',
		color: '#ff8'
    });
    if(autohide == true){
        hide=setTimeout('autohide()',4000);
    }
}//func

function loadajax(url,el){
	 
	new Request.HTML({             
        url:url,
		method: 'get',
		
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			var pageTracker = _gat._getTracker("UA-1454401-17");
            pageTracker._trackPageview(url);   
			$(el).removeClass('loading');
		},
        update: $(el)
	}).send();
}

function findshow(){
	$$('.show').removeEvents('click');
	$$('.show').addEvent('click', function(e) { //find all 'class=show' and add an onclick event
			e = new Event(e).stop();
			//holderheight = 400;
			//holderwidth = 515;
			//this.setStyle('opacity',0.9);
			createdisplay(holderwidth,holderheight,1,this.href);
		});
}

function resizeoverlay(){
if($('dOverlay')){ // create the divs
	var winscroll = window.getScrollSize(); 
	   $('dOverlay').setStyles({'height': winscroll.y, 'width':winscroll.x});
    if(window.styleActive == 0){  
	    var holderfx = new Fx.Morph('dHolder', {duration:speed, wait:true});
		current = setUpPostion(true);
		isitopened = $('dHolder').getStyle('width').toInt();
	    if(isitopened > 0){
	        holderfx.start({
			    'height': holderheight, 
			    'width': holderwidth,
			    'top':current.y,
			    'left':current.x
	        });
	    }
    }
}
}

function closedisplay(){
	//console.log('close called');
    if($('dOverlay').style.opacity >= opacity){
       // if(myShow !== undefined) myShow.destroy();
        var overlayfx = new Fx.Morph('dOverlay',{
	         duration:speed, 
	         wait:true, 
	         onStart: function(){	
			        //alert('2');
			        window.styleActive = 1;
			        $('dOverlay').removeEvent('click');
			        //, function(e)
			        //{ e = new Event(e).stop(); }
	         }  
        });
        
        var holderfx = new Fx.Morph('dHolder', {
			duration:speed, 
			wait:true, 
			onComplete: function(){ window.styleActive = 0; }
			});
			
	    if(window.styleActive == 0){
	    	
	    	var tempElfx = new Fx.Morph('dHolderContent', {duration: speed});
	    	 
	    	tempElfx.start({
	    	    'opacity': 0
	    	}).chain(function(){
		    	//console.log('morph done');
		    	
		        current = setUpPostion(false);								    
				holderfx.start({
					'height':0, 
					'width':0,
					'top':current.y,
					'left':current.x,
			        'opacity':0
		        }).chain(function(){
			        var d = detectMacXFF(); //mac firefox fix
			        if (d) {
				        $('dOverlay').style.visibility = "hidden";
						$('dHolder').empty();
			        }else{
				        overlayfx.start({'opacity':[opacity,0]});
						$('dHolder').empty();
			        }
		        });
	    	});// end chain
			
	    }//end of if
    }//if
}

function autohide(){
	console.log('autohide');
    if($('dOverlay').style.opacity >= opacity){
        var overlayfx = new Fx.Morph('dOverlay', {
	         duration:speed, 
	         wait:true,
	         onStart: function(){	
			        //alert('3');
			        window.styleActive = 1;
			        $('dOverlay').removeEvent('click');
			        //, function(e)
			        //{ e = new Event(e).stop(); }
	         }  
        });

        var holderfx = new Fx.Morph('dHolder', {duration:speed, wait:true, onComplete: function(){ window.styleActive = 0; }} );

	    var d = detectMacXFF();
	    if (d) {
		    var overlayfx = new Fx.Styles('dOverlay', {
			    duration:speed, 
			    wait:true,
			    onComplete:function(){
				    $('dOverlay').style.visibility = "hidden";
			    }
		    });
		    
		    overlayfx.start({'background-color':'#ffffff'});
		    $('dHolder').style.visibility = "hidden";
			    
	    }else{
			    holderfx.start({// hack for big active
			        'opacity':0
			    }).chain(function(){
			        overlayfx.start({'opacity': [opacity,0 ]});
			    });
        }
    }
}


function detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

function setUpPostion(withDisplay){
	currentScroll = window.getScroll(); //user scrolled to
	win = $(document.body).getSize(); //viewport
	
	if(withDisplay){
		newy = ((win.y-holderheight) / 2) + currentScroll.y;
		newx = ((win.x-holderwidth) / 2) + currentScroll.x;	
	}else{
		newy = ((win.y) / 2) + currentScroll.y;
		newx = ((win.x) / 2) + currentScroll.x;	
	}
	result = {
		y : function() {
			return newy;
		},
		x: function(){
			return newx;
		}
	};
	//console.log('newy',newy,'newx',newx);
	
	return result;
}

window.addEvent('domready', findshow);
window.onresize = resizeoverlay;
