// IE6 flicker bugfix
// http://blog.klustered.com/2008/04/08/ie6-background-flicker/
try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}

jQuery.noConflict();

// Use jQuery via jQuery(...)
jQuery(document).ready(function(){

	/*
	*	Thomas Cook calls
	*/
				
		// fade out the used ones
		if ( jQuery("#usedline3").length > 0 ) {
			jQuery('#usedline3').fadeTo("slow", 0.33);
			jQuery('#use3').fadeOut('slow');
			jQuery('#print3').fadeOut('slow');
		}
		if ( jQuery("#usedline2").length > 0 ) {
			jQuery('#usedline2').fadeTo("slow", 0.33);
			jQuery('#use2').fadeOut('slow');
			jQuery('#print2').fadeOut('slow');
		}
		if ( jQuery("#usedline1").length > 0 ) {
			jQuery('#usedline1').fadeTo("slow", 0.33);
			jQuery('#use1').fadeOut('slow');
			jQuery('#print1').fadeOut('slow');
		}
		if ( jQuery("#usedline0").length > 0 ) {
			jQuery('#usedline0').fadeTo("slow", 0.33);
			jQuery('#use0').fadeOut('slow');
			jQuery('#print0').fadeOut('slow');
		}
		
		// fade out the used one and mark it in db
		jQuery('#use3').bind('click', function() {
			jQuery('#line3').fadeTo("slow", 0.33);
			var cardid = jQuery('#tc_desc_code3').html();
			markInDB(cardid);
			jQuery('#use3').fadeOut('slow');
			jQuery('#print3').fadeOut('slow');
		});
		jQuery('#use2').bind('click', function() {
			jQuery('#line2').fadeTo("slow", 0.33);
			var cardid = jQuery('#tc_desc_code2').html();
			markInDB(cardid);
			jQuery('#use2').fadeOut('slow');
			jQuery('#print2').fadeOut('slow');
		});
		jQuery('#use1').bind('click', function() {
			jQuery('#line1').fadeTo("slow", 0.33);
			var cardid = jQuery('#tc_desc_code1').html();
			markInDB(cardid);
			jQuery('#use1').fadeOut('slow');
			jQuery('#print1').fadeOut('slow');
		});
		jQuery('#use0').bind('click', function() {
			jQuery('#line0').fadeTo("slow", 0.33);
			var cardid = jQuery('#tc_desc_code0').html();
			markInDB(cardid);
			jQuery('#use0').fadeOut('slow');
			jQuery('#print0').fadeOut('slow');
		});
		
		// print out the voucher
		jQuery('#print3').bind('click', function() {
			jQuery('#tc_printvoucher_image').modal();
			var cardid = jQuery('#tc_desc_code3').html();
			var time1 = jQuery('#tc_desc_time13').html();
			var time2 = jQuery('#tc_desc_time23').html();
			jQuery('#code_auf_voucher').html(cardid);
			jQuery('#zeit_auf_voucher').html(time1.concat(' '+time2));
		});
		jQuery('#print2').bind('click', function() {
			jQuery('#tc_printvoucher_image').modal();
			var cardid = jQuery('#tc_desc_code2').html();
			var time1 = jQuery('#tc_desc_time12').html();
			var time2 = jQuery('#tc_desc_time22').html();
			jQuery('#code_auf_voucher').html(cardid);
			jQuery('#zeit_auf_voucher').html(time1.concat(' '+time2));
		});
		jQuery('#print1').bind('click', function() {
			jQuery('#tc_printvoucher_image').modal();
			var cardid = jQuery('#tc_desc_code1').html();
			var time1 = jQuery('#tc_desc_time11').html();
			var time2 = jQuery('#tc_desc_time21').html();
			jQuery('#code_auf_voucher').html(cardid);
			jQuery('#zeit_auf_voucher').html(time1.concat(' '+time2));
		});
		jQuery('#print0').bind('click', function() {
			jQuery('#tc_printvoucher_image').modal();
			var cardid = jQuery('#tc_desc_code0').html();
			var time1 = jQuery('#tc_desc_time10').html();
			var time2 = jQuery('#tc_desc_time20').html();
			jQuery('#code_auf_voucher').html(cardid);
			jQuery('#zeit_auf_voucher').html(time1.concat(' '+time2));
		});
		
		jQuery('#tc_drucken').bind('click', function() {
			jQuery('#tc_drucken').hide();
			jQuery('#tc_abbrechen').hide();
			jQuery('#stage').hide();
			jQuery('#tc_printvoucher_image').show();
			/* jQuery('#simplemodal-container').css("position","absolute");
			jQuery('#simplemodal-container').css("top","0px");
			jQuery('#simplemodal-container').css("left","0px"); 
			jQuery('#stage').addClass('nonPrint');*/
			generateVoucherPDF(jQuery('#code_auf_voucher').html(), jQuery('#zeit_auf_voucher').html());
			jQuery('#stage').show();
			jQuery.modal.close();
			jQuery('#tc_drucken').show();
			jQuery('#tc_abbrechen').show();			
		});
		jQuery('#tc_abbrechen').bind('click', function() {
			jQuery.modal.close();
		});
	/*
	*	END Thomas Cook Calls
	*/
	// jQuery("div.kartenLayer:last").addClass("lastKL");

	/**
	*
	*	Mega Drop-Down
	*
	**/
	
	// do not show the 4th level in Investoren
	/*
	jQuery('.first_level').each(function(){
		if(jQuery(this).children('a').html()=='Investoren'){
			jQuery(this).children('.dropDown_positonDiv').children('.dropDown_left').children('.dropDown_right').children('.second_level').each(function(){
				console.log("1");
				jQuery(this).children('li').each(function(){
					console.log("2");
					jQuery(this).children('.third_level').each(function(){
						console.log("3");
						jQuery(this).children('.li').children('.forth_level').each(function(){
							console.log("4");
							jQuery(this).children('li').css('display','none');
						});
					});
					
				});
			});
		}
		
	});*/
	 
	 //Float li of second navi level
	 jQuery('.second_level').each(function(){
	 
		var elements = new Array();
		var i = 1
		
		jQuery(this).children('li').each(function(){
			
			// Bold the Headlines
			jQuery(this).children('a').css('font-weight','bold');
			
			// count the links to determine where to put the elements
			var j = 3;
			jQuery(this).find('a').each(function(){
				j+=1;
			});
			
			// make the columns
			switch (i){
			
				// the first 3 elements will be placed in column 0,1,2
				case 1: 
				
					elements[0] = j;
					jQuery(this).addClass('floatLeft');
					break;
					
				case 2: 
				
					elements[1] = j;
					jQuery(this).addClass('floatLeft');
					break;
					
				case 3: 
				
					elements[2] = j;
					jQuery(this).addClass('floatLeft');
					jQuery(this).css('margin-right','0px');
					break;
					
				default : 
					// the next ones will be places in which column is the smalest
					if(elements[0] <= elements[1] && elements[0] <= elements[2]){
					
						// left column the smalest
						elements[0]+=j;
						jQuery(this).addClass('floatLeft');

					}else if(elements[0] >= elements[1] && elements[1] <= elements[2]){
					
						// middle column the smalest
						elements[1]+=j;
						jQuery(this).addClass('floatLeft');
						
					}else{
					
						// right colum the smalest
						jQuery(this).addClass('floatLeft');
						jQuery(this).css('margin-right','0px');
						elements[2]+=j;
						
					}

			}
			i ++;
		});
		// if the dropdown has only 1 or 2 elements reduce the background size
		if(i==2){
			jQuery(this).width("250px");
		}
		if(i==3){
			jQuery(this).width("500px");
		}
		
		jQuery(this).append('<li class="clearAll"></li>');
		jQuery(this).append('<li class="clearAll"></li>');
	});
	
	
	//Set thw width and height of background-div
	jQuery('.dropDown_positionDiv').each(function(){
		var p_right = parseInt(jQuery(this).children('.dropDown_left').css('padding-right'));
		var p_left = parseInt(jQuery(this).children('.dropDown_left').css('padding-left'));
		var p_top = parseInt(jQuery(this).children('.dropDown_left').css('padding-top'));
		var p_bottom = parseInt(jQuery(this).children('.dropDown_left').css('padding-bottom'));
		jQuery(this).height(jQuery(this).children('.dropDown_left').height() + p_top + p_bottom);
		jQuery(this).width(jQuery(this).children('.dropDown_left').width() + p_right + p_left);
	});
	
	jQuery('.dropDown_bottom').each(function(){
		jQuery(this).width(jQuery(this).parent().width() - parseInt(jQuery('.dropDown_left').css('padding-left')) - parseInt(jQuery('.dropDown_right').css('padding-right')));
	});
	//Hovereffekt
	jQuery('#mainnav .first_level a').each(function(){
		jQuery(this).mouseover(
			function(){
				jQuery(this).next('.dropDown_positonDiv').show();
			});
		jQuery(this).mouseleave(
			function(){
				jQuery(this).next('.dropDown_positonDiv').hide();
			}
		);
	});
	

	jQuery('.dropDown_positonDiv').each(function(){
		jQuery(this).hover(
			//MousOver
			function(){
				jQuery(this).prev('a').addClass("activeHover");
				jQuery(this).show();
			},
			//Mousout
			function(){
				jQuery(this).prev('a').removeClass("activeHover");
				jQuery(this).hide();
			}
		);
	});
	
	//Position of the Megadropdown
	var stage_offset = jQuery('#stage').offset();
	var stage_left = stage_offset.left;
	var stage_width = jQuery('#stage').width();
	
	jQuery('.dropDown_positonDiv').each(function(){
		//width of second_level + padding (left: 16, right: 16px)
		 var menu_width = 720 + (2*16);
		var menu_offset = jQuery(this).parent().offset();
		var menu_left = menu_offset.left;
																//13px is the BG-Shadow width 				
		if((menu_left + menu_width) - (stage_left + stage_width - 19) > 0){
			var new_left = (menu_left + menu_width) - (stage_left + stage_width - 19);
			var left_string = '-' + String(new_left) + 'px';
			jQuery(this).css('left', left_string);
			
		} 
	});
	
	
	
	//Set the height of News_border
	var max_news_height = 0;
	jQuery('.newsBody').each(function(){
		if(max_news_height < jQuery(this).height()){
			max_news_height = jQuery(this).height();
		}
	});
	jQuery('.newsLayer_border').height(max_news_height);
	
	
	// bookmarks
	jQuery('a#bookmarkLink').click(function(e) {
		jQuery('div#bookmarkPopup').css({
				'top' : - (jQuery('div#bookmarkPopup').height()) + 'px',
				'left':  '-20px'
			});
			jQuery('div#bookmarkPopup').toggle();
			return false;
	});
	jQuery('div#bookmarkPopupTop').click(function() {
		jQuery('div#bookmarkPopup').hide();
	});
	
	// InfoLayer HTML zusammenbauen	
	jQuery("img.infoicon").wrap('<a href="javascript:void(0);" onclick="showLayer(this,\'show\',event);return false;" class="linkInfoLayer"></a>');
	jQuery(".infoLayerBody").wrap('<div class="infoLayer"></div>');
	jQuery(".infoLayerBody").before('<div class="infoLayerHead"><a href="" title="Info schliessen" onclick="showLayer(this,\'hide\');return false;">zurück</a></div>');
	jQuery(".infoLayerBody").after('<div class="infoLayerFoot"></div>');

	jQuery(".btLink a").css("padding-right","5px");
	jQuery(".btLink").css("display","block");
	if(jQuery.browser.msie && (jQuery.browser.version == "7.0" || jQuery.browser.version == "6.0"  ) ) {
		jQuery(".btLink").after('<br class="clearAll" />');
		window.setTimeout("renderBtLinkIe()",10); // Angaben werden sonst in IE 6 + 7 nicht uebernommen :-(
	}
	else {
		jQuery(".btLink a").after('<img src="/img/bg-end_bt-link.gif" width="6" height="29" alt="" border="0" class="btEndpic"  />');
	}

	jQuery(".tx-indexedsearch-res hr:last").remove();
	// Suchergebnisse mit Bildern:
	var countSearchResults = jQuery(".tx-indexedsearch-percent").length;
	for ( var i = 0; i < countSearchResults; i++ ) {
		var percentHtml = jQuery(".tx-indexedsearch-percent").get(i).innerHTML;
		percentHtml = percentHtml.replace(/%/, ""); // %-Zeichen loeschen

		if ( percentHtml > 75 )
			jQuery(".tx-indexedsearch-percent").get(i).innerHTML = '<img src="/img/search_percent100.gif" alt="100%" width="35" height="10" />';
		else if ( percentHtml > 50 )
			jQuery(".tx-indexedsearch-percent").get(i).innerHTML = '<img src="/img/search_percent75.gif" alt="75%" width="35" height="10" />';
		else if ( percentHtml > 25 )
			jQuery(".tx-indexedsearch-percent").get(i).innerHTML = '<img src="/img/search_percent50.gif" alt="50%" width="35" height="10" />';
		else
			jQuery(".tx-indexedsearch-percent").get(i).innerHTML = '<img src="/img/search_percent25.gif" alt="25%" width="35" height="10" />';
	}
	
	// Suchbox einblenden, falls Suche ohne Parameter aufgerufen:
	if ( jQuery(".tx-indexedsearch").length >= 1 && jQuery(".tx-indexedsearch-whatis").length <= 0 ) {
		jQuery(".tx-indexedsearch-searchbox").css("display","block");
	}

	if(jQuery("#frameLayer")) {
	}
	
	// farbige Tabelle: abwechselnd grau Zeilen
	jQuery(".coloredTable tbody tr:odd").addClass("trOdd");

});

function printElem(elem)
    {
       var html='<html><head><link rel="stylesheet" href="/css/print-core.css" type="text/css" /><title>Voucher</title></head><body>';
	   html+= jQuery(elem).html();
	   html+="</body></html>";

	   var printWin = window.open('','','left=0,top=0,width=1000,height=500,toolbar=0,scrollbars=0,status  =0');
	   printWin.document.write(html);
	   printWin.document.close();
	   printWin.focus();
	   printWin.print();
	   printWin.close();

    }

function generateVoucherPDF(_code, _zeit){
	var result = '';
	var ses_id = document.cookie.split('fe_typo_user=');
	if(ses_id.length>0) {
		ses_id = ses_id[1].split(';');
		ses_id = ses_id[0];
	}
	result = jQuery.ajax({
		type: 'POST',
		url: '/typo3conf/ext/thomas_cook_ext/pi1/inc/inc.generateVoucherPDF.php',
		data: ({
			code: _code,
			zeit: _zeit,
			hashe: ses_id
		}),
		success: function(returnval) {
			var uri = "/typo3conf/ext/thomas_cook_ext/pi1/inc/"+returnval;
            WinPdf = window.open(uri);
			WinPdf.focus();
			//window.setTimeout("deletePDF("+returnval+")", 10000);
			deletePDF(returnval);
        }

	}).responseText;
	
	return result;
}

function deletePDF(_url){

	result = jQuery.ajax({
		type: 'POST',
		url: '/typo3conf/ext/thomas_cook_ext/pi1/inc/inc.deletePDF.php',
		data: ({
			url:_url
		})

	}).responseText;
}

function renderBtLinkIe() {
	jQuery(".btLink a").css("padding-right","5px");
	jQuery(".btLink a").after('<img src="/img/bg-end_bt-link.gif" width="6" height="29" alt="" border="0" class="btEndpic"  />');
	jQuery(".btLink").css("display","block");
}


function zeige(objekt,aktion,ebene) {
	if( ebene == 1 && aktion == 1 ) {
		jQuery(objekt).children("a").addClass("activeHover");
	}
	if( ebene == 1 && aktion == 0 ) {
		jQuery(objekt).children("a").removeClass("activeHover");
	}
	if(aktion == 1){
		jQuery(objekt).children("ul").css("display","block");
	}
	if( aktion == 0 ){
		if(jQuery(objekt).children('ul'))jQuery(objekt).children('ul').css('display','none');
	}
}

function adjustFrameLayerHeight() {
	var theFrame = jQuery("#frameLayer", parent.document.body);
	theFrame.height(jQuery(document.body).height());
	frameLayerHeightPlus = 0;
	window.setTimeout("adjustFrameLayerHeight()", 800);
}

/* mouseposition */
document.onmousedown = LayerPos;

var x = null;
var y = null;
var infoLayerPosX = null;
var infoLayerPosY = null;

// Cursor-position
function LayerPos(e){
	if(jQuery(".linkInfoLayer img").css("width")) {
		infoBildBreite = parseInt(jQuery(".linkInfoLayer img").css("width").substring(-2,(jQuery(".linkInfoLayer img").css("width").length)-2));
		subnavLayerBreite = parseInt(jQuery("#subnavLayer").css("width").substring(-2,(jQuery("#subnavLayer").css("width").length)-2));
		contentBGpad = parseInt(jQuery(".contentBG").css("padding-left").substring(-2,(jQuery(".contentBG").css("padding-left").length)-2));
		stageLinkerRand = parseInt(document.getElementById("stage").offsetLeft);
		headLayerHoehe = parseInt(jQuery("#headLayer").css("height").substring(-2,(jQuery("#headLayer").css("height").length)-2));
		headlineLayerHoehe = parseInt(jQuery("#headlineLayer").css("height").substring(-2,(jQuery("#headlineLayer").css("height").length)-2));
		if(e){
			x = parseInt(e.pageX);
			y = parseInt(e.pageY);
			infoLayerPosX = x-stageLinkerRand-subnavLayerBreite-contentBGpad+infoBildBreite+22;
			infoLayerPosY = y-headlineLayerHoehe-headLayerHoehe;
		}
		else {
			x = parseInt(window.event.x);
			y = parseInt(window.event.y);
			infoLayerPosX = x + infoBildBreite;
			infoLayerPosY = y;
		}
	}
}

function showLayer(objekt,aktion) {
	jQuery(document).unbind("keydown");
    var infoLayer = jQuery(objekt).parent().children(".infoLayer");
	if ( infoLayer.html() == null )
		infoLayer = jQuery(objekt).parent().parent().children(".infoLayer");
	if( aktion == "show") {
		jQuery("#contentLayer").css("zIndex",1);
		if(infoLayer.css("display")=="block") {
			infoLayer.css("display","none");
		}
		else {
			jQuery(".infoLayer").css("display","none");
			infoLayer.css("top",(infoLayerPosY)+"px");
			infoLayer.css("left",(infoLayerPosX)+"px");
			infoLayer.css("display","block");
			
			jQuery(document).keydown(function(event){
                var key = event.which; 
                if (key == 27) { // ESC 
                    infoLayer.css("display","none");
                    jQuery(document).unbind("keydown");       
                } 
            }); 
		}
	}
	else {
		if(jQuery.browser.msie) {
			jQuery("#contentLayer").css("zIndex",-1);
		}
		jQuery(objekt).parent().parent(".infoLayer").css("display","none");
	}
}

function styleTable () {
	//jQuery("coloredTable").children("tr");
}

function calcCardfinderResultCount() {
	var queryStr = 'card_type='+document.getElementById('card_type').value+'&motive='+document.getElementById('motive').value;
	queryStr += '&additional_insurance='+document.getElementById('additional_insurance').value+'&additional_functions='+document.getElementById('additional_functions').value;
	loadXMLDoc('http://'+window.location.hostname+'/?eID=ajaxcardfinder', 'POST', queryStr, handle_calcCardfinderResultCount);
}

function handle_calcCardfinderResultCount() {
	var response = null;
	if ( req1 && req1.readyState == 4) {
		if (req1.status == 200) {
			response = req1.responseText;
		} else {
			// alert("There was a problem retrieving the data:\n" +req1.statusText);
		}
		req1 = false;
	}

	if ( req2 && req2.readyState == 4) {
		if (req2.status == 200) {
			response = req2.responseText;
		} else {
			// alert("There was a problem retrieving the data:\n" +req2.statusText);
		}
		req2 = false;
	}
	
	if ( req3 && req3.readyState == 4) {
		if (req3.status == 200) {
			response = req3.responseText;
		} else {
			// alert("There was a problem retrieving the data:\n" +req3.statusText);
		}
		req3 = false;
	}
	if ( response != null ) {
		if ( response == "1" )
			jQuery("#cardfinderResultCount").html("Die Auswahl ergibt 1 geeignete Karte!");
		else
			jQuery("#cardfinderResultCount").html("Die Auswahl ergibt "+response+" geeignete Karten!");
	}
}

var req1;
var req2;
var req3;
function loadXMLDoc(url, reqType, queryString, handle) {
	var reqType = reqType || 'GET'; 
	var handle = handle || processReqChange;
	var req;
	req = false;
	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
			req.onreadystatechange = handle;
			req.open(reqType, url, true);
			if (reqType=='POST') {
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
			}
			req.send(queryString);
	    } catch(e) {
			req = false;
	    }
	} else if(window.ActiveXObject) {
   		try {
     		req = new ActiveXObject("Msxml2.XMLHTTP");
			if (req) {
				req.onreadystatechange = handle;
				req.open(reqType, url, true);
				if (reqType=='POST') {
					req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
				}
				req.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
				req.send(queryString);
			}
   		} catch(e) {
     		req = false;
		}
	}
	if ( !req1 ) 
		req1 = req;
	else if ( !req2 ) 
		req2 = req;
	else if ( !req3 ) 
		req3 = req;
}
function processReqChange() {
	if ( req1 && req1.readyState == 4) {
		if (req1.status == 200) {
			eval(req1.responseText);
		} else {
			alert("There was a problem retrieving the data:\n" +req1.statusText);
		}
		req1 = false;
	}

	if ( req2 && req2.readyState == 4) {
		if (req2.status == 200) {
			eval(req2.responseText);
		} else {
			alert("There was a problem retrieving the data:\n" +req2.statusText);
		}
		req2 = false;
	}
	
	if ( req3 && req3.readyState == 4) {
		if (req3.status == 200) {
			eval(req3.responseText);
		} else {
			alert("There was a problem retrieving the data:\n" +req3.statusText);
		}
		req3 = false;
	}
}

/*
*	START Thomas Cook voucher functions
*/

function markInDB(cardid) {
	jQuery.ajax({
		type: "POST",
		url: "fileadmin/scripts/markInDB.php",
		data: "id="+cardid,
		success: function(msg){

		}
	});
}

/**
*	simple Modal jQuery Plugin
*/

(function(d){var k=d.browser.msie&&parseInt(d.browser.version)===6&&typeof window.XMLHttpRequest!=="object",m=d.browser.msie&&parseInt(d.browser.version)===7,l=null,f=[];d.modal=function(a,b){return d.modal.impl.init(a,b)};d.modal.close=function(){d.modal.impl.close()};d.modal.focus=function(a){d.modal.impl.focus(a)};d.modal.setContainerDimensions=function(){d.modal.impl.setContainerDimensions()};d.modal.setPosition=function(){d.modal.impl.setPosition()};d.modal.update=function(a,b){d.modal.impl.update(a,
b)};d.fn.modal=function(a){return d.modal.impl.init(this,a)};d.modal.defaults={appendTo:"body",focus:true,opacity:50,overlayId:"simplemodal-overlay",overlayCss:{},containerId:"simplemodal-container",containerCss:{},dataId:"simplemodal-data",dataCss:{},minHeight:null,minWidth:null,maxHeight:null,maxWidth:null,autoResize:false,autoPosition:true,zIndex:1E3,close:true,closeHTML:'<a class="modalCloseImg" title="Close"></a>',closeClass:"simplemodal-close",escClose:true,overlayClose:false,position:null,
persist:false,modal:true,onOpen:null,onShow:null,onClose:null};d.modal.impl={d:{},init:function(a,b){var c=this;if(c.d.data)return false;l=d.browser.msie&&!d.boxModel;c.o=d.extend({},d.modal.defaults,b);c.zIndex=c.o.zIndex;c.occb=false;if(typeof a==="object"){a=a instanceof jQuery?a:d(a);c.d.placeholder=false;if(a.parent().parent().size()>0){a.before(d("<span></span>").attr("id","simplemodal-placeholder").css({display:"none"}));c.d.placeholder=true;c.display=a.css("display");if(!c.o.persist)c.d.orig=
a.clone(true)}}else if(typeof a==="string"||typeof a==="number")a=d("<div></div>").html(a);else{alert("SimpleModal Error: Unsupported data type: "+typeof a);return c}c.create(a);c.open();d.isFunction(c.o.onShow)&&c.o.onShow.apply(c,[c.d]);return c},create:function(a){var b=this;f=b.getDimensions();if(b.o.modal&&k)b.d.iframe=d('<iframe src="javascript:false;"></iframe>').css(d.extend(b.o.iframeCss,{display:"none",opacity:0,position:"fixed",height:f[0],width:f[1],zIndex:b.o.zIndex,top:0,left:0})).appendTo(b.o.appendTo);
b.d.overlay=d("<div></div>").attr("id",b.o.overlayId).addClass("simplemodal-overlay").css(d.extend(b.o.overlayCss,{display:"none",opacity:b.o.opacity/100,height:b.o.modal?f[0]:0,width:b.o.modal?f[1]:0,position:"fixed",left:0,top:0,zIndex:b.o.zIndex+1})).appendTo(b.o.appendTo);b.d.container=d("<div></div>").attr("id",b.o.containerId).addClass("simplemodal-container").css(d.extend(b.o.containerCss,{display:"none",position:"fixed",zIndex:b.o.zIndex+2})).append(b.o.close&&b.o.closeHTML?d(b.o.closeHTML).addClass(b.o.closeClass):
"").appendTo(b.o.appendTo);b.d.wrap=d("<div></div>").attr("tabIndex",-1).addClass("simplemodal-wrap").css({height:"100%",outline:0,width:"100%"}).appendTo(b.d.container);b.d.data=a.attr("id",a.attr("id")||b.o.dataId).addClass("simplemodal-data").css(d.extend(b.o.dataCss,{display:"none"})).appendTo("body");b.setContainerDimensions();b.d.data.appendTo(b.d.wrap);if(k||l)b.fixIE()},bindEvents:function(){var a=this;d("."+a.o.closeClass).bind("click.simplemodal",function(b){b.preventDefault();a.close()});
a.o.modal&&a.o.close&&a.o.overlayClose&&a.d.overlay.bind("click.simplemodal",function(b){b.preventDefault();a.close()});d(document).bind("keydown.simplemodal",function(b){if(a.o.modal&&b.keyCode===9)a.watchTab(b);else if(a.o.close&&a.o.escClose&&b.keyCode===27){b.preventDefault();a.close()}});d(window).bind("resize.simplemodal",function(){f=a.getDimensions();a.o.autoResize?a.setContainerDimensions():a.o.autoPosition&&a.setPosition();if(k||l)a.fixIE();else if(a.o.modal){a.d.iframe&&a.d.iframe.css({height:f[0],
width:f[1]});a.d.overlay.css({height:f[0],width:f[1]})}})},unbindEvents:function(){d("."+this.o.closeClass).unbind("click.simplemodal");d(document).unbind("keydown.simplemodal");d(window).unbind("resize.simplemodal");this.d.overlay.unbind("click.simplemodal")},fixIE:function(){var a=this,b=a.o.position;d.each([a.d.iframe||null,!a.o.modal?null:a.d.overlay,a.d.container],function(c,h){if(h){var g=h[0].style;g.position="absolute";if(c<2){g.removeExpression("height");g.removeExpression("width");g.setExpression("height",
'document.body.scrollHeight > document.body.clientHeight ? document.body.scrollHeight : document.body.clientHeight + "px"');g.setExpression("width",'document.body.scrollWidth > document.body.clientWidth ? document.body.scrollWidth : document.body.clientWidth + "px"')}else{var e;if(b&&b.constructor===Array){c=b[0]?typeof b[0]==="number"?b[0].toString():b[0].replace(/px/,""):h.css("top").replace(/px/,"");c=c.indexOf("%")===-1?c+' + (t = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"':
parseInt(c.replace(/%/,""))+' * ((document.documentElement.clientHeight || document.body.clientHeight) / 100) + (t = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"';if(b[1]){e=typeof b[1]==="number"?b[1].toString():b[1].replace(/px/,"");e=e.indexOf("%")===-1?e+' + (t = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) + "px"':parseInt(e.replace(/%/,""))+' * ((document.documentElement.clientWidth || document.body.clientWidth) / 100) + (t = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) + "px"'}}else{c=
'(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (t = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"';e='(document.documentElement.clientWidth || document.body.clientWidth) / 2 - (this.offsetWidth / 2) + (t = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) + "px"'}g.removeExpression("top");g.removeExpression("left");g.setExpression("top",
c);g.setExpression("left",e)}}})},focus:function(a){var b=this;a=a&&d.inArray(a,["first","last"])!==-1?a:"first";var c=d(":input:enabled:visible:"+a,b.d.wrap);setTimeout(function(){c.length>0?c.focus():b.d.wrap.focus()},10)},getDimensions:function(){var a=d(window);return[d.browser.opera&&d.browser.version>"9.5"&&d.fn.jquery<"1.3"||d.browser.opera&&d.browser.version<"9.5"&&d.fn.jquery>"1.2.6"?a[0].innerHeight:a.height(),a.width()]},getVal:function(a,b){return a?typeof a==="number"?a:a==="auto"?0:
a.indexOf("%")>0?parseInt(a.replace(/%/,""))/100*(b==="h"?f[0]:f[1]):parseInt(a.replace(/px/,"")):null},update:function(a,b){var c=this;if(!c.d.data)return false;c.d.origHeight=c.getVal(a,"h");c.d.origWidth=c.getVal(b,"w");c.d.data.hide();a&&c.d.container.css("height",a);b&&c.d.container.css("width",b);c.setContainerDimensions();c.d.data.show();c.o.focus&&c.focus();c.unbindEvents();c.bindEvents()},setContainerDimensions:function(){var a=this,b=k||m,c=a.d.origHeight?a.d.origHeight:d.browser.opera?
a.d.container.height():a.getVal(b?a.d.container[0].currentStyle.height:a.d.container.css("height"),"h");b=a.d.origWidth?a.d.origWidth:d.browser.opera?a.d.container.width():a.getVal(b?a.d.container[0].currentStyle.width:a.d.container.css("width"),"w");var h=a.d.data.outerHeight(true),g=a.d.data.outerWidth(true);a.d.origHeight=a.d.origHeight||c;a.d.origWidth=a.d.origWidth||b;var e=a.o.maxHeight?a.getVal(a.o.maxHeight,"h"):null,i=a.o.maxWidth?a.getVal(a.o.maxWidth,"w"):null;e=e&&e<f[0]?e:f[0];i=i&&i<
f[1]?i:f[1];var j=a.o.minHeight?a.getVal(a.o.minHeight,"h"):"auto";c=c?a.o.autoResize&&c>e?e:c<j?j:c:h?h>e?e:a.o.minHeight&&j!=="auto"&&h<j?j:h:j;e=a.o.minWidth?a.getVal(a.o.minWidth,"w"):"auto";b=b?a.o.autoResize&&b>i?i:b<e?e:b:g?g>i?i:a.o.minWidth&&e!=="auto"&&g<e?e:g:e;a.d.container.css({height:c,width:b});a.d.wrap.css({overflow:h>c||g>b?"auto":"visible"});a.o.autoPosition&&a.setPosition()},setPosition:function(){var a=this,b,c;b=f[0]/2-a.d.container.outerHeight(true)/2;c=f[1]/2-a.d.container.outerWidth(true)/
2;if(a.o.position&&Object.prototype.toString.call(a.o.position)==="[object Array]"){b=a.o.position[0]||b;c=a.o.position[1]||c}else{b=b;c=c}a.d.container.css({left:c,top:b})},watchTab:function(a){var b=this;if(d(a.target).parents(".simplemodal-container").length>0){b.inputs=d(":input:enabled:visible:first, :input:enabled:visible:last",b.d.data[0]);if(!a.shiftKey&&a.target===b.inputs[b.inputs.length-1]||a.shiftKey&&a.target===b.inputs[0]||b.inputs.length===0){a.preventDefault();b.focus(a.shiftKey?"last":
"first")}}else{a.preventDefault();b.focus()}},open:function(){var a=this;a.d.iframe&&a.d.iframe.show();if(d.isFunction(a.o.onOpen))a.o.onOpen.apply(a,[a.d]);else{a.d.overlay.show();a.d.container.show();a.d.data.show()}a.o.focus&&a.focus();a.bindEvents()},close:function(){var a=this;if(!a.d.data)return false;a.unbindEvents();if(d.isFunction(a.o.onClose)&&!a.occb){a.occb=true;a.o.onClose.apply(a,[a.d])}else{if(a.d.placeholder){var b=d("#simplemodal-placeholder");if(a.o.persist)b.replaceWith(a.d.data.removeClass("simplemodal-data").css("display",
a.display));else{a.d.data.hide().remove();b.replaceWith(a.d.orig)}}else a.d.data.hide().remove();a.d.container.hide().remove();a.d.overlay.hide();a.d.iframe&&a.d.iframe.hide().remove();setTimeout(function(){a.d.overlay.remove();a.d={}},10)}}}})(jQuery);

