
/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL0M2SG681CQ - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: pupsnshop.com
 *	FILE: LIB.JS
 *
 *	The software is a commercial product delivered under single, non-exclusive, 
 *	non-transferable license for one domain or IP address. Therefore distribution, 
 *	sale or transfer of the file in whole or in part without permission of Flynax 
 *	respective owners is considered to be illegal and breach of Flynax License End 
 *	User Agreement. 
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2014 |  All copyrights reserved. 
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/

var wf_days = new Array();
wf_days['sun'] = 0;
wf_days['mon'] = 1;
wf_days['tue'] = 2;
wf_days['wed'] = 3;
wf_days['thu'] = 4;
wf_days['fri'] = 5;
wf_days['sat'] = 6;

$(document).ready(function(){
	if ( wf_use_geo && !wf_sess_geo )
	{
		if ( wf_ipgeo )
		{
			$.getJSON(wf_request,
				{
					mode: 'query',
					query: wf_ipgeo
				},
				function(response){
					if ( response && response['data']['woeid'] )
					{
						wf_la_woeid = response['data']['woeid'];
						
						if ( wf_la_woeid )
						{
							wf_getContent(wf_la_woeid);
							xajax_saveSessWoeid(wf_la_woeid);
						}
						else
						{
							$('#wf_block_loading').html(wf_not_found);
							return false;
						}
					}
					else
					{
						$('#wf_block_loading').html(wf_not_found);
						return false;
					}
				}
			);
		}
		else
		{
			$('#wf_block_loading').html(wf_not_found);
		}
	}
	else if ( wf_use_geo && wf_sess_geo )
	{
		wf_getContent(wf_sess_geo);
	}
	else
	{
		wf_getContent(wf_woeid);
	}
});

var wf_getContent = function(wf_woeid){
	$.getJSON(wf_request,
		{
			mode: 'wf',
			w: wf_woeid,
			u: wf_unit == 'Celsius' ? 'c' : 'f'
		},
		function(response){
			if ( !response['data']['location'] )
			{
				$('#wf_block_loading').html(wf_not_found);
				return false;
			}
			
			var data = response['data'];
			var unit = wf_unit == 'Celsius' ? 'C' : 'F';
			unit = '&deg;'+ unit;
			
			/* build title */
			var location = data['location'];
			
			if ( location['region'] != '' && location['city'] != '' )
			{
				var title = location['city'] +", "+ location['region'];
			}
			else
			{
				var title = location['city'] +", "+ location['country'];
			}
			$('#wf_block_loc').html(title);
			
			/* build current conditions */
			var cur_cond = data['condition'];
			var conditions = wf_conditions[cur_cond['code']] +', <b>'+ cur_cond['temp'] +'</b> '+ unit;
			$('#wf_block_cond div.cur_cond').html(conditions);
			
			/* set wether sign */
			if ( cur_cond['code'] != '3200' )
			{
				var image = '<img alt="'+ wf_conditions[cur_cond['code']] +'" title="'+ wf_conditions[cur_cond['code']] +'" src="'+ wf_url +'signs/'+ cur_cond['code'] +'.png" />';
				$('#wf_block_cond div.cur_img').html(image);
			}
			
			/* set forecast */
			var forecast = data['forecast'];
			var forecast_out = '<table style="font-size: 12px;">';
			
			for ( var i=0; i<forecast.length; i++ )
			{
				forecast_out += '<tr> \
									<td valign="top" style="width: 30px;text-transform: capitalize;" title="'+ forecast[i]['date'] +'">'+ $.datepicker.regional[wf_lang_code].dayNamesShort[wf_days[forecast[i]['day'].toLowerCase()]] +'</td> \
									<td valign="top"> \
										<b>'+ wf_conditions[forecast[i]['code']] +'</b> \
										<div style="font-size: 11px;padding: 0 0 5px 0;"> \
										'+wf_high+' '+ forecast[i]['high'] +' '+ unit +', '+wf_low+' '+ forecast[i]['low'] +' '+ unit +' \
										</div> \
									</td> \
								</tr>';
			}
			forecast_out += '</table>';
			
			$('#wf_block_forecast').html(forecast_out);
			
			/* open block */
			$('#wf_block_loading').fadeOut('normal', function(){
				$('#wf_block_data').fadeIn('normal');
			});
		}
	);
}