<?
    include_once '../../../const_vars.php';
    echo 'fxw_url = \''.SITE_URL.'\';';
?>

fxw_init = function(){
    var data_script_src = fxw_url+'/tools/widget/v6/data.php?';
    var container;

    var geoid = typeof fxw_geoid == 'number' ? fxw_geoid  : 0;
    var filter_type = typeof fxw_filter_type == 'string' ? fxw_filter_type  : '';
    var filter = typeof fxw_filter == 'string' ? fxw_filter  : '';
    var story_count = typeof fxw_story_count == 'number' ? fxw_story_count : 3;
    var container_id = typeof fxw_container_id == 'string' ? fxw_container_id  : 'fxw_widget_container';
    var unstyled = typeof fxw_unstyled == 'boolean' ? fxw_unstyled : false;

    data_script_src += 'geoid='+encodeURIComponent(geoid);
    data_script_src += '&filter_type='+encodeURIComponent(filter_type);
    data_script_src += '&filter='+encodeURIComponent(filter);
    data_script_src += '&page_size='+encodeURIComponent(story_count);
    data_script_src += '&container_id='+encodeURIComponent(container_id);
    data_script_src += '&unstyled='+unstyled;

    data_script_src += '&show_summaries=' + (typeof fxw_show_summaries == 'boolean' ? fxw_show_summaries : false);
    data_script_src += '&show_images=' + (typeof fxw_show_images == 'boolean' ? fxw_show_images : true);
    data_script_src += '&zip=' + (typeof fxw_zip == 'undefined' ? '' : fxw_zip);
    
    data_script_src += '&link_color=' + encodeURIComponent((typeof fxw_link_color == 'string' ? fxw_link_color : '#679CD2'));
    data_script_src += '&widget_title='+ encodeURIComponent((typeof fxw_widget_title == 'string' ? fxw_widget_title : '{location} News'));
    data_script_src += '&bg_color=' + encodeURIComponent((typeof fxw_bg_color == 'string' && fxw_bg_color != '' ? fxw_bg_color : '#FFFFFF'));
    data_script_src += '&border_color=' + encodeURIComponent((typeof fxw_border_color == 'string' && fxw_border_color != '' ? fxw_border_color : '#E0E0E0'));
    data_script_src += '&widget_title_bg=' + encodeURIComponent((typeof fxw_widget_title_bg == 'string' && fxw_widget_title_bg != '' ? fxw_widget_title_bg : '#FFFFFF'));
    data_script_src += '&widget_title_color=' + encodeURIComponent((typeof fxw_widget_title_color == 'string' && fxw_widget_title_color != '' ? fxw_widget_title_color : '#E77A00'));
    
    data_script_src += '&story_title_color=' + encodeURIComponent((typeof fxw_story_title == 'string' && fxw_story_title != '' ? fxw_story_title : '#2067AF'));
    data_script_src += '&story_source_color=' + encodeURIComponent((typeof fxw_story_source == 'string' && fxw_story_source != '' ? fxw_story_source : '#3F9439'));
    data_script_src += '&story_metadata_color=' + encodeURIComponent((typeof fxw_story_metadata == 'string' && fxw_story_metadata != '' ? fxw_story_metadata : '#888888'));
    data_script_src += '&story_summary_color=' + encodeURIComponent((typeof fxw_story_summary == 'string' && fxw_story_summary != '' ? fxw_story_summary : '#333333'));
    
    // Create the containing div and then add a script tag for data.php
    // data.php gets story data and then calls fxw_init
    if(document.getElementById(container_id) == null)
        document.write('<div id="'+container_id+'" class="fxw_container"></div>');
    
    container = document.getElementById(container_id);
    
    if(unstyled == false) {
        style_sheet = document.createElement('link');
        style_sheet.rel = "stylesheet";
        style_sheet.type = "text/css";
        style_sheet.href = fxw_url+'/tools/widget/v6/default.css';
        container.appendChild(style_sheet);
    }

    data_script = document.createElement('script');
    data_script.type = 'text/javascript';

    data_script.src = data_script_src;
    container.appendChild(data_script);

};
fxw_init();


/*
 * FUNCTIONS TO BUILD THE HEADING
 */
function fxw_build_widget_heading(fxw_container, fxw_geo_url, data, styles){
    var heading,
        title,
        more_link;

    if(styles.bg_color && !styles.unstyled) fxw_container.style.background = styles.bg_color;

    heading = document.createElement('div');
    fxw_container.appendChild(heading);
    heading.className = 'fxw_heading';
    if(!styles.unstyled) heading.style.background = styles.widget_title_bg;

    title = document.createElement('a'); 
    heading.appendChild(title);
    title.className = 'fxw_title';
    if(!styles.unstyled) title.style.color = styles.widget_title_color;
    if(!styles.unstyled) title.style.borderColor = styles.border_color;
    title.href = fxw_geo_url;
    title.target = '_blank';
    title.innerHTML = styles.widget_title.replace('{location}', data.geodata.pretty).replace('{filter}', data.filter);

    more_link = document.createElement('a');
    heading.appendChild(more_link);
    more_link.className = 'fxw_more';
    if(!styles.unstyled) more_link.style.color = styles.link_color;
    more_link.href = fxw_geo_url;
    more_link.target = '_blank';
    more_link.innerHTML = 'more';
}


/*
 * FUNCTIONS TO BUILD THE STORY WIDGET
 */
function fxw_build_story_widget(data, styles){
    var clear,
        fxw_container,
        fxw_geo_url,
        graphic,
        i,
        image,
        item,
        metadata,
        stories,
        story,
        story_title,
        summary,
        time_ago;


    fxw_geo_url = fxw_url+'/'+data.geodata.short;
    fxw_container = document.getElementById(styles.container_id);
    fxw_build_widget_heading(fxw_container, fxw_geo_url, data, styles);

    // Stories
    stories = document.createElement('ul');
    fxw_container.appendChild(stories);
    stories.className = 'fxw_stories';

    for(i = 0; i < data.stories.length; i++){
        item = data.stories[i];
        story_link = fxw_geo_url+'/share/'+item.storyid+'/'+item.url_title;

        story = document.createElement('li');
        stories.appendChild(story);
        story.className = 'fxw_item';

        if(item.image && styles.show_images){
            graphic = document.createElement('a')
            story.appendChild(graphic);
            graphic.className = 'fxw_graphic';
            graphic.href = story_link;
            graphic.target = '_blank';
            image = document.createElement('img');
            graphic.appendChild(image);
            image.src = item.image;
        }

        story_title = document.createElement('a');
        story.appendChild(story_title);
        story_title.className = 'fxw_story_title';
        if(!styles.unstyled) story_title.style.color = styles.story_title_color;
        story_title.href = story_link;
        story_title.target = '_blank';
        story_title.innerHTML = item.title;

        metadata = document.createElement('small');
        story.appendChild(metadata);
        if(!styles.unstyled) metadata.style.color = styles.story_metadata_color;

        source = document.createElement('a');
        metadata.appendChild(source);
        source.className = 'fxw_source';
        if(!styles.unstyled) source.style.color = styles.story_source_color;
        source.href = fxw_geo_url+'/source/'+encodeURIComponent(item.pretty);
        source.innerHTML = item.pretty;
        source.target = '_blank';

        metadata.appendChild(document.createTextNode(' - '));

        time_ago = document.createElement('a');
        metadata.appendChild(time_ago);
        if(!styles.unstyled) time_ago.style.color = styles.story_metadata_color;
        ts = new Date(item.print_time * 1000);
        time_ago.href = fxw_geo_url+'/day/'+ts.getFullYear()+'-'+ts.getMonth()+1+'-'+ts.getDate();
        time_ago.innerHTML = fxw_time_ago(item.print_time);

        if(item.summary && styles.show_summaries){
            summary = document.createElement('p');
            story.appendChild(summary);
            summary.className = 'fxw_summary';
            if(!styles.unstyled) summary.style.color = styles.story_summary_color;
            summary.innerHTML = item.summary.slice(0,150);
        }
    
        clear = document.createElement('div');
        story.appendChild(clear);
        clear.style.clear = 'both';
    }
}


/*
 * FUNCTION TO BUILD A WEATHER WIDGET
 */
function fxw_build_weather_widget(data, styles){
    var clear,
        content,
        current_temp,
        day,
        day_forecast,
        dow,
        forecast,
        fxw_geo_url,
        fxw_container,
        high,
        i,
        low,
        text,
        text_forecast_container,
        weather,
        wunder_link;

    fxw_geo_url = '';
    fxw_geo_url = fxw_url+'/'+data.geodata.short;
    fxw_container = document.getElementById(styles.container_id);
    fxw_build_widget_heading(fxw_container, fxw_geo_url, data, styles);

    content = document.createElement('div');
    fxw_container.appendChild(content);
    content.className = 'fxw_weather';

    // Text weather
    if(styles.show_summaries && data.weather_text.length > 0){
        text_forecast_container = document.createElement('p');
        content.appendChild(text_forecast_container);
        text_forecast_container.className = 'fxw_text_forecast';
        if(!styles.unstyled) text_forecast_container.style.color = styles.story_summary_color;
    
        current_temp = document.createElement('div');
        text_forecast_container.appendChild(current_temp);
        current_temp.className = 'fxw_temp '+data.weather_forecast[0].icon;
        if(!styles.unstyled) current_temp.style.borderColor = styles.border_color;
        current_temp.title = data.weather_forecast[0].conditions;
    
        high = document.createElement('small');
        high.innerHTML = data.weather_forecast[0].high+'&deg;';
        current_temp.appendChild(high);
        high.className = 'fxw_high';
        if(!styles.unstyled) high.style.borderColor = styles.border_color;
        if(!styles.unstyled) high.style.color = styles.story_metadata_color;
    
        low = document.createElement('small');
        low.innerHTML = data.weather_forecast[0].low+'&deg;';
        current_temp.appendChild(low);
        low.className = 'fxw_low';
        if(!styles.unstyled) low.style.color = styles.story_metadata_color;
    
        for(i = 0; i < data.weather_text.length; i++){
            if(i != 0){
                text_forecast_container = document.createElement('p');
                content.appendChild(text_forecast_container);
                text_forecast_container.className = 'fxw_text_forecast';
                if(!styles.unstyled) text_forecast_container.style.color = styles.story_summary_color;
            }
            weather = data.weather_text[i];
            wunder_link = document.createElement('a');
            text_forecast_container.appendChild(wunder_link);
            if(!styles.unstyled) wunder_link.style.color = styles.link_color;
            wunder_link.href = 'http://wunderground.com/cgi-bin/findweather/hdfForecast?ref=fwix&query='+data.wunderground_query;
            wunder_link.target = '_blank';
            wunder_link.innerHTML = weather.title+':';
            text = ' '+ weather.body.slice(0, 100);
            if(weather.body.length > 100)
                text += '...';
            text_forecast_container.appendChild(document.createTextNode(text));
        }
    
        clear = document.createElement('div');
        content.appendChild(clear);
        clear.style.clear = 'both';
    }

    // Forecast
    forecast = document.createElement('div');
    content.appendChild(forecast);
    forecast.className = 'fxw_forecast';
    if(!styles.unstyled) forecast.style.borderColor = styles.border_color;

    for(i = 0; i < data.weather_forecast.length; i++){
        if(i == styles.story_count)
            break;
        weather = data.weather_forecast[i];
        day = document.createElement('dl');
        forecast.appendChild(day);
        if(i == 0)
            day.className = 'fxw_first';
        else if(i == styles.story_count - 1 || i == 5)
            day.className = 'fxw_last';
        day.title = weather.conditions;
        if(!styles.unstyled) day.style.borderColor = styles.border_color;
        day.style.width = (98/styles.story_count)+'%';

        dow = document.createElement('dt');
        day.appendChild(dow);
        wunder_link = document.createElement('a');
        dow.appendChild(wunder_link);
        if(!styles.unstyled) wunder_link.style.color = styles.link_color;
        wunder_link.href = 'http://wunderground.com/cgi-bin/findweather/hdfForecast?ref=fwix&query='+data.wunderground_query;
        wunder_link.target = '_blank';
        wunder_link.innerHTML = weather.dow.slice(0,3);

        day_forecast = document.createElement('dd');
        day.appendChild(day_forecast);
        day_forecast.className = 'fxw_day_forecast '+weather.icon;
        day_forecast.innerHTML = weather.conditions;

        high = document.createElement('dd');
        day.appendChild(high);
        high.className = 'fxw_high';
        if(!styles.unstyled) high.style.borderColor = styles.border_color;
        if(!styles.unstyled) high.style.color = styles.story_metadata_color;
        high.innerHTML = weather.high+'&deg;';

        low = document.createElement('dd');
        day.appendChild(low);
        low.className = 'fxw_low';
        if(!styles.unstyled) low.style.color = styles.story_metadata_color;
        low.innerHTML = weather.low+'&deg;';
    }

    clear = document.createElement('div');
    forecast.appendChild(clear);
    clear.style.clear = 'both';
}


/*
 * FUNCTIONS TO BUILD THE HEADING
 */
function fxw_build_photo_widget(data, styles){
    var clear,
        fxw_container,
        fxw_geo_url,
        i,
        image,
        image_link,
        item,
        photos;


    fxw_geo_url = fxw_url+'/'+data.geodata.short;
    fxw_container = document.getElementById(styles.container_id);
    fxw_build_widget_heading(fxw_container, fxw_geo_url, data, styles);

    photos = document.createElement('ul');
    fxw_container.appendChild(photos);
    photos.className = 'fxw_photos';

    for(i = 0; i < data.images.length; i++){
        item = document.createElement('li');
        photos.appendChild(item);
        if(i%3 == 0)
            item.className = 'fxw_first';
        if(i%3 == 1)
            item.className = 'fxw_second';
        if(i%3 == 2)
            item.className = 'fxw_third';

        image_link = document.createElement('a');
        item.appendChild(image_link);
        image_link.target = '_blank';
        image_link.href = data.images[i].link;
        image_link.title = data.images[i].title;
        image = document.createElement('img');
        image_link.appendChild(image);
        image.src = data.images[i].src;
        image.alt = data.images[i].title;
    }

    clear = document.createElement('div');
    fxw_container.appendChild(clear);
    clear.style.clear = 'both';
}


/*
 * FUNCTION TO GET TIME AGO
 */
function fxw_time_ago(timestamp){
    var current_time,
        difference,
        item_time;

    current_time = new Date().getTime()/1000;
    item_time = parseFloat(timestamp);
    difference = current_time - item_time;

    if( difference < 60 ) {
        return  "Seconds ago"; 
    }else{
        difference = Math.round( difference / 60 );
        if( difference < 60 ) {
            if( difference == 1 ) 
                return difference + " min ago";
            else
                return difference + " min ago";
        }else{
            difference = Math.round( difference / 60 );
            if( difference < 24 ) {
                if( difference == 1 ) 
                    return difference + " hr ago";
                else 
                    return difference + " hrs ago";
            }else{
                difference = Math.round( difference / 24 );
                if( difference < 365 ) {
                    if( difference == 1 ) 
                        return difference + " day ago";
                    else
                        return difference + " days ago";
                }else {
                    return 'over a year ago'; 
                }
            }
        }
    }
}

