jQuery(document).ready(function() {


	jQuery('ul#nav li.parent').children('a').removeAttr('href');

	jQuery('#narrow-by-list dd').hide().each(function(index) { 
		if (index < 4) jQuery(this).show();
	});
	jQuery('#narrow-by-list dt').each(function(index) {
		if (index < 4) jQuery(this).addClass('on');
	});
	jQuery('#narrow-by-list dt').click(function() {
		if (jQuery(this).hasClass('on')) {
			jQuery(this).removeClass('on');
		} else {
			jQuery(this).addClass('on');
		}
		jQuery(this).next('dd').toggle();
	});
	
	
	jQuery("ul#nav li.level0").addClass('hover-intent').hoverIntent({
		interval: 50, // milliseconds delay before onMouseOver
		over: drops_show, 
		timeout: 300, // milliseconds delay before onMouseOut
		out: drops_hide
	});

	var childData   = function(selector, arg){
	    return selector.find(arg).attr('data');
	}
	
	function createXmlDOMObject(xmlString) {
        var xmlDoc = null;

        if( ! window.DOMParser )
        {
            // the xml string cannot be directly manipulated by browsers 
            // such as Internet Explorer because they rely on an external 
            // DOM parsing framework...
            // create and load an XML document object through the DOM 
            // ActiveXObject that it can deal with
            xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
            xmlDoc.async = false;
            xmlDoc.loadXML( xmlString );
        }
        else
        {
            // the current browser is capable of creating its own DOM parser
            parser = new DOMParser();
            xmlDoc = parser.parseFromString( xmlString, "text/xml" ) ;
        }

        return xmlDoc;
    }
	
	
	var rainyconditions = new Array(
		'Rain',
		'Chance of Rain',
		'Showers',
		'Scattered Showers',
		'Isolated Thunderstorms',
		'Scattered Thunderstorms',
		'Thunderstorm',
		'Drizzle',
		'Snow Showers',
		'Chance of Snow',
		'Light snow',
		'Rain and Snow'
	);
	
	var sendAjax =function(where,loadDefaultOnFail){

		jQuery('#weather-loader').show();
		jQuery('#weather-forecast, #weather-error').hide();
		jQuery('.rainy').removeClass('rainy');
		jQuery.ajax({
			type: "GET",
			data:"where="+where,
			dataType: (jQuery.browser.msie) ? "text" : "xml",
			url: "/google-weather.php",
			success: function(data,status){
			
				if (typeof data == "string") {
				   xml = new ActiveXObject("Microsoft.XMLDOM");
				   xml.async = false;
				   xml.loadXML(data);
				 } else {
				   xml = data;
				 }
				
				forecast    =  jQuery(xml).find('forecast_information');

				var city = childData(forecast, 'city');
				
				var rainyflag = false;
			   
				if(city != undefined)
				{
					//current conditions
					cCondition  =  jQuery(xml).find('current_conditions');
									
					var date         = childData(forecast, 'forecast_date');
					var condition    = childData(cCondition, 'condition');
					var tempF        = childData(cCondition, 'temp_f');
					var humidity     = childData(cCondition, 'humidity');
					var wind         = childData(cCondition, 'wind_condition');
					var icon         = childData(cCondition, 'icon');
			
					jQuery('#city').text(city);
					jQuery('#date').text(date);
					jQuery('#condition').text(condition);
					jQuery('#tempF').text(tempF+' F');
					jQuery('#wind').text(wind);
					jQuery('#humidity').text(humidity);
					jQuery('#icon').attr({'src':'http://www.google.com'+icon});
					jQuery('#current-weather-icon > .weather-overlay').attr('title',condition);
					
					if (jQuery.inArray(condition, rainyconditions) >= 0) {
						rainyflag = true;
						jQuery('#current-weather-icon > .weather-overlay').addClass('rainy');
					}
					
					//forecast
					forecastNum    = jQuery(xml).find('forecast_conditions').size();  
					
					 for(var i = 0; i < forecastNum; i++){

							var condition = jQuery(xml).find('forecast_conditions:eq(' + i + ')');
							
							var icon  = childData(condition, 'icon');
							var day = childData(condition, 'day_of_week');
							var high = childData(condition, 'high');
							var low = childData(condition, 'low');
							var condition = childData(condition, 'condition');

							jQuery('#icon' + (i+1)).attr({'src':'http://www.google.com'+icon});
							jQuery('#day-of-week' + (i+1)).text(day);
							jQuery('#high' + (i+1)).text(high);
							jQuery('#low' + (i+1)).text(low);
							jQuery('#icon' + (i+1)).parents('.weather-icon').children('.weather-overlay').attr({'title':condition});
							
							if (jQuery.inArray(condition, rainyconditions) >= 0) {
								rainyflag = true;
								jQuery('#icon' + (i+1)).parents('.weather-icon').children('.weather-overlay').addClass('rainy');
							}
							
							
						}

					if (rainyflag) {
						jQuery('#weather-ad-notrainy').hide();
						jQuery('#weather-ad-rainy').show();
					} else {
						jQuery('#weather-ad-rainy').hide();
						jQuery('#weather-ad-notrainy').show();
					}
					jQuery('#weather-loader').hide();
					jQuery('#weather-forecast').show('fast');
		 
				}
				else
				{
					if (loadDefaultOnFail) {
					
						sendAjax(97213,false);
					} else {
						jQuery('#weather-loader').hide();
						jQuery('#weather-error').show('fast');
					}
				}
	        
		    }
	 
		 }); 
	}
	
	
	jQuery('#weather-city').submit(function(){
		where  = jQuery('#weather-place').val();
		jQuery.cookie('zip-weather',where);
		sendAjax(where,false);
		return false;
	});
	
	jQuery('#weather-place').click(function () {
		jQuery(this).val('');
	});
	
	jQuery('#change-city').removeAttr('href').click(function() {
		jQuery('#weather-city').show('fast');
	});
	
	jQuery('#weather-city').hide();
	
	var where = '';
	
	if (jQuery.cookie('zip-weather')) {
		where = jQuery.cookie('zip-weather');
		jQuery('#weather-place').val(where);
		sendAjax(where,true);
	} else if (jQuery('#weather-user-zip').attr('value') != '') {
		where =  jQuery('#weather-user-zip').attr('value');
		jQuery('#weather-place').val(where);
		sendAjax(where,true);
	} else {
		where = '97213';
		jQuery('#weather-place').val(where);
		sendAjax(where,false);
	}
	
});

function toggleMenu(el, over) { }

function drops_show(){ 
	jQuery('ul#nav li').removeClass('over');
	jQuery('ul#nav li ul.level0').css('left','-10000px');
	if (jQuery(this).attr('id') == 'weather') {
		jQuery(this).children('ul.level0').css('left','-291px');
	} else {
		jQuery(this).children('ul.level0').css('left','0');
	}
	jQuery(this).addClass('over');
}

function drops_hide(){ 
	jQuery(this).children('ul.level0').css('left','-10000px');
	jQuery(this).removeClass('over');
}
