$(document).ready(function(){
	$('#colapse-up a').click(function(){
		$('#colapse').css('width',0);
		$('#colapse').css('display','block');
		$('#colapse').animate({width:188},200);
		return false;
	});
	$('a.colapse-none').click(function(){
		$('#colapse').animate({width:0},200,function(){
			$('#colapse').css('display',"none");
		});
		return false;
	});
	
  jQuery.each($("#audienceNav > ul > li"), function(){
        $(this).easytooltip($(this).attr("name"));
  });
  
  shortCategoryList(0,5);
  $('#f_head').Imagetooltip('f_head');
  $('#Videoheaderplayer').Imagetooltip('Videoheaderplayer');
  fadeTitelsOut();
  //setTimeout("$('#showlogo').hide()",3500);
  ImgWheel();
  initAcTest();
  init100Nations();
});


function fadeTitelsOut(){
  mediaHeaderAni = setTimeout("$('#mediaHeader').fadeOut(3000)",4000);
}

function fadeTitelsIn(){
$('#mediaHeader').fadeIn(500,function () { fadeTitelsOut(); });
}

function changeMainMedium(f_head, f_ambi, f_title, f_description, f_alt, video) {
    clearTimeout(mediaHeaderAni);
    $('#f_ambi').css("backgroundImage","url("+f_ambi+")");
    if(video){
        $("#f_headcontainer").html('<div id="Videoheaderplayer"><p><a href="http://get.adobe.com/flashplayer">Get Flash</a> to see this player.</p></div>');
        swfobject.embedSWF("/static/poncho/flash/player-licensed.swf?file="+video+"&autostart=true&controlbar=over&stretching=exactfit&fullscreen=false&backcolor=ffffff&frontcolor=615043", "Videoheaderplayer", "770", "300", "9.0.98", "expressInstall.swf");
    }else{
        $("#f_headcontainer").html('<img id="f_head">');
        $('#f_head').attr('src', f_head);
        $('#f_head').attr('alt', f_alt);
        $("#f_head").click(function(){ ImgWheel_ni('n'); });
        $("#f_head").css("cursor","pointer");
        $('#f_head').Imagetooltip('f_head');
    }
    $('#mediaHeader').html('<h2>'+f_title+'<span>'+f_description+'</span></h2>');
    fadeTitelsIn();
    return false;
}

mainmediaimages = false;
function ImgWheel(){
    mainmediaimages = $('div.mainmedia a.mainm_img');
    if(mainmediaimages.length > 1){
        if($("#f_head")){
            $("#f_head").click(function(){ ImgWheel_ni('n'); });
            $("#f_head").css("cursor","pointer");
        }
        $("#imgwh").prepend('<a id="imgwhprev" class="back" href="#">back</a>');
        $("#imgwhprev").click(function(){ ImgWheel_ni('p'); });
        $("#imgwh").prepend('<a id="imgwhnext" class="forward" href="#">forward</a>');
        $("#imgwhnext").click(function(){ ImgWheel_ni('n'); });
    }
}
function ImgWheel_ni(dir){
    if(!dir) dir = 'n';
    var cur_img = $("#f_head").attr('src');
    var ni = false;
    var pos = 0
    var i = 0;
    jQuery.each(mainmediaimages, function(){
        i++;
        if(cur_img == $(this).attr('href')) pos=i;
    });
    if(dir == 'p') pos = pos-1;
    else pos = pos+1;
    if(pos>mainmediaimages.length && dir=='n') pos = 1;
    if(pos<1 && dir=='p') pos = mainmediaimages.length;
    var i = 0;
    jQuery.each(mainmediaimages, function(){
        i++;
        if(i == pos){
            changeMainMedium($(this).attr('href'), $(this).attr('refAmbi'), $(this).attr('refTitle'), $(this).attr('refDesc'), $(this).attr('refAlt'));
            ni = false;
        }
    });
}



function shortCategoryListProcess(obj, from, count){
    var e = (from+count);
    if(e < 1) e = count;
    if(e>obj.length) e = obj.length;
    if(from<0) from = 0;
    if(from>obj.length) from = from-count;
    //alert(from+" "+e);
    s = from;
    from = 0;
    jQuery.each(obj, function(){
        if(from >= s && from < e){
            $(this).show();
        }else{
            $(this).hide();
        }
        from = from+1;
    });
}
function shortCategoryList(from, count){
    //return 0;
    var places = $("#places li");
    shortCategoryListProcess(places, from, count);
    if(places.length > count){
        $("#places").append('<li class="placessw_line"></li>');
        
        $("#places").append('<li class="placessw" id="placesPrev">previous</li>');
        prev = $("#placesPrev");
        prev.click(function(){
            shortCategoryListProcess(places, (s-count), count);
        });
        
        $("#places").append('<li class="placessw" id="placesNext">next</li>');
        next = $("#placesNext");
        next.click(function(){
            shortCategoryListProcess(places, (s+count), count);
        });
    }
}

function initAcTest() {
  // Define fancybox options
  var testMinHeight = 580;
  var testMaxHeight = 700;
  var height = Math.min(testMaxHeight, Math.max(testMinHeight, 0.75 * $(window).height()));
  // Init fancybox
  $('a.ac-test').fancybox({
    'width'	  :	640,
    'height'  : height,
    'margin'  : 0,
    'padding' : 0
  });
     
}

function init100Nations(){
  $('a.nations').fancybox();
}

