﻿
var common = new Object();
String.prototype.trim = function() { return this.replace(/^[\s]+|[\s]+$/,'');};
common.init = function() {
    $('.hidden-button').css('display', 'none');
    // set up behavior, and content of search text field
    $('.section-top .search .text').bind('keypress', common.checkSearchSubmit);
    $('.section-top .search .text').attr('value', resources.searchPhrase).bind('focus', null, function(e) { if ($(this).attr('value') == resources.searchPhrase) { $(this).attr('value', '') } }).bind('blur', null, function() { if ($(this).attr('value') == '') { $(this).attr('value', resources.searchPhrase) } });
    // home menu item hover
    $('.section-nav ul .home a')
        .bind('mouseover', null, function(e) { $(this).addClass('over').removeClass('out'); })
        .bind('mouseout', null, function(e) { $(this).addClass('out').removeClass('over'); })
    // adjust top menu in some fancy way
    common.adjustTopMenu();
}

common.adjustTopMenu = function() {
    var totItemsCount = $('.section-nav ul li').length;
    var mnuWidth = $('.section-nav ul').width() - (totItemsCount * 2); // some safety margin
    var totItemsWidth = 0;
    var TOT_BORDER_WIDTH = 2;
    $('.section-nav ul li').each( // measure the full width of current
            function(index) {
                totItemsWidth += $(this).width() + (parseInt($(this).css('padding-left')) * 2) + TOT_BORDER_WIDTH;
            }
    );
    var addWidth = parseInt(((mnuWidth - totItemsWidth) / totItemsCount) / 2);
    $('.section-nav .horiznav').css('display','none');
    if (addWidth != 0) {
        $('.section-nav ul li').each(
            function(index) {
                var currPadding = parseInt($(this).css('padding-left'));
                $(this).css('padding-left', (addWidth + currPadding) + 'px').css('padding-right', (addWidth + currPadding) + 'px');
            }
        );
    }
    $('.section-nav .horiznav').css('display','');
}

common.checkSearchSubmit = function(e) {
    var ENTER = 13;
    if (e.keyCode == ENTER) {
        return true;
    }
}

common.startPageInit = function() {
    $('.teaser a.inner').bind('click', function(e) {
        var strAttr = $(this).attr('href');
        window.location.href = strAttr;
        return false;
    });

    $('.teaser a.inner')
        .bind('mouseover', null, function(e) { $(this).children('.image').children('img.over').css('display', ''); })
        .bind('mouseout', null, function(e) { $('.teaser .image .over').css('display', 'none'); });
    $('.image-container').cycle({timeout:3000,speed:1500});
}

common.listPageInit = function() {
    //$('#ChooseYearSelectBox').bind('change', function(e) { alert('must be changed to some kind of behavior'); });
}

var archive=new Object();
archive.years = [];
archive.addYear=function(strDate) {
    var strYear = strDate.trim().substring(0, 4);
    for(var i=0;i<archive.years.length;i++) {
        if(archive.years[i]==strYear) {
            return;
        }
    }
    $.merge(archive.years, [strYear]);
}
archive.initSelectBox = function() {
    var strHtml = '';
    var strYearCurrent = new Date().getFullYear().toString().trim();
    strYearCurrent = (strYearCurrent.length==2)?"20"+strYearCurrent:strYearCurrent;
    for(var i=0;i<archive.years.length;i++) {
        strHtml += '<option '+(strYearCurrent==archive.years[i]?'selected="selected"':'')+' value="'+archive.years[i]+'">'+archive.years[i]+'</option>';
    }
    $('#ChooseYearSelectBox').append(strHtml).bind('change', 
        function(e) {
            var strYear = $(this).val()+'';
            $('.list-container .item').each(
                function(counter) {
                    //alert(strYear=='');
                    $(this).css('display',( $(this).children('.date').text().trim().substring(0,4)==strYear || strYear=='')?'':'none');
                }
            );
            return false;
        }
    ).css('display','').trigger('change'); // trigger it to be THIS year
    archive.years = null; // dont need to waste mem on this anymore    
}
archive.fixSort = function() {
    // need to sort them, if reddot spits them out wrong
    var objRoot = $('.list-container');
    var items = objRoot.children('.item').get();
    items.sort(function(a, b) {
       var compA = $(a).children('.date').text();
       var compB = $(b).children('.date').text();
        // backwards, latest first       
       return (compA > compB) ? -1 : (compA < compB) ? 1 : 0; //return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
    });
    objRoot.html(items);
}
archive.init=function() {
    if($('#ChooseYearSelectBox').length==0) return;
    archive.fixSort();
    $('.list-container .item .date').each(
        function(counter) {
            archive.addYear($(this).text());
        }
    );
    archive.initSelectBox();
}
$(document).ready(function() {
    common.init();
    archive.init();
});
