/*

Shows weather forecast in element.
Requires gwproxy.php to be present on webserver
container is a jQuery element or selector
locale and location are strings.
location could be whatever google accepts like 
'New York' or ',,,22500000,31000000' to get weather for lat=22.5 and long=31

*/
function strstr (haystack, needle, bool) {
    var pos = 0;
    haystack += '';
    pos = haystack.indexOf( needle );
	
    if (pos == -1) {
        return false;
    } else{
        if (bool){
            return haystack.substr( 0, pos );
        } else{
			amp = haystack.indexOf('&');
			if(amp>pos){
            	return haystack.slice( pos, amp );
			}else{
				return haystack.slice( pos );
			}//if(amp>1){
        }//if (bool){
    }//if (pos == -1) {
	
}//function strstr

function showWeather(container,location,locale){
	$j(container).html('<img vspace="5" hspace="5" class="preloader" src="/sites/piran_2011/imgs/ajax-preloader.gif" />');
	$j.get( '/sites/piran_2011/modules/utils/gw.php?weather=' + encodeURI(location) + '&hl='+encodeURI(locale), function(xml) {
		
		function addForecastDiv( day, condition, temp, icon ){
			
			daydiv = $j("<div class='day'></div>");
			if(!strstr(icon,'google')){
				icon = 'http://www.google.com/'+icon;
			}
			daydiv.append( "<div class='icon'><img src='" + icon + "' /></div>" );
			daydiv.append( "<div class='name'>" + day + "</div>" );
			daydiv.append( "<div class='condition'>" + condition + "</div>" );
			daydiv.append( "<div class='temp'>" + temp + "</div>");
			//$j('.name, .condition, .temp').wrap('<div class="data">');
			$j(container).append(daydiv);
			
		}
		$j('.preloader').hide();
		
		var t_unit = '&deg;F';
		if( $j(xml).find('forecast_information').find('unit_system').attr('data') == 'SI' )
		t_unit = '&deg;C';
		addForecastDiv(now,
			$j(xml).find('current_conditions').find('condition').attr('data') + '<br/>' + 
			$j(xml).find('current_conditions').find('humidity').attr('data') + '<br/>' + 
			$j(xml).find('current_conditions').find('wind_condition').attr('data'),
			  
			$j(xml).find('current_conditions').find('temp_c').attr('data') + '&deg;C ('+
			$j(xml).find('current_conditions').find('temp_f').attr('data') + '&deg;F) ',
			$j(xml).find('current_conditions').find('icon').attr('data')
		);

		/*$j(xml).find('forecast_conditions').each(function(index){
			console.log(index);
			if(index < 1){ //prikažemo vreme za današnji dan
				addForecastDiv(
					$j(this).find('day_of_week').attr('data'),
					$j(this).find('condition').attr('data'),
					$j(this).find('low').attr('data') + t_unit + ' - ' + 
					$j(this).find('high').attr('data') + t_unit,
					$j(this).find('icon').attr('data')
				);
			}
		});*/
	},'xml');

}

