// Compatability with jQuery:
//jQuery.noConflict();

window.$ = function (element) {
	return document.getElementById(element);
};

// http://www.quirksmode.org/js/cookies.html
function readCookie(name) {
    var
    nameEQ = name + "=",
    ca = document.cookie.split(';'),
    c, i;

    for (i = 0; i < ca.length; i++) {
        c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }

    return null;
}



jQuery(function ($) {
    if ($('#action_billboard').length) {
        if ($.browser.msie && parseInt($.browser.version) <= 6) {
            $('#action_billboard .button').mouseover(function() {
                $(this).addClass('hover');
            }).mouseout(function() {
                $(this).removeClass('hover');
            });
        }
    }

    /* Header and footer JavaScript */
    if ($.browser.msie && parseInt($.browser.version) <= 6) {
        $('.drawing').each(function() {
            var bg = $(this).css('background-image');
            if (bg.indexOf('.png') != -1)
                $(this).css('background-image', 'none').get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bg.split('url("')[1].split('")')[0] + "', sizingMethod='scale')";
        });
    }
	
	var sform = $('#searchform'),
	ss = sform.children('input'),
	sb = sform.children('button'),
	defSearchVal = ss.attr('title');
	ss.attr('title', '');
	if (ss.val() == '') ss.val(defSearchVal);
	else if (ss.val() != defSearchVal) {
		ss.addClass('active');
		sb.addClass('visible');
	}
	ss.focus(function() {
		if (ss.val() == defSearchVal) ss.val('').addClass('active');
	}).blur(function() {
		if (ss.val() == '') ss.val(defSearchVal).removeClass('active');
	}).keydown(function(e) {
		if (e.which == 27) ss.val('');
	}).keyup(function() {
		if (ss.val() != '') sb.addClass('visible');
		else sb.removeClass('visible');
	});
	if (ss.length) {
		$(ss[0].form).submit(function() {
			if (ss.val() == defSearchVal) ss.val('');
		});
	}
	
	var abutton = $('#accountbutton'),
	submenu = abutton.next('div.loginmenu').get(0) || abutton.next('div.submenu').get(0);
	if (submenu) {
		submenu = $(submenu);
		var clickOutside,
		hideMenu = function() {
			abutton.removeClass('selected').unbind('click').click(showMenu);
			submenu.hide();
			$(document).unbind('click', clickOutside);
			return false;
		},
		showMenu = function() {
			abutton.addClass('selected').unbind('click').click(hideMenu);
			submenu.show();
			submenu.find('input[name="user_name"]').focus();
			$(document).click(clickOutside);
			return false;
		};
		clickOutside = function(e) {
			var elems = $(e.target).parents().andSelf();
			if ($.inArray(submenu[0], elems) == -1 && $.inArray(abutton[0], elems) == -1)
				hideMenu();
		};
		hideMenu();
	}

    function scroll_tweet(li){
        
        var post = li.children("div.post"),
        pWidth = post.width();
        if (pWidth > parseInt(balloon.css('width'))){
            var leftEnd = li.find('div.end');
            if(leftEnd.length === 0){
                leftEnd = $('<div class="end left" />').appendTo(li);
            } 
            var offsX = parseInt(leftEnd.width());
            post.animate({left: offsX - pWidth - 55}, 23000, 'linear', function(){post.css('left', offsX);});
        }
    }

    function swap_tweets(current_tweet){
        var next_tweet = (current_tweet + 1) % 5; /* max five tweets */
        var li = $("li#tweet" + next_tweet);

        //$("#tweet" + current_tweet).fadeOut(300);
        $("#tweet" + current_tweet).hide();
        setTimeout(function(){
            li.fadeIn(400);
         }, 400);

        setTimeout(function(){scroll_tweet(li);}, 3800);                

        display_tweet = next_tweet;
        setTimeout(function(){swap_tweets(display_tweet);}, 11300);
    }
        
    var balloon = $("#twitterpost div.balloon");
    display_tweet = 0;
    if($("div.balloon ul li#tweet1").html() !== null){
        setTimeout(function(){swap_tweets(display_tweet);}, 12000);
        var firstli = $("li#tweet0");
        setTimeout(function(){scroll_tweet(firstli);}, 7500);
    }

});

function toggle(element) {
    if(typeof(element) == 'string')
        element = $(element);

    if(element)
        element.style.display = (element.style.display != 'block') ? 'block' : 'none';
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		};
	}
}
