$(document).ready(
	function()
	{
	
	
        //fotogallerij
        $(".galerij").ccgallery();
       
        //wedstrijd header
        bindHeader();
        changeHeader('volgende');
    
        //Nieuws teaser
        rotate();           
        bindNieuwsHome();
        NewsteaserButtons();
		
        //Founders
        doSponsors();         
        replaceLogos();
        
		//Twitter
                        
        //Foto van de week
        bindFotovandeWeek();
        
        //Tickets prijs berekenen
        calculateTicketPrice();       
        
doTwitter(); 
        
	}
);

/************************************************
 * wedstrijd (header)
 ************************************************/
function bindHeader()
{
    $(".tabs-vorige").hover(function(){
        changeHeader('vorige');       
    });
    
    $(".tabs-volgende").hover(function(){
        
        changeHeader('volgende');
    });
}

function changeHeader(current)
{
    //Alles verbergend
    $('.wedstrijd-vorige').css('display', 'none');
    $('.wedstrijd-volgende').css('display', 'none');
    $(".wedstrijd-tabs li").removeClass("active");
    
    $(".tabs-" + current).addClass("active");
    $('.wedstrijd-' + current).css('display','block');
}

/************************************************
 * NewsTeaser
 ************************************************/
function NewsteaserButtons()
{
    $(".content-arrowright").click(
        function()
        {
            clearTimeout(rotater);
            rotate();
        }
    ) 
    
    $(".content-arrowleft").click(
        function()
        {
            clearTimeout(rotater);
            rotate(1);
        }
    )    
}


function bindNieuwsHome()
{
	$(".content-rotator ul li").hover(
		function(e)
		{
			clearTimeout(rotater);
			rotater = setTimeout("rotate()", 5000);	

			$(".content-rotator ul li.active").removeClass("active");

			$(this).addClass("active");
			
            changeNewsteaserItem();
		}
	);
	
	$(".content-rotator ul li a").click(
		function(e){
			e.preventDefault();
		});
}

function rotate(moveBack)
{
    var active    = $(".content-rotator ul li.active");
    
    if(moveBack == 1)
    {
        var next      = active.prev();
        if (next.length == 0)
        {
        	next = $(".content-rotator ul li:last");
        }
    }else{
        var next      = active.next();
        if (next.length == 0)
        {
        	next = $(".content-rotator ul li:first");
        }
    }
    
    $(".content-rotator ul li.active").removeClass("active");
    next.addClass("active");
    changeNewsteaserItem();
    rotater = setTimeout("rotate()", 5000);
}


function changeNewsteaserItem()
{
    $(".content-image .content-afbeelding").attr("src", $(".content-rotator ul li.active a .afbeelding").attr("src"));    
    $(".content-image .content-text h1 a").text($(".content-rotator ul li.active a .titel").text());
    $(".content-image .content-text p").text($(".content-rotator ul li.active a .omschrijving").text());           
    $(".content-image .content-text h1 a").attr("href", $(".content-rotator ul li.active a").attr("href"));
    $(".content-image .content-rotator .categorieSelected").text('Categorie: ' + $(".content-rotator ul li.active a .categorie").text());
}


/************************************************
 * Twitter
 ************************************************/
function doTwitter()
{
	getTwitters('main-tweet', { 
  	id: 'magixxnijmegen', 
  	count: 4, 
  	enableLinks: true, 
  	ignoreReplies: false, 
  	clearContents: true,
  	template: '<img src="%user_profile_image_url%"/>%text% <br /><a href="http://twitter.com/%user_screen_name%/statuses/%id%/" target="_blank">%time%</a> <a class="reply" href="http://twitter.com/?status=@%user_screen_name%%20&in_reply_to_status_id=%id_str%&in_reply_to=%user_screen_name%/" target="_blank">reply</a>'
  	}
  	);
	
	  	}



/************************************************
 * Founders
 ************************************************/
function doSponsors()
{
	$("ul#sponsors").animate({
		left: "-345px"
	},
	2000,
	function()
	{
		doSwitch();
	});	
}

function doSwitch()
{
	$("ul#sponsors li:first").appendTo("ul#sponsors");
	$("ul#sponsors").css("left", "0px");
	
	setTimeout("doSponsors()", 1000);
}

function replaceLogos()
{
    $("ul#sponsors li").each(
        function()
		{
			var doReplace = true;
            $(this).css("text-align", "center");
			
            
			$(this).find("img").load(
					function()
					{           
						
                        var img = $(this);
						doLogoReplace(img);  
                        doReplace == false;
					}
			);	
            
            if(doReplace == true)
            {
                img = $(this).find("img");
                doLogoReplace(img);    
            }
            	
            			
		}
	);
}

function doLogoReplace(img)
{
	var height = img.height();
	
	//console.log(height);
    //alert(Math.round(((200-height)/2)-10));
    var top = Math.round(((200-height)/2)-10);
    
	img.css("margin-top", top+"px");
}
/************************************************
 * Foto van de week
 ************************************************/
function bindFotovandeWeek()
{
    $('.foto .medium-content .link a').click(function(){
        
        fillBackground();
        $("#modal-fotobeoordelen .large").attr("src",$(".fotovdweek").attr("src"));
    	
        $('.close').click(function(){
            $("body div#ccSHADOW").fadeOut();
	        $("#modal-wrap").css({display: "none"});
        });
    });
}

function fillBackground()
{
    var iWidth = $("body").width();
    if ($("body div#ccSHADOW").length == 0)
	{
		var shadow = $("<div></div>").attr("id", "ccSHADOW")
				.css({
						background: "#000000",
						opacity: "0.6",
						position: "absolute",
						left: "0",
						top: "0",
						'z-index': 900
				});
		
		var iHeight = $(document).height();
		shadow.width(iWidth+"px");
		shadow.height(iHeight+"px");
		$("body").append(shadow);
	}
	else
	{
		var shadow = $("body div#ccSHADOW");
	}	
	
	shadow.show();
    
    iLeft = (iWidth/2) - 200;
    $("#modal-wrap").css({background:"#ffffff",
                        display: "block",
                        left: iLeft+"px",
                        width: "400px",
                       	'z-index': 1000});

}

function calculateTicketPrice()
{   
    $('.jeugdTicket').change(function() {
        ticketsPriceCalculator();
    });
    
    $('.volwassenTicket').change(function() {
        ticketsPriceCalculator();
    });
      
}

function ticketsPriceCalculator()
{
    var prijs = 0;
    prijs += (5  * $('.jeugdTicket').attr('value'));
    prijs += (10 * $('.volwassenTicket').attr('value'));
    
    $('.ticketPrijs').attr('value','' + prijs + '.00');
    
}
