$(document).ready(function(){
		
        // Merkenbanner
        $("#scroller").simplyScroll({
			autoMode: 'loop'
		});
        
        $('.slideshow').cycle({
    		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
            prev:   '.prev', 
            next:   '.next', 
            timeout:  999999 
    	});
        
        // Mijn opticien functies
        $(".tab").livequery('click', function() {
            var id  = $(this).attr("lang");
            $(".tab[lang="+id+"]").removeClass("tab").addClass("selected");
            $(".mijnOpticienContent[lang="+id+"]").slideDown("1000");           
        });

        $(".selected").livequery('click', function() {
            var id  = $(this).attr("lang");
            $(".selected[lang="+id+"]").removeClass("selected").addClass("tab");
            $(".mijnOpticienContent[lang="+id+"]").slideUp("1000");           
        });

        // Contact google maps
        $("#showHalsteren").click( function(){
            $('#dialog').jqm();
            $('#dialog').jqmShow();
            showAddress('Dorpsstraat 128 4661 HT Halsteren, Nederland');
        });
        
        $("#showTholen").click( function() {
            $('#dialog').jqm();
            $('#dialog').jqmShow();
            showAddress('Markt 8 4691 BX Tholen, Nederland');            
        });
        
        // Brillenkampioen
        $(".brillenkampioenThumb, .mostVisits").click( function (){
           $(".slideshow").cycle(($(this).attr("id") - 1));
           $("#info").load("/ajax/brillenkampioen.php?ip="+$(".pages").attr("alt")+"&participantId="+$(this).attr("alt"), function() {
                Cufon.replace('h3');
                $("a.stem").livequery('click', function() {
                    $(this).load("/ajax/brillenkampioenVote.php?ip="+$(".pages").attr("alt")+"&participantId="+$(this).attr("alt"), function() {
                        $(this).removeClass("stem").addClass("gestemd").text("Gestemd!");
                    });
                });            
           });
        });
        
        $("a.nextPage").click( function(){
            var currentPage = parseInt($("#pageStats").attr("alt")); 
            var totalPages  = parseInt($("#pageStats").attr("title"));

            if(totalPages > currentPage){
                $(this).hide();
                var newPage     = currentPage + 1;
                $("#pageStats").attr("alt",""+newPage+"");
                $("ul.thumb[title="+currentPage+"]").fadeOut(500, function() {
                    $("ul.thumb").hide();
                    $("ul.thumb[title="+newPage+"]").fadeIn(500, function(){
                        $("a.nextPage").show();
                    });
                    
                });
                $("#pageStats").html("Serie "+newPage+" van "+totalPages);
            }
        });      

        $("a.prevPage").click( function(){
            var currentPage = parseInt($("#pageStats").attr("alt")); 
            var totalPages  = parseInt($("#pageStats").attr("title"));
            
            if(currentPage > 1){
                $(this).css("visibility", "hidden");
                var newPage     = currentPage - 1;
                $("#pageStats").attr("alt",""+newPage+"");
                $("ul.thumb[title="+currentPage+"]").fadeOut(500, function() {
                    $("ul.thumb[title="+newPage+"]").fadeIn(500, function() {
                        $("a.prevPage").css("visibility", "");
                    });
                });
                $("#pageStats").html("Serie "+newPage+" van "+totalPages);
            }
        });

        $("a.stem").livequery('click', function() {
            $(this).load("/ajax/brillenkampioenVote.php?ip="+$(".pages").attr("alt")+"&participantId="+$(this).attr("alt"), function(){
                $(this).removeClass("stem").addClass("gestemd").text("Gestemd!");
            });
        });            

        
        // Nieuws berichten roteren
        function cycleNews(){
            var current = $(".right:visible").attr("name");
            var total   = $(".right:visible").attr("totalnews");
            
            if(current == total){
                var next    = 1;
            }else{
                var next    = parseInt(current) + 1;
            }
                $(".right[name="+current+"]").fadeOut(500, function() {
                    $(".right[name="+next+"]").fadeIn(1000);
                });                
        }
        
        setInterval(function () {
            cycleNews();
        }, 12 * 1000);         

});
