function hideHumanValidationFields() {
    $('#hvalidation').prev().hide();
    $('#hvalidation').hide();
}

function replaceURLWithHTMLLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp,"<a href='$1' target='blank'>$1</a>"); 
}

$(document).ready(function() {
    hideHumanValidationFields();
    
    $('#menu li:first').addClass('first');
    
    $('ul.sf-menu').superfish({
        speed:         'fast', 
        dropShadows:    false,
        onBeforeShow:  function(){
            var pWidth = $(this).parent().width()-23;
            $('.sf-menu ul').css('width', pWidth+'px');
            $('ul.sf-menu li li:hover ul').css('width', pWidth+'px');
            $('ul.sf-menu li li.sfHover ul').css('width', pWidth+'px');            
        }
    });
    
    $('#slider').nivoSlider({
        effect:'fade',
        animSpeed:500,
        pauseTime:5000,
        startSlide:0, 
        directionNav:false, 
        controlNav:false,
        keyboardNav:false,
        pauseOnHover:false, 
        manualAdvance:false,
        captionOpacity:0.6
    });   

    $('#sponsorSlider').nivoSlider({
        effect:'slideInLeft',
        animSpeed:500,
        pauseTime:5000,
        startSlide:0, 
        directionNav:false, 
        controlNav:false,
        keyboardNav:false
    });    
   
    $('#albumShowContentImages a:nth-child(5n)').addClass('last');
    
    $("a.imageGroup").fancybox({
        'overlayShow'	:	true,
        'cyclic'        :   true,
        'titleShow'     :   true,
        'titlePosition' 	: 'over'
    });    
    
    $("a").filter(function(){
        if ($(this).attr('href')) {
            if($(this).attr('href').match(/\.(jpe?g|png|gif)/i)) {
                $(this).fancybox();
            }
        }
    });  
    
    $("a[rel~=external]").each(function(i){
        this.target="_blank";
    });	    
    
    $('#twitterMessage p.message').each(function(index) {
        $(this).html(replaceURLWithHTMLLinks($(this).html()));
    });    
    
    $('.dateField').datepick({
        dateFormat: 'dd-mm-yyyy', 
        showOn: 'both', 
        changeMonth: false, 
        changeYear: false, 
        currentText: '', 
        showWeeks: true
    }); 		
    
    $('textarea.tiny').tinymce({
        script_url : '/scripts/jquery/tiny_mce/tiny_mce.js',
        file_browser_callback : "tinyBrowser",
        theme : "advanced",
        plugins : "table,contextmenu,paste,style,advlink",
        mode: "specific_textareas",
        editor_selector : "mceEditor",
        width : "100%",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_buttons1: "bold,italic,underline,forecolor,|,formatselect,|,bullist,numlist,|,justifyleft,justifycenter,justifyright,justifyfull,|,outdent,indent,|,link,unlink,|,table,|,charmapcode,code,pastetext",
        theme_advanced_buttons2: "",
        theme_advanced_buttons3: "",
        content_css : "/cms/styles/tinymce.css",
        relative_urls : false,
        paste_auto_cleanup_on_paste : true,
        paste_preprocess : function(pl, o) {
            o.content = o.content;
        },
        paste_postprocess : function(pl, o) {
            o.node.innerHTML = o.node.innerHTML;
        }
    });    
    
    $('.cmsContentContainer img').each(function() {
        if($(this).css('float') == 'left') {
            $(this).addClass('imageLeft');
        } else if($(this).css('float') == 'right') {
            $(this).addClass('imageRight');
        }
    });

    $(".cmsContentContainer a").filter(function(){
        if($(this).attr('href').match(/\.(jpe?g|png|gif)/i)) {
            $(this).fancybox();
        }
    });    
    
    $("#memberIndexTable").tablesorter({
        headers: {
            3: {
                sorter: false
            }, 
            4: {
                sorter: false
            }, 
            5: {
                sorter: false
            }
        }
    }); 
    
    if($('.obmt').length > 0) {
        $('.obmt').attr('href', 'mailto:'+$('.obmt').attr('href').replace(/\|/g,'@').replace(/\:/g,'.')).text($('.obmt').text().replace(/\|/g,'@').replace(/\:/g,'.'));	
    }
    
    $('#main').css('min-height', $('#sidebar').height());
});

