﻿
$(document).ready(function()
{
    //init tabs
    $('.tabs').tabify();
    $('.destination-tabs').tabify();

    //init accordion
    initAccordion();
});

function loadWeatherWidget(txt)
{
    //var template = document.createElement('div');
    $('.destinations-page-weather table tr:first td:not(:first)').html(txt);
    //$(template).load('/CMSTemplates/VivaTravelASPX/templates/destinations/weather.html', function(html){ parseWeatherResponse(json, html) });
}

function parseWeatherResponse(json, html)
{
    if(json != null)
    {
        var clouds = json.weatherObservation.clouds;
        var weather = json.weatherObservation.weatherCondition;
        var temperature = json.weatherObservation.temperature;
        var humidity = json.weatherObservation.humidity;
        var weatherIcon = getWeatherIcon(clouds, weather);
        var conditions = weather;
        
        if (weather == 'n/a')
        {
            if (clouds == 'n/a')
            {
                conditions = 'fine';
            }
            else
            {
                conditions = clouds;
            }
        }
        
        html = html.replace('{conditions_img}', '<img id="weather_img" src="/cmstemplates/vivatravelaspx/images/weather_images/' + weatherIcon + '" />')
                   .replace('{temp}', temperature)
                   .replace('{temp_type}','C')
                   .replace('{humidity}',humidity);
        
        $('.destinations-page-weather table tr:first td:not(:first)').html(html);
    }
    else
    {
        $('.destinations-page-weather table tr:first td:not(:first)').html('-');
    }
}

function getWeatherIcon(clouds, weather)
{
    var weatherimage = '';

    if (weather == 'n/a')
    {
        switch (clouds)
        {
            case 'n/a':
                weatherimage = 'sunny.gif';
                break;
            case 'clear sky':
                weatherimage = 'sunny.gif';
                break;
            case 'few clouds':
                weatherimage = 'partly_cloudy.gif';
                break;
            case 'scattered clouds':
                weatherimage = 'partly_cloudy.gif';
                break;
            case 'broken clouds':
                weatherimage = 'partly_cloudy.gif';
                break;
            default:
                weatherimage = 'cloudy.gif';
                break;
        }
    }
    else {
        weather = weather.replace('light ', '').replace('heavy ', '').replace(' in vicinity', '');

        switch(weather)
        {
            case 'drizzle':
	            weatherimage = 'rain.gif';
                break;
            case 'rain':
	            weatherimage = 'rain.gif';
                break;
            case 'snow':
	            weatherimage = 'snow.gif';
                break;
            case 'snow grains':
                weatherimage = 'sleet.gif';
                break;
            case 'ice crystals':
	            weatherimage = 'icy.gif';
                break;
            case 'ice pellets':
	            weatherimage = 'icy.gif';
                break;
            case 'hail':
	            weatherimage = 'sleet.gif';
                break;
            case 'small hail':
	            weatherimage = 'sleet.gif';
                break;
            case 'snow pellets':
	            weatherimage = 'sleet.gif';
                break;
            case 'unknown precipitation':
	            weatherimage = 'rain.gif';
                break;
            case 'mist':
	            weatherimage = 'mist.gif';
                break;
            case 'fog':
	            weatherimage = 'fog.gif';
                break;
            case 'smoke':
	            weatherimage = 'smoke.gif';
                break;
            case 'volcanic ash':
	            weatherimage = 'smoke.gif';
                break;
            case 'sand':
	            weatherimage = 'dust.gif';
                break;
            case 'haze':
	            weatherimage = 'haze.gif';
                break;
            case 'spray':
	            weatherimage = 'rain.gif';
                break;
            case 'widespread dust':
	            weatherimage = 'dust.gif';
                break;
            case 'squall':
	            weatherimage = 'flurries.gif';
                break;
            case 'sandstorm':
	            weatherimage = 'dust.gif';
                break;
            case 'duststorm':
	            weatherimage = 'dust.gif';
                break;
            case 'well developed dust':
	            weatherimage = 'dust.gif';
                break;
            case 'sand whirls':
	            weatherimage = 'dust.gif';
                break;
            case 'funnel cloud':
	            weatherimage = 'flurries.gif';
                break;
            case 'tornado':
	            weatherimage = 'storm.gif';
                break;
            case 'waterspout':
	            weatherimage = 'storm.gif';
                break;
            case 'showers':
	            weatherimage = 'storm.gif';
                break;
            case 'thunderstorm':
	            weatherimage = 'thunderstorm.gif';
                break;
            default:
	            if (weather.indexOf('rain') > -1)
                {
		            weatherimage = 'rain.gif';
	            }
                else if (weather.indexOf('snow') > -1)
                {
		            weatherimage = 'snow.gif';
	            }
                else if (weather.indexOf('thunder') > -1)
                {
		            weatherimage = 'thunderstorm.gif';
	            }
                else if (weather.indexOf('dust') > -1)
                {
		            weatherimage = 'dust.gif';
	            }
                else
                {
		            weatherimage = 'sunny.gif';
	            }
                break;
        }
    }

    return weatherimage;
}
