// JavaScript Document

// Must re-initialize window position
function cWindowShow(windowCall, winTitle, winWidth, winHeight, winType) {

	// if no window type defined, then we use dialog as default
	var _type = ( winType == '' || winType == null ) ? 'dialog' : winType;

	var Obj = document.getElementById('cWindow');
	if(!Obj){
		Obj    = document.createElement('div');
		
		var html  = '';
		html += '<div class="cWindowBackground"></div>';
		html += '<div id="cWindow" class="' + _type + '" 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">';
    	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, #cWindowContentTop, #cWindowContent').width(winWidth);
	jQuery('#cwin_bm, #cwin_tm').width(winWidth);
	
	
	var myWidth = 0, myHeight = 0;
	myWidth  = jQuery(window).width();
	myHeight = jQuery(window).height();
	
	var yPos = jQuery(window).height() - 30;
	
	var leftPos = (myWidth - winWidth)/2;
	
	jQuery('#cWindow').css('zindex', cGetZIndexMax() + 1);
    
    jax.loadingFunction = function(){
		jQuery('#cWindowContent').addClass('loading');
	}
	
	jax.doneLoadingFunction = function(){
		jQuery('#cWindowContent').removeClass('loading');
	};
	
    eval(windowCall);
	
	// Set editor position, center it in screen regardless of the scroll position
	jQuery("#cWindow").css('margin-top', (jQuery(document).scrollTop() + 10 +(yPos - winHeight)/2) + (20)+'px');
	
	// Set height and width for transparent window
	jQuery('#cWindow').css('height', winHeight)
	                  .css('left', leftPos);
	jQuery('#cWindowContent').css('height', (winHeight - 50)) // - 30px title and 20px border
	                         .css('width', (winWidth - 20));  // -20px border
	jQuery('#cWindowContentOuter').css('height', winHeight);
	jQuery('#cwin_tm, #cwin_bm').css('width', winWidth);
	jQuery('#cwin_ml, #cwin_mr').css('height', winHeight);
	
	// IE6: Rebuild alpha transparent border
	if (jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7)
	{
		// Doublecheck
		if (typeof(jomsIE6)!="undefined" && jomsIE6==true)
		{
		jQuery('#cwin_tm, #cwin_bm, #cwin_ml, #cwin_mr').each(function()
		{
			jQuery(this)[0].filters(0).sizingMethod="crop";
		})

		// IE6: Fallback for systems with AlphaImageLoader disabled.
		var cWindowContentOuter = jQuery('#cWindowContentOuter');
		cWindowContentOuter.css({'width': parseInt(cWindowContentOuter.css('width'))-2,
		                         'height': parseInt(cWindowContentOuter.css('height'))-2,
		                         'border': '1px solid black'});	
		}
	}

	jQuery('#cWindow').fadeIn();
}








// Custom CWindow show that we can pass raw HTML into.
function cWindowShowCustom(input, winTitle, winWidth, winHeight, winType) {

	// if no window type defined, then we use dialog as default
	var _type = ( winType == '' || winType == null ) ? 'dialog' : winType;

	var Obj = document.getElementById('cWindow');
	if(!Obj){
		Obj    = document.createElement('div');
		
		var html  = '';
		html += '<div class="cWindowBackground"></div>';
		html += '<div id="cWindow" class="' + _type + '" 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">';
    	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, #cWindowContentTop, #cWindowContent').width(winWidth);
	jQuery('#cwin_bm, #cwin_tm').width(winWidth);
	
	
	var myWidth = 0, myHeight = 0;
	myWidth  = jQuery(window).width();
	myHeight = jQuery(window).height();
	
	var yPos = jQuery(window).height() - 30;
	
	var leftPos = (myWidth - winWidth)/2;
	
	jQuery('#cWindow').css('zindex', cGetZIndexMax() + 1);
    
    jax.loadingFunction = function(){
		jQuery('#cWindowContent').addClass('loading');
	}
	
	jax.doneLoadingFunction = function(){
		jQuery('#cWindowContent').removeClass('loading');
	};
	
	// Set editor position, center it in screen regardless of the scroll position
	jQuery("#cWindow").css('margin-top', (jQuery(document).scrollTop() + 10 +(yPos - winHeight)/2) + (20)+'px');
	
	// Set height and width for transparent window
	jQuery('#cWindow').css('height', winHeight)
	                  .css('left', leftPos);
	jQuery('#cWindowContent').css('height', (winHeight - 50)) // - 30px title and 20px border
	                         .css('width', (winWidth - 20));  // -20px border
	jQuery('#cWindowContentOuter').css('height', winHeight);
	jQuery('#cwin_tm, #cwin_bm').css('width', winWidth);
	jQuery('#cwin_ml, #cwin_mr').css('height', winHeight);
	
	// IE6: Rebuild alpha transparent border
	if (jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7)
	{
		// Doublecheck
		if (typeof(jomsIE6)!="undefined" && jomsIE6==true)
		{
		jQuery('#cwin_tm, #cwin_bm, #cwin_ml, #cwin_mr').each(function()
		{
			jQuery(this)[0].filters(0).sizingMethod="crop";
		})

		// IE6: Fallback for systems with AlphaImageLoader disabled.
		var cWindowContentOuter = jQuery('#cWindowContentOuter');
		cWindowContentOuter.css({'width': parseInt(cWindowContentOuter.css('width'))-2,
		                         'height': parseInt(cWindowContentOuter.css('height'))-2,
		                         'border': '1px solid black'});	
		}
	}

	jQuery('#cWindow').fadeIn();
	
	/*Show the content after fully loaded*/
	jQuery('#cWindowContent').html(input);
	/**/
}













function cWindowHide() {
	if (jQuery('#cWindowAction').length > 0)
	{
		jQuery('#cWindowAction').animate({'bottom': -jQuery('#cWindowAction').height()}, 200, function()
		{
			jQuery('#cWindow').fadeOut('fast', function(){ jQuery(this).remove(); });
		});
	} else {
		jQuery('#cWindow').fadeOut('fast', function(){ jQuery(this).remove(); });
	}	
	jQuery(".cWindowBackground").fadeOut('fast', function(){ jQuery(this).remove(); });
}

function cWindowActions(action) {
	/* Remove cWindowAction from previously loaded cWindow (if any) */
	jQuery('#cWindowAction').remove();

	/* Create new cWindowAction */
	cWindowAction = jQuery('<div>').attr('id', 'cWindowAction')
	                               .html(action)
	                               .appendTo('#cWindowContentOuter');

	/* Slide cWindowAction to position */                                                  
	cWindowAction.css('bottom', -cWindowAction.height())
	             .animate({'bottom': 0}, 200);

	/* Set up behavior when actions are invoked */
	jax.loadingFunction = function() {
		jQuery('#cWindowAction').addClass('loading');
		jQuery('#cWindowContent').find('input,textarea,button')
		                         .attr('disabled', true);
	}
	jax.doneLoadingFunction = function() {
		jQuery('#cWindowAction').removeClass('loading');
		jQuery('#cWindowContent').find('input,textarea,button')
		                         .attr('disabled', true);
	};
}

function cWindowResize(newHeight){
	currentHeight = jQuery('#cWindowContentOuter').height();
	reduceHeight = (currentHeight - newHeight) /2; 
	
	// Lower top window
	jQuery('#cWindow').animate({marginTop: "+="+ reduceHeight +"px"}, 200);
    
    // Reduce height
	jQuery('#cwin_mr,#cwin_ml,#cWindowContentOuter').animate({height: newHeight}, 200, 'linear' , function()
	{
		if (typeof(jomsIE6)!="undefined" && jomsIE6==true) {
			// IE6: Fallback for systems with AlphaImageLoader disabled.
			var cWindowContentOuter = jQuery('#cWindowContentOuter');
			cWindowContentOuter.css({'height': parseInt(cWindowContentOuter.css('height'))-1,
			                         'border': '1px solid black'});	
		}
	});

    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;
}

