jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
var userAgent = navigator.userAgent.toLowerCase();
jQuery.browser = {
    version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
        safari: /webkit/.test( userAgent ),
	   opera: /opera/.test( userAgent ),
	     msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
	       mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
	      };

function initMainBody() {
	$("a, area").unbind();
	$("a, area").click(function(){
		if($(this).attr('target') != '_blank') { 
		/* [rel='history'] */
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
                this.blur(); //alx remove dotted line around href
		return false;
		}
	});

        //TODO alx
        //check if user is logged in and display top menu buttons accordingly

}
function pageload(hash) {
	var opt = '';
	if(hash == '') {hash = 'startseite';}
	if($.browser.msie) {
		// jquery's $.load() function does't work when hash include special characters like åäö.
		hash = encodeURIComponent(hash);
	}
	$("#main_body").html('<div class="box_text" style="border: none;background: transparent;"><img src="'+loadImg+'" alt="Bitte warten ..." /></div>');
	
	$.post('/ajax/content.ajax.php',
		{
			hash: 	hash,
			opt:	opt
		}, function(data){
			$("#main_body").html(data);
			initMainBody();
		}
	);

        //alx 20100108
        setActiveMainMenuItem(hash);
        //TODO alx should preload schliessen-image
	$.scrollTo('#top', 400);
}

function setActiveMainMenuItem(hash) {

//TODO alx this is stupid hardcoded, should be nicer

     var main_menu = new Object();
     var exceptions = new Object();
     var activeItem = false;
     
     main_menu['start'] = "startseite";
     main_menu['wettbewerb']  = "wettbewerb";
     main_menu['wg_profile']  = "wgs_suche";
     main_menu['forum']  = "forum";
     main_menu['musik']  = "bands_";
     main_menu['events']  = "events";
     main_menu['finale']  = "wettbewerb_finale";

     exceptions['wgs_anmelden'] = "wettbewerb";
     exceptions['grolsch_informationen'] = "wettbewerb";
     exceptions['wgs_profil'] = "wg_profile";

     for ( var menuItem in main_menu ) {
         //reset to normal state to avoid multiple actives
         $('#main_menu_'+menuItem).children("a").css('backgroundPosition', '0px 0px');
        if (hash.indexOf(main_menu[menuItem]) == 0) activeItem = menuItem;
     }
     
     if (activeItem == false ){
         for ( menuItem in exceptions ) {
            if (hash.indexOf(menuItem) == 0) activeItem = exceptions[menuItem];
         }
     }

     if (activeItem != false ){
         $('#main_menu_'+activeItem).children("a").css('backgroundPosition', '0px -120px');
     }

}

function popupVideo(pfad, width, height) {
	// width = 320;
	var video = '<script type="text/javascript">';
	video += 'var pfad = "'+pfad+'";';
	video += 'var width = "'+width+'";';
	video += 'var height = "'+height+'";';
	video += 'var flashvars = { MM_ComponentVersion: 1, skinName: "/flash/tvplayer/Clear_Skin_3", streamName: ""+pfad, height: '+height+', autoPlay: "true", autoRewind: "false" };';
	video += 'var params = { wmode: "transparent", quality: "high",scale: "noscale",salign: "lt" };';
	video += 'var attributes = { id: "FLVPlayer", name: "FLVPlayer" };';
	video += 'swfobject.embedSWF("/flash/tvplayer/FLVPlayer_Progressive.swf", "video", width, height, "9.0.0", "#ffffff", flashvars, params, attributes);';
	video += '$("#FLVPlayer").hide().show();';
	video += '</script\>';
	
	$.prompt('<div id="popup" style="width: '+width+'px;height: auto;"><div id="video">Um diese Videos betrachten zu können benötigst Du den kostenlosen Adobe <a href="http://get.adobe.com/de/flashplayer/" target="_blank">Flash Player 8</a> oder h&ouml;her.</div>'+video+'</div>', {buttons: {}, opacity: 0.6});
}
var playItem = 0;
var myPlayList = [
{
    aid:"r1",
    title:"Dance like crazy",
    band:"How To Loot Brazil",
    laufzeit:"4:25",
    mp3:"/cache/music/1.mp3?"
},
{
    aid:"r2",
    title:"Messy girl",
    band:"Angelika Express",
    laufzeit:"3:12",
    mp3:"/cache/music/2.mp3?"
},
{
    aid:"r3",
    title:"Headlights",
    band:"3 Feet Smaller",
    laufzeit:"2:56",
    mp3:"/cache/music/3.mp3?"
},
{
    aid:"r4",
    title:"Zu was",
    band:"Voltaire",
    laufzeit:"4:55",
    mp3:"/cache/music/4.mp3?"
},
{
    aid:"r5",
    title:"Fesse à fesse, face to faces",
    band:"Phonoboy",
    laufzeit:"4:07",
    mp3:"/cache/music/5.mp3?"
},
{
    aid:"r6",
    title:"Messages",
    band:"Planetakis",
    laufzeit:"3:29",
    mp3:"/cache/music/6.mp3?"
},
{
    aid:"r7",
    title:"Tanz baby",
    band:"Justus Parker",
    laufzeit:"3:13",
    mp3:"/cache/music/7.mp3?"
},
{
    aid:"r8",
    title:"Betty & Linda",
    band:"Artwon Artown Artnow",
    laufzeit:"3:51",
    mp3:"/cache/music/8.mp3?"
},
{
    aid:"r9",
    title:"Heart Of Horror",
    band:"Embrace",
    laufzeit:"2:44",
    mp3:"/cache/music/9.mp3?"
},
{
    aid:"r10",
    title:"Nothing to gain",
    band:"Frank Popp",
    laufzeit:"2:49",
    mp3:"/cache/music/10.mp3?"
},
{
    aid:"r11",
    title:"Wo bist du jetzt",
    band:"Studio Grande",
    laufzeit:"2:30",
    mp3:"/cache/music/11.mp3?"
},
{
    aid:"r12",
    title:"Have a good time",
    band:"Louis Lament",
    laufzeit:"4:04",
    mp3:"/cache/music/12.mp3?"
},
{
    aid:"r13",
    title:"Soll/Haben",
    band:"Nachlader",
    laufzeit:"4:22",
    mp3:"/cache/music/13.mp3?"
},
{
    aid:"r14",
    title:"Die beginnende Vergangenheit",
    band:"Weltrekorder",
    laufzeit:"3:34",
    mp3:"/cache/music/14.mp3?"
},
{
    aid:"r15",
    title:"Shoot my lover",
    band:"Pony Empire",
    laufzeit:"2:21",
    mp3:"/cache/music/15.mp3?"
},
{
    aid:"r16",
    title:"All is nothing",
    band:"The Rumours",
    laufzeit:"2:13",
    mp3:"/cache/music/16.mp3?"
},
{
    aid:"r17",
    title:"Moment",
    band:"Another Day",
    laufzeit:"3:03",
    mp3:"/cache/music/17.mp3?"
},
{
    aid:"r18",
    title:"Lass uns Verschwinden",
    band:"Hallo Erde",
    laufzeit:"3:13",
    mp3:"/cache/music/18.mp3?"
},
{
    aid:"r19",
    title:"Cincinnati",
    band:"LA Love",
    laufzeit:"1:45",
    mp3:"/cache/music/19.mp3?"
},
];

function topToggle ( state ) {
/*
 * alx 20091227
 * toggles top area open / close
 * and replaces oeffnen/schliessen image
 */

/*TODO make more general?*/
/*TODO IE clips off left overlap image on animate ?!!?*/

    if (state == undefined) state = 'toggle';

    $("#top_playlist").toggle(); //makes for smoother opening in FF
    $("#top_menu_top").animate({height: state, opacity: 1.0}, 'fast', "swing", function () {

        if ($("#topToggle_link").css('backgroundImage').toLowerCase().indexOf('oeffnen') >= 0 ){
            $("#topToggle_link").css('backgroundImage', $("#topToggle_link").css('backgroundImage').replace("oeffnen", "schliessen"));
        } else {
             $("#topToggle_link").css('backgroundImage', $("#topToggle_link").css('backgroundImage').replace("schliessen", "oeffnen"));
        }

        /* overlap the rest of page
         * if asynch after method call then FF flashes like crazy, so needs to be here probably
         */
        if (jQuery.support.opacity == true ) {
            //basically checks for IE because effect looks total crap there, opacity of deckel! even in IE8
            if (state == 'toggle') {
                if ($("#header").css("margin-bottom") == "0px" || $("#header").css("margin-bottom") == "auto") {
                    $("#header").animate({marginBottom: '-80px', opacity: 1.0}, 'fast'); //retract
                } else {
                    $("#header").animate({marginBottom: '0px', opacity: 1.0}, 'fast');
                }
            }
            if (state == 'show') {
                $("#header").animate({marginBottom: '-80px', opacity: 1.0}, 'fast'); //retract
            }
            if (state == 'hide') {
                $("#header").animate({marginBottom: '0px', opacity: 1.0}, 'fast'); //retract
            }
        }
//        $("#top_menu_middle").attr('style', 'margin-left:-28px;');

    });

     

    

}



$(document).ready(function() {

                /* alx 20091227        */
        $('#topToggle_link').click(function() {topToggle();});
        $('#top_menu_login').click(function() {topToggle();});
        $('#top_menu_newsletter').click(function() {topToggle();});
	
});
