$(window).resize(function() {
  if($('#overlay').css('display') == 'block'){
	  adjustOverlaySize();
	  setTimeout('repositionModalDialog()',200);
  }  
});

var bodyWidth = 0;
var windowHeight = 0;
var expanderHeight = 0;
var newTop = 0;
var newLeft = 0;
var modalWidth = 0;
var modalHeight = 0;
var origmodalbodyheight = 0;
var iframe_content = null;

var otherdomain = false;
var	crossdombodywidth = 0;
var	crossdombodyheight = 0;	


function showModalDialog(path){
	adjustOverlaySize();
	otherdomain = false;
	//$('select').css('visibility','hidden');	
	scrollTo(0,0);
	if($('div#modalbox').length == 0){
		if ($.browser.msie){
//			$('.expander').prepend('<div id="iemodal" style="width:auto;background-color:transparent;position:absolute;left:0px;top:0px;z-index:10000;display:none;"><div id="modalbox" class="roundmodal" style="display:block;position:relative;"><div id="modalclose" ></div></div></div>');
			$('.expander').prepend('<div id="iemodal" style="width:auto;background-color:transparent;position:absolute;left:0px;top:0px;z-index:10000;display:none;"><div id="modalbox" class="roundmodal" style="display:block;position:relative;"><a id="modalclose" href="#" title="Close"></a></div></div>');
		}
		else{
			//$('.expander').prepend('<div id="modalbox" class="roundmodal"><div id="modalclose" ></div></div>');	
			$('.expander').prepend('<div id="modalbox" class="roundmodal"><a id="modalclose" href="#" title="Close"></a></div>');
		}
	}
	
	$('#modalclose , #overlay').click(function(){
		closeModalDialog();	
	});
	if (!$.browser.msie){
		$('.roundmodal').corner('5px');
	}	
	$('#modalclose').attr('style','filter: progid:DXImageTransform.Microsoft.AlphaImageLoader (src="images/modalclosebutton.png", sizingMethod="scale")');
	showModalContent(path);
}

function showCrossDomainModalDialog(path, width, height){
	adjustOverlaySize();
	otherdomain = true;
	crossdombodywidth = width;
	crossdombodyheight = height;
	//$('select').css('visibility','hidden');	
	scrollTo(0,0);
	if($('div#modalbox').length == 0){
		if ($.browser.msie){
			$('.expander').prepend('<div id="iemodal" style="width:auto;background-color:transparent;position:absolute;left:0px;top:0px;z-index:10000;display:none;"><div id="modalbox" class="roundmodal" style="display:block;position:relative;"><a id="modalclose" href="#" title="Close"></a></div></div>');
		}
		else{
			$('.expander').prepend('<div id="modalbox" class="roundmodal"><a id="modalclose" href="#" title="Close"></a></div>');	
		}
	}
	
	$('#modalclose , #overlay').click(function(){
		closeModalDialog();	
	});
	if (!$.browser.msie){
		$('.roundmodal').corner('5px');
	}	
	$('#modalclose').attr('style','filter: progid:DXImageTransform.Microsoft.AlphaImageLoader (src="images/modalclosebutton.png", sizingMethod="scale")');
	showModalContent(path);
}

function adjustOverlaySize(){
	if($('div#overlay').length == 0){
		$('body').prepend('<div id="overlay"></div>');
	}
	$('#overlay').css('opacity','0.70');
	$('#overlay').fadeIn('fast');
	bodyWidth = $('body').outerWidth(true);
	windowHeight = $(window).height();
	expanderHeight = $('.expander').outerHeight(true);
	
	if(bodyWidth < 1000)
		bodyWidth=1000;
	$('#overlay').width(bodyWidth);
	$('#overlay').height(expanderHeight);
}

function closeModalDialog(){	
	$('#modalcontent').slideUp('fast');
	if ($.browser.msie){
		$('#iemodal').fadeOut('fast');	
	}
	else{
		$('#modalbox').fadeOut('fast');	
	}
	$('#overlay').fadeOut('fast');	
	$('select').css('visibility','visible');
	setTimeout('$("#modalcontent").remove()',500);
	
	$('div#closemodaltip').remove();
}

function repositionModalDialog(){
	newLeft = (bodyWidth - (modalWidth+40))/2;
	newTop = (windowHeight - modalHeight)/2;
	
	if(windowHeight < modalHeight){
		newTop = 40;
	}
	if ($.browser.msie){
		$('#iemodal').css({'left':newLeft,'top':newTop});
	}
	else{
		$('#modalbox').css({'left':newLeft,'top':newTop});
	}
}
function resizeModalContent(){
	var currentWidth = 0; var widestchild = 0;
	var titleHeight = 0;
	var newContentWidth = 0;
	var newContentHeight = 0;
	var addScrollNo = true;
	
	if(otherdomain == false){
		origmodalbodyheight = $('#modalcontent').contents().find('div#modalbody').outerHeight();
		$('#modalcontent').contents().find('div#modalbody').children().each(function(){
			currentWidth = $(this).width();
			if(currentWidth > widestchild)
			{
				widestchild  = currentWidth;
			}
			newContentHeight = newContentHeight + $(this).outerHeight(true);
			if($.trim($(this).attr('id')) == 'modaltitle'){
				titleHeight = $(this).outerHeight(true);
			}
		});
		
		if($('#modalcontent').contents().find('div#modalbody').find('object').length == 0){//if no flash object in page
			newContentHeight = $('#modalcontent').contents().find('div#modalbody').outerHeight(true);
		}
		newContentWidth = widestchild + 20;
		newContentHeight += 20;
		
		if(newContentHeight - titleHeight > 650){
			newContentWidth += 36;
			newContentHeight = 650;
			addScrollNo = false
		}
		if(newContentWidth > 920){
			newContentWidth = 920;
		}
		
		$('#modalcontent').width(newContentWidth);	
		$('#modalcontent').height(newContentHeight);
		
		modalWidth = $('#modalcontent').width();
		modalHeight = $('#modalcontent').height();
		
		var hiddenDivs = $('#modalcontent').contents().find("div:hidden").not("script");
		if(hiddenDivs.length > 0 && addScrollNo == true){
			$('#modalcontent').attr('scrolling','no');
		}
		if(addScrollNo == true){
			//$(window.frameModal.document.getElementsByTagName("body")[0]).attr('scroll','no');
			$('#modalcontent').contents().find("body").attr('scroll','no');
		}
	}
	else{
		newContentWidth = crossdombodywidth;
		newContentHeight = crossdombodyheight;
		
		if($.browser.msie){
			newContentHeight +=10;
		}
		
		$('#modalcontent').width(newContentWidth);	
		$('#modalcontent').height(newContentHeight);
		
		modalWidth = $('#modalcontent').width();
		modalHeight = $('#modalcontent').height();		
	}
	
}

function iframeInitialize(){
	var iframe = $("#modalcontent");
	setTimeout("resizeModalContent()",250);
	setTimeout("repositionModalDialog()",250);

	if ($.browser.msie){
		setTimeout("$('#iemodal').hide()",250);
		setTimeout("$('#iemodal').css('visibility','visible')",250);
		setTimeout("$('#iemodal').fadeIn('fast')",250);
		if (window.PIE) {
			$('#modalbox').each(function() {
				PIE.attach(this);
			});
		}
	}
	else{
		setTimeout("$('#modalbox').hide()",250);
		setTimeout("$('#modalbox').css('visibility','visible')",250);
		setTimeout("$('#modalbox').fadeIn('fast')",250);
	}
	
	if(otherdomain == false){
		if(iframe.contents().find('form').length == 0 && iframe.contents().find('script[src*="swfaddress.js"]').length == 0){
			setTimeout(function(){
				iframe_content = iframe.contents().find('#modalbody'); 
					iframe_content.resize(function(){
						if(iframe_content.outerHeight() > origmodalbodyheight){
							$('#modalcontent').attr('scrolling','no');
							//$('#modalcontent').contents().find('body').attr('scroll','no');
							//$(window.frameModal.document.getElementsByTagName("body")[0]).attr('scroll','no');
							$('#modalcontent').height($('#modalcontent').height() + (iframe_content.outerHeight() - origmodalbodyheight ));
							origmodalbodyheight = iframe_content.outerHeight();
						}
						else if(iframe_content.outerHeight() < origmodalbodyheight){
							$('#modalcontent').attr('scrolling','no');
							//$('#modalcontent').contents().find('body').attr('scroll','no');
							//$(window.frameModal.document.getElementsByTagName("body")[0]).attr('scroll','no');
							$('#modalcontent').height($('#modalcontent').height() - (origmodalbodyheight - iframe_content.outerHeight()));
							origmodalbodyheight = iframe_content.outerHeight();
						}			
					});		
			},1000);
		}
	}
}

function showModalContent(source){
	$('#modalbox').append("<iframe id='modalcontent' name='frameModal' frameborder='0' scrolling='auto' allowtransparency='true' src='" + source + "' style='width:900px' onload='top.iframeInitialize();'>&lt/iframe>");
	if ($.browser.msie){
		$('#iemodal').css('visibility','hidden');
		$('#iemodal').show();
	}
	else{
		$('#modalbox').css('visibility','hidden');
		$('#modalbox').show();
	}
	
//	frames["frameModal"].onload = function(){
//		alert('');
//	}
	
//	$("#modalcontent").load(function (){
//		var iframe = $("#modalcontent");
//		setTimeout("resizeModalContent()",250);
//		setTimeout("repositionModalDialog()",250);
//
//		if ($.browser.msie){
//			setTimeout("$('#iemodal').hide()",250);
//			setTimeout("$('#iemodal').css('visibility','visible')",250);
//			setTimeout("$('#iemodal').fadeIn('fast')",250);
//			if (window.PIE) {
//				$('#modalbox').each(function() {
//					PIE.attach(this);
//				});
//			}
//		}
//		else{
//			setTimeout("$('#modalbox').hide()",250);
//			setTimeout("$('#modalbox').css('visibility','visible')",250);
//			setTimeout("$('#modalbox').fadeIn('fast')",250);
//		}
//		
//		setTimeout(function(){
//			iframe_content = iframe.contents().find('#modalbody'); 
////			if(!iframe_content.hasClass('notresizable')){
//				iframe_content.resize(function(){
//					if(iframe_content.outerHeight() > origmodalbodyheight){
//						$('#modalcontent').attr('scrolling','no');
//						//$('#modalcontent').contents().find('body').attr('scroll','no');
//						//$(window.frameModal.document.getElementsByTagName("body")[0]).attr('scroll','no');
//						$('#modalcontent').height($('#modalcontent').height() + (iframe_content.outerHeight() - origmodalbodyheight ));
//						origmodalbodyheight = iframe_content.outerHeight();
//					}
//					else if(iframe_content.outerHeight() < origmodalbodyheight){
//						$('#modalcontent').attr('scrolling','no');
//						//$('#modalcontent').contents().find('body').attr('scroll','no');
//						//$(window.frameModal.document.getElementsByTagName("body")[0]).attr('scroll','no');
//						$('#modalcontent').height($('#modalcontent').height() - (origmodalbodyheight - iframe_content.outerHeight()));
//						origmodalbodyheight = iframe_content.outerHeight();
//					}			
//				});		
////			}			

//		},1000);
//		
//	});
}
/*Auot overlay config*/
$(document).ready(function() {
	var cDOM = window.location;
	cDOM = cDOM.toString();
	keyDom = cDOM.indexOf('?o=');
	if (keyDom > 1){
		$.ajax({
		type: "GET",
		url: "/gbm/features/common/tools/auto-overlay.xml",
		dataType: "xml",
		success: parseXmlOverlay
		});
	}
	//alert(checkOverlayDOM);
});
function parseXmlOverlay(xml){
	var checkOverlayDOM = window.location;
	checkOverlayDOM = checkOverlayDOM.toString();
	keyOverlay = checkOverlayDOM.indexOf('?o=');
	var overlayKey = checkOverlayDOM.substr(keyOverlay+3);
	//alert(overlayKey);
	$(xml).find('channel').filter(function(index){
		//$("#output").append($(this).find('url') + "<br />");
		if(overlayKey.length > 0){
			//alert(overlayKey);
			expression = new RegExp(overlayKey,"gi");
			if($('key', this).text().match(expression)) return true;
		}
		else
			return true;
	}).each(function(){
		overlayVal = $(this).find('url').text();
		showModalDialog(overlayVal);
	})
//	});
}
