/* change featured header */
FeaturedHeader = function(){
	this.first = false;
	this.previousId = false;
    this.ids=new Array();
    this.counter=0;
    this.timeout=false;
	
    this.change = function(featuredId) {
        this._change(featuredId);
        if (this.timeout){
            clearTimeout(this.timeout);
        }
    }
	
    this._change = function(featuredId) {
        var currentId = false; 
        
        if (featuredId){
            currentId=featuredId;
        } else if (this.first) {
            currentId=this.first;
        }
        
        if (currentId && currentId != this.previousId){
            if (this.previousId){
                $('#'+this.previousId+'_link').toggleClass('selected');
            }
            this.previousId=currentId;
            $('#'+currentId+'_link').toggleClass('selected');
        
            var selectorPrefix='#' + currentId;
            var headline=$(selectorPrefix+ ' h1').html();
            var description=$(selectorPrefix+ ' h2').html();
            var title=$(selectorPrefix+ ' h3').html();
            var caption=$(selectorPrefix+ ' h4').html();
            var href=$(selectorPrefix+ '_link').attr('href');
            var src=$(selectorPrefix+ '_link img').attr('src');
            var wrapped=$('#featured_header');
            wrapped.css('background-image', 'url('+src+')');
            wrapped.click(function(){
                window.location=href;
            });
            $('#featured_header h2:first').html(headline);
            $('#featured_header p:first').html(caption + '<br />' +title + '<br />' + description);
            $('#featured_header a:first').attr('href', href);

            var optionalTitleImage=$(selectorPrefix+ ' h5');
            if (optionalTitleImage.length==1){
                var optionalTitleImageUrl=optionalTitleImage.html();
                if (optionalTitleImageUrl){
                    var wrappedImage=$('#featured_header img.big_title');
                    wrappedImage.attr('src', optionalTitleImageUrl);
                    wrappedImage.css('visibility','visible');
                }
            }
        }
    }
	this.put = function(featuredId) {
        if (!this.first){
            this.first=featuredId;
            this.ids=new Array();
        }
        
        this.ids.push(featuredId);
    }
    
    this.next = function() {
        this._change(this.ids[++this.counter%this.ids.length]);
    }
    
    this.carousel = function(){
        var instance=this;
        this.timeout=setTimeout(function(){
                    instance.next();
                    instance.carousel();
                   }, 10000
                  );
    };
}
var featuredHeader=new FeaturedHeader();
$(document).ready(
    function(){
        featuredHeader.carousel();
    }
);

$(document).ready(
    function(){
        var clone=$('#header_placeholder');
        if ( clone.length>0 ){
            var replaceMe=$('div.main_area:first');
            if ($.browser.opera || $.browser.safari || $.browser.msie){//ugly workaround for firefox bug
                replaceMe.replaceWith(clone);
            } else {
                var html=clone.html();
                replaceMe.replaceWith(clone);
                replaceMe.html(html);
            }
            clone.css("visibility","visible");
        }
    }
);

/* countdown util */
function CountDown( secondsFrom1970 ) {
	// init target time
	var target			= new Date();
    target.setTime(secondsFrom1970*1000);
	this.targetTime		= target.getTime();
	/**
	 * refresh countdown
	 */
	this.refresh = function() {
		var today				 = new Date();
		var currentTime		   = today.getTime();
		// time left
		this._leftMilliseconds	= (this.targetTime - currentTime);
		this._leftSeconds		 = Math.floor( this._leftMilliseconds / 1000 );
		this._leftMinutes		 = Math.floor( this._leftSeconds / 60 );
		this._leftHours		   = Math.floor( this._leftMinutes / 60 );
		// no module
		this.leftDays			 = Math.floor( this._leftHours / 24 );
		// for print
		this.leftMilliseconds	 = this._leftMilliseconds % 1000;
		this.leftSeconds		  = this._leftSeconds % 60;
		this.leftMinutes		  = this._leftMinutes % 60;
		this.leftHours			= this._leftHours % 24;
	}
	this.refresh();
}


/* ellipsis (autotruncate) extension */
(function($) {
	$.fn.ellipsis = function(enableUpdating){
		var s = document.documentElement.style;
		if (!('textOverflow' in s || 'OTextOverflow' in s)) {
			return this.each(function(){
				var el = $(this);
				if(el.css("overflow") == "hidden"){
					var originalText = el.html();
					var w = el.width();
					
					var t = $(this.cloneNode(true)).hide().css({
                        'position': 'absolute',
                        'width': 'auto',
                        'overflow': 'visible',
                        'max-width': 'inherit'
                    });
					el.after(t);
					
					var text = originalText;
					while(text.length > 0 && t.width() > el.width()){
						text = text.substr(0, text.length - 1);
						t.html(text + "...");
					}
					el.html(t.html());
					
					t.remove();
					
					if(enableUpdating == true){
						var oldW = el.width();
						setInterval(function(){
							if(el.width() != oldW){
								oldW = el.width();
								el.html(originalText);
								el.ellipsis();
							}
						}, 200);
					}
				}
			});
		} else return this;
	};
})(jQuery);