$(function(){
    
	 $("a[rel='intextimage']").colorbox(); 
     $('.banners_large').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
            ,   timeout:    15000
            ,   speed:      2000
			,	height: 	225
	});
	$('.banners_small').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
            ,   timeout:    15000
            ,   speed:      2000
			,	height: 	75
	});

    $('a.email').hover(function ()
	{
		$(this).nospam({
		replaceText: false,
		filterLevel: 'low'
		});
	},
		function(){
		$(this).attr('href','#');
		}
	); 
	
    $('ul.quickTree').quickTree();
    $.validator.messages.required = "";
    $.validator.messages.email = "";
    if($("a.download")){
        downloadManager($(this).attr("id"));
    }
    $(".loginbox input[type=text]").focus(function() {
        $(this).css("background-color","#FFFFFF");
    });
    $(".loginbox input[type=text]").blur(function() {
        $(this).css("background-color","#F7F7F7");
    });
    $('#loginbox_form').submit(function() {
        return false;
      });
    $("#loginbox input[type=button].next").click(function(){
       
        if($('#loginbox_form').validate().form()){
                  
            var queryString = $('#loginbox_form').formSerialize();
    
            $.post('/download/check_email_http', queryString,function(data){
                if ($(data).text() != 'not-found'){
                    $.cookie('download_token', $(data).text(),{ path: '/'});
                    $("a.download").each(function(){
                        $(this).attr("href",$(this).attr("rel")).removeAttr("class").removeAttr("rel").addClass("registered");
                    });
                    $.fn.colorbox({width:"500px",inline:true,href:"#thanksbox"});
                    $.doTimeout( 5000, function(){
                        $.fn.colorbox.close();
                    });
                }else{
                    $.fn.colorbox({width:"500px",inline:true,href:"#formbox"});
                }
            });
             
        }else{
            $.fn.colorbox.resize();
        }

    });
    $("#formbox input[type=button].next").click(function(){
        if($('#formbox_form').validate().form()){
            $.fn.colorbox({width:"500px",inline:true,href:"#formboxvervolg"});
        }
    });
    $("#formbox input[type=button].previous").click(function(){
        $.fn.colorbox({width:"500px",inline:true,href:"#loginbox"});
    });
    $("#formboxvervolg input[type=button].previous").click(function(){
        $.fn.colorbox({width:"500px",inline:true,href:"#formbox"});
    });
    $("#formboxvervolg input[type=button].next").click(function(){
        if($('#formbox_form').validate().form()){
            var queryString = $('#formbox_form').formSerialize();
            $.post('/download/insert_email_http', queryString,function(data){
    
                if ($(data).find('download_token').text() != 0){
                    $.cookie('download_token', $(data).find('download_token').text(),{ path: '/'});
                    $("a.download").each(function(){
                        $(this).attr("href",$(this).attr("rel")).removeAttr("class").removeAttr("rel").addClass("registered");
                    });
                    $.fn.colorbox({width:"500px",inline:true,href:"#thanksbox"});
                    $.doTimeout( 5000, function(){
                        $.fn.colorbox.close();
                    });
                }else{
                    alert('Error please contact the webmaster');
                }
            });
        }
        var queryStringInfo = $('#formboxvervolg_form').formSerialize();
        $.post('/download/insert_information_http?email='+$("#email").val(), queryStringInfo);
    });
    
     $("#example_1").monthpicker({
	elements: [
		{tpl:"month"},
		{tpl:"year",opt:{

			range: "-0~1"
		}}

	],
	onChanged: callback
    });
        
});

function downloadManager(id){
    if ($.cookie('download_token')){
        $("a.download").each(function(){
            $(this).attr("href",$(this).attr("rel"));
        });
    }else{
        $(".download").colorbox({width:"500px",inline:true, href:"#loginbox"});
    }
}

function callback(data,$e){
    var str = "";
    for(key in data) {
            str += " " + key + ": " + data[key]+ "; ";

    }
    $('.newslist').show();
    $('#not_found').show();
    if ($('.'+data['month']+data['year']).size() > 1){
        $('.newslist').hide();
    }
    $('.'+data['month']+data['year']).each(function() {
        $(this).fadeIn();
        $('#not_found').hide();
    });
}
