var month = 11; // Month being displayed
var year = 1951; // year being displayed

// current date e.g. today
var current_year = 1951;
var current_month = 11;
var current_day = 10;
var current_claim = -1;
var current_logcast = '';

function initialize(y, m, d, c) {
    current_year = year = y;
    current_month = month = m;
    current_day = d;
    current_claim = c;

    calendarUpdate();
    calendarLoadData(year, month);
    calendarLoadData(year, month+1);
}

function initializeLogcast() {
    window.setTimeout('updateLogcast()', 2000);
}

function updateLogcast() {
    new Ajax.PeriodicalUpdater('logcast', '/Logger.py', {
        method: 'get', frequency: 3, decay: 1.5
    });
}

function initializeTags() {
    var tag_links = document.getElementsByClassName('suggested-tag');
    var tags = getTags();

    for (var x=0; x<tag_links.length; x++) {
        var suggested_tag = tag_links[x];
        suggested_tag.onclick = toggleTag;
        suggested_tag.cleanWhitespace();
        var tag_value = suggested_tag.innerHTML;

        if (hasTag(tag_value)) {
            suggested_tag.toggleClassName('suggested-tag-selected');
        }
    }
}

function getTags() {
    var all_tags = document.getElementById('tags').value.split(' ');
    var tags = new Array();

    for (var x=0; x<all_tags.length; x++) {
        if (all_tags[x]) {
            tags[tags.length] = all_tags[x];
        }
    }

    return tags;
}

function hasTag(tag) {
    var tags = getTags();
    for (var y=0; y<tags.length; y++) {
        if (tag.toLowerCase() == tags[y].toLowerCase()) {
            return 1;
        }
    }
    return 0;
} 

function addTag(tag) {
    var new_tags = '';
    var tags = getTags();

    for (var x=0; x<tags.length; x++) {
        new_tags += (tags[x] + ' ');
    }

    new_tags += tag + ' ';

    document.getElementById('tags').value = new_tags;
}

function removeTag(tag) {
    var new_tags = '';
    var tags = getTags();
    
    for (var x=0; x<tags.length; x++) {
        if (tags[x].toLowerCase() != tag.toLowerCase()) {
            new_tags += (tags[x] + ' ');
        }
    }

    document.getElementById('tags').value = new_tags;
}

function toggleTag(e) {
    var target;
    if (e.target) target = e.target;
    if (e.srcElement) target = e.srcElement;
    $(target).cleanWhitespace();
    var tag = target.innerHTML;
   
    if (hasTag(tag)) {
        target.toggleClassName('suggested-tag-selected');
        removeTag(tag);
    } else {
        target.toggleClassName('suggested-tag-selected');
        addTag(tag);
    }
}

function embedVideo(container, video_id) {

  while($(container).firstChild) {
      $(container).removeChild($(container).firstChild);
  }

  var swf = "http://www.youtube.com/v/" + video_id;
  var id = "movie";
  var height = "425";
  var width = "350";
  var version = "6";
  var bgcolor = "#FFFFFF";

  var so = new SWFObject(swf, id, height, width, version, bgcolor);
  so.addParam("movie",swf);
  so.addParam("wmode","transparent");
  so.write(container);

  // html = ''
  // html += '<object width="425" height="350">';
  // html += '  <param name="movie" value="http://www.youtube.com/v/' + video_id + '"></param>';
  // html += '  <param name="wmode" value="transparent"></param>';
  // html += '  <embed src="http://www.youtube.com/v/' + video_id + '" type="application/x-shockwave-flash"';
  // html += '         wmode="transparent" width="425" height="350"></embed>';
  // html += '</object>';

  // $(container).update(html);
}

function setFocus(id) {
    $(id).focus();
}

function toggleTags() {
    var x = 0;
    do {
        x++;
        var id = 'tag-' + x;
        var tag = document.getElementById(id);

        if (tag) {
            $(tag).show();
        }
    } while(tag);

    $('tags-more').hide();
}

function loginBox() {
    $('login-box-content').hide(); 
    $('login-box').show();
    
    new Effect.Appear('login-box-content');
    window.setTimeout('setFocus(\'login-username\')',1000);
}

function searchYouTube() {
    var query = document.getElementById('video-search').value;
    if (query) {
        $('youtube-video').hide();
        $('youtube-search-results').hide();
        $('youtube-searching').show();

        new Ajax.Request('/ajax_youtube_search.py', {
            parameters:{'query':query},
            onSuccess:function(t) {
                $('youtube-searching').hide();

                var results = $('youtube-search-results');
                var videos = t.responseText.split('\n');

                results.show();
                while(results.firstChild) {
                    results.removeChild(results.firstChild);
                }

                for(var x=0; x<videos.length-1; x++) {
                    var video = videos[x];
                    var parts = video.split('\t');
                    var title = parts[0];
                    var thumbnail = parts[1];
                    var url = parts[2];
                    var author = parts[3];
                    var rating = parts[4];
                    var id = parts[5];

                    var entry = document.createElement('div');
                    entry.className = 'youtube-result';
                    entry.id = id;
                    entry.title = url;

                    entry.onmouseover = function() { hilight(this); };
                    entry.onmouseout = function() { unhilight(this) };
                    entry.onclick = function() { setVideo(this) };

                    var div_title = document.createElement('div');
                    div_title.className = 'youtube-result-title';
                    div_title.appendChild(document.createTextNode(title));

                    var div_author = document.createElement('div');
                    div_author.className = 'youtube-result-author';
                    div_author.appendChild(document.createTextNode('by ' + author));

                    var div_rating = document.createElement('div');
                    div_rating.className = 'youtube-result-rating';
                    div_rating.appendChild(document.createTextNode('rating ' + rating + '/5.0'));

                    var img = document.createElement('img');
                    img.setAttribute('src',thumbnail);
                    img.setAttribute('height',97);
                    img.setAttribute('width',130);

                    var clear = document.createElement('div');
                    clear.style.clear = 'both';

                    entry.appendChild(img);
                    entry.appendChild(div_title);
                    entry.appendChild(div_author);
                    entry.appendChild(div_rating);
                    entry.appendChild(clear);
                    results.appendChild(entry);
                }

            }
        });
    }
}

function hilight(item) {
    item.style.backgroundColor = '#CCF';
}

function unhilight(item) {
    item.style.backgroundColor = '';
}

function setVideo(entry) {
   $('video-url').value = entry.title;
   $('youtube-search-results').hide();
   $('video-search').value = '';
   embedVideo('youtube-video',entry.id); 
   $('youtube-video').show();
}

function checkKey(event) {
    if (window.event) {
        // IE
        keynum = event.keyCode;
    } else if (event.which) {
        keynum = event.which;
    }

    if (keynum == 13) {
        searchYouTube();
        return false;
    }

    return true;
}

