/* Script to fix PNG alpha transparency in IE6 - based on bgsleight, modified for use with jQuery */
function alphaBackgrounds(selector){
	var needsAlphaFix = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if(needsAlphaFix) {
		$(selector).each(function() {
			var bg = $(this).css('background-image');
			var br = $(this).css('background-repeat');
		
			if (br.match(/no-repeat/i) != null){
				var mysizemethod = "crop";
			} else {
				var mysizemethod = "scale";
			}
			if (bg.match(/\.png/i) != null){
				var mypng = bg.substring(5,bg.length-2);
				$(this).css('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='"+mysizemethod+"')");
				$(this).css('background-image','url(/interface/js/x.gif)');
			}
		});
	}
}

// Create tooltip
function makeTooltip(title,content,titleclass,follow,left,top,width,height,offset_x,offset_y,showclose,effect) {
	$('#tooltip').html('');
	var closebtn = '';
	if(showclose == true) {
		closebtn = '<span class="tooltipClose ui-icon-right ui-icon ui-icon-close">Close</span>';
	}
	if(follow == true) {
		// Position tooltip element near mouse pointer
		$().mousemove(function(e){
			$('#tooltip').css("left",( e.pageX - eval(offset_x) )+'px');
			$('#tooltip').css("top",( e.pageY + eval(offset_y) )+'px');
		});
	}
	if(follow == false && left && top) {
		$('#tooltip').css("left",left+'px');
		$('#tooltip').css("top",top+'px');
	}
	if(width) {
		$('#tooltip').css("width",width+'px');
	}
	if(height) {
		$('#tooltip .content').css("height",height+'px').css('overflow','auto');
	}
	$('#tooltip').html('<div class="title ui-corner-top '+titleclass+'"><strong>'+title+'</strong>'+closebtn+'</div><div class="content ui-corner-bottom">'+content+'</div>');
	if(showclose == true ) {
		$('#tooltip .tooltipClose').css('cursor','pointer').click(function() {
			hideTooltip();
		});
	}
	$('#tooltip').fadeIn("normal");
}
function hideTooltip() {
	$('#tooltip').fadeOut("normal");
}


// build AQI info helper content
function tooltipAQI(key) {
	var title = new Array();
	title['good'] =                'Good';
	title['moderate'] =            'Moderate';
	title['unhealthy_sensitive'] = 'Unhealthy for Sensitive Groups';
	title['unhealthy'] =           'Unhealthy';
	title['very_unhealthy'] =      'Very Unhealthy';
	title['hazardous'] =           'Hazardous';

	var alignment = new Array();
	alignment['good'] =                'left';
	alignment['moderate'] =            'left';
	alignment['unhealthy_sensitive'] = 'left';
	alignment['unhealthy'] =           'left';
	alignment['very_unhealthy'] =      'right';
	alignment['hazardous'] =           'right';

	var content = new Array();
	content['good'] =                '<div><strong>Numerical Value: </strong>0-50</div><div>Air quality is considered satisfactory, and air pollution poses little or no risk.</div>';
	content['moderate'] =            '<div><strong>Numerical Value: </strong>51-100</div><div>Air quality is acceptable; however, for some pollutants there may be a moderate health concern for a very small number of people who are unusually sensitive to air pollution.</div>';
	content['unhealthy_sensitive'] = '<div><strong>Numerical Value: </strong>101-150</div><div>Members of sensitive groups may experience health effects. The general public is not likely to be affected.</div>';
	content['unhealthy'] =           '<div><strong>Numerical Value: </strong>151-200</div><div>Everyone may begin to experience health effects; members of sensitive groups may experience more serious health effects.</div>';
	content['very_unhealthy'] =      '<div><strong>Numerical Value: </strong>201-300</div><div>Health alert: everyone may experience more serious health effects.</div>';
	content['hazardous'] =           '<div><strong>Numerical Value: </strong>301-500</div><div>Health warnings of emergency conditions. The entire population is more likely to be affected.</div>';

	var offset_x;
	if(alignment[key] == 'left') {
		offset_x = 5;
	} else {
		offset_x = 175;
	}

	makeTooltip(title[key],content[key],key,true,'','','','',offset_x,21);
}

// Animate AQI meters
/*
$.fn.meterIntro = function() {
	var self = $(this).attr('id');
	var mynum = $('#'+self+' .number').html();
	$('#'+self+' .pointer').css('left','0px').animate({
		left: mynum,
	},2000,'swing');
}
*/

// run these scripts when DOM is ready
$(document).ready(function() {
	alphaBackgrounds('.alphaPNG');
	$('.meterbar .segment').hover(
		function(){
			var myparent = $(this).parent().attr('id');
			var mylevel = $(this).attr('class').replace('segment ','');
			stimer = setTimeout(function() {
				tooltipAQI(mylevel);
			},500);
		}, // hover over
		function() {
			var myparent = $(this).parent().attr('id');
			clearTimeout(stimer);
			htimer = setTimeout(function() {
				hideTooltip();
			},200);
		} // hover exit
	);//hover

	$('a[rel=modal-dialog]').attr('rel',function() {
		return $(this).attr('href');
	}).removeAttr('href').css('cursor','pointer').click(
		function() {
			var myurl = $(this).attr('rel')+'?simple=true';
			var mytitle = $(this).attr('title');
			$("#dialog").html('').dialog('destroy').dialog({
					width: 760,
					height: 480,
					modal: true,
					draggable: false,
					resizable: false,
					title: mytitle
				}).dialog('open').load(myurl);
		}
	); //modal-dialog click


	$(".helplink").css('cursor','pointer').click(
		function(e){
			var myid = $(this).attr('id').replace('_helplink','_help');
			var myx = e.pageX;
			var myy = e.pageY;
			var mycontent = $('#'+myid).html();
			makeTooltip('Form Help',mycontent,'title'   ,false ,myx ,myy,300  ,400   ,null    ,null    ,true);
		//makeTooltip(title      ,content  ,titleclass,follow,left,top,width,height,offset_x,offset_y,showclose,showEffect)
	});

	$(".accordionList h4").removeClass('ui-state-active');
	$(".accordionList").accordion({
		header: "h4",
		clearStyle: true,
		autoHeight: false,
		active: false,
		alwaysOpen: false
	});
	$("#SEPSubmissionForm input:[name=project_name]").append('Hello');
});
