// JavaScript Document

// Must re-initialize window position
function cWindowShow(windowCall, winTitle, winWidth, winHeight){
	
	var Obj = document.getElementById('cWindow');
	if(!Obj){
		Obj    = document.createElement('div');
		
		var html  = '';
		html += '<div id="cWindow" return false;" style="top: 0px;display:none;">';
		html += '	<!-- top section -->';
    	html += '	<div id="cwin_tl"></div>';
    	html += '	<div id="cwin_tm"></div>';
    	html += '	<div id="cwin_tr"></div>';
    	html += '	<div style="clear: both;"></div>';
    	html += '	<!-- middle section -->';
    	html += '	<div id="cwin_ml"></div>';
    	html += '	<div id="cWindowContentOuter">';
    	html += '		<div id="cWindowContentTop">';
		html += '			<a href="javascript:void(0);" onclick="cWindowHide();" id="cwin_close_btn">Close</a>';
		html += '			<div id="cwin_logo">'+winTitle+'</div>';
    	html += '		</div>';			
    	html += '		<div id="cWindowContent" style="position:relative" class="">';
    	html += '		</div>';		
    	html += '	</div>';
    	html += '	<div id="cwin_mr"></div>';
    	html += '	<div style="clear: both;"></div>';
    	html += '	<!-- bottom section -->';
    	html += '	<div id="cwin_bl"></div>';
    	html += '	<div id="cwin_bm"></div>';
    	html += '	<div id="cwin_br"></div>';
    	html += '	<div style="clear: both;"></div>';
		html += '</div>';
		
		
		Obj.innerHTML = html;
		document.body.appendChild(Obj);
	} else {
		jQuery('#cwin_logo').html(winTitle);
	}
	
	// Set cWindowWidth + 40
	jQuery('#cWindow').width(winWidth + 40);
	jQuery('#cWindowContentOuter').width(winWidth);
	jQuery('#cWindowContentTop').width(winWidth);
	jQuery('#cWindowContent').width(winWidth);
	jQuery('#cwin_bm').width(winWidth);
	jQuery('#cwin_tm').width(winWidth);
	
	
	var myWidth = 0, myHeight = 0;
	
	myWidth  = jQuery(window).width();
	myHeight = jQuery(window).height();
	
	var yPos;
	yPos= jQuery(window).height()- 30  ;
	var leftPos = (myWidth - winWidth)/2;
	
	jQuery('#cWindow').css('zIndex', cGetZIndexMax() + 1);
    jQuery('#cWindowContent').html('<div class="ajax-wait">&nbsp;</div>');
    
    jax.loadingFunction = function(){
		jQuery('#cWindowContent').addClass('winloading');
	}

	jax.doneLoadingFunction = function(){
		jQuery('#cWindowContent').removeClass('winloading');
	};
	
    eval(windowCall);

	
	/*
	Set editor position, center it in screen regardless of the scroll position
	*/
	jQuery("#cWindow").css('marginTop', (jQuery(document).scrollTop() + 10 +(yPos - winHeight)/2) + (20)+'px');

	
	
	/*
    Set height and width for transparent window
	*/
    jQuery('#cWindow').css('height', winHeight);
    jQuery('#cWindow').css('left', leftPos);
	jQuery('#cWindowContent').css('height', (winHeight - 50)); // - 30px title and 20px border
	jQuery('#cWindowContent').css('width', (winWidth - 20)); // -20px border
	jQuery('#cWindowContentOuter').css('height', winHeight);
	jQuery('#cwin_ml').css('height', winHeight);
	jQuery('#cwin_mr').css('height', winHeight);    
	
	jQuery('#cWindow').fadeIn();
	
}

function cWindowHide(){
	if(jQuery('#cWindowAction').get().length > 0)
		jQuery('#cWindowAction').animate({ bottom: "-40px"}, 200 , '', function(){
			jQuery('#cWindow').fadeOut('fast', function(){ jQuery(this).remove();});
		});
	else
		jQuery('#cWindow').fadeOut('fast', function(){ jQuery(this).remove();});
}

function cWindowActions(action){
	var html = '<div id="cWindowAction" style="border-top: 1px solid #FFFFFF; background: #CCCCCC; position: absolute; bottom: -40px; width: 100%; height: 32px; left: 0px;">'; 
	html += '<table style="height:32px;padding-right:4px;width:100%;"><tr>';
	html += '<td align="left"><div id="cwin-wait">&nbsp;</div></td><td  align="right" valign="middle">';
	html += action;
	html += '</td></tr></table></div>';
	
	jax.loadingFunction = function(){
		jQuery('#cwin-wait').show();
		jQuery('#cWindowContent input').attr('disabled', true);
		jQuery('#cWindowContent textarea').attr('disabled', true);
		jQuery('#cWindowContent button').attr('disabled', true);
	}

	jax.doneLoadingFunction = function(){
		jQuery('#cwin-wait').hide();
		jQuery('#cWindowContent input').attr('disabled', false);
		jQuery('#cWindowContent textarea').attr('disabled', false);
		jQuery('#cWindowContent button').attr('disabled', false);
	};
	
	jQuery('#cWindowContent').append(html);
	jQuery('#cWindowAction').animate({ 
        bottom: "0px"
      }, 200 );
}

function cWindowResize(newheight){
	jQuery('#cwin_mr').animate({ 
        height: newheight +"px"
      }, 200 );
    
    jQuery('#cwin_ml').animate({ 
        height: newheight +"px"
      }, 200 );
    
    jQuery('#cWindowContentOuter').animate({ 
        height: newheight +"px"
      }, 200 );
      
    jQuery('#cWindowContent').animate({ 
        height: newheight - 50 +"px"
      }, 200 );
}


function cGetZIndexMax(){
	var allElems = document.getElementsByTagName?
	document.getElementsByTagName("*"):
	document.all; // or test for that too
	var maxZIndex = 0;

	for(var i=0;i<allElems.length;i++) {
		var elem = allElems[i];
		var cStyle = null;
		if (elem.currentStyle) {cStyle = elem.currentStyle;}
		else if (document.defaultView && document.defaultView.getComputedStyle) {
			cStyle = document.defaultView.getComputedStyle(elem,"");
		}

		var sNum;
		if (cStyle) {
			sNum = Number(cStyle.zIndex);
		} else {
			sNum = Number(elem.style.zIndex);
		}
		if (!isNaN(sNum)) {
			maxZIndex = Math.max(maxZIndex,sNum);
		}
	}
	return maxZIndex;
}

