jQuery.fn.exists = function(){return jQuery(this).length>0;}

$(function(){
    if($('#newsletter').exists()){
        //functions that setup default actions for our newsletter inputs
        $('#newsletter').find('input').each(function(){
            $(this).data('default',$(this).val());
            
            $(this).focus(function(){
                $(this).val(($(this).val() == $(this).data('default'))? '' : $(this).val());
                $(this).removeClass('error');    
            });
            
            $(this).blur(function(){
                if($(this).val() == ""){
                    $(this).val($(this).data('default'));
                }else{
                    if($(this).attr('name') == 'email'){
                        if($(this).val().search('[a-zA-Z0-9\.\-\_]+@[a-zA-Z0-9\.\-]+[.][a-zA-Z]{2,3}')<0){
                            $(this).addClass('error');
                        }
                    }
                }
                    
            });
        });
        //newsletter submit function
        $('#newsletter').submit(function(){
            var $ok = true;
            $(this).find('input').each(function(){
                if($(this).val() == $(this).data('default')){
                    $(this).addClass('error');
                    $ok = false;
                }else{
                    if($(this).attr('name') == 'email'){
                        if($(this).val().search('[a-zA-Z0-9\.\-\_]+@[a-zA-Z0-9\.\-]+[.][a-zA-Z]{2,3}')<0){
                            $(this).addClass('error');
                            $ok = false;
                        }
                    }
                }
            });
            if($ok){
                $.post('./mailer.php',$(this).serialize(),function(data){
                    if(data == 'ok'){
                        $('#newsletter').empty().html('<h2>Thank You</h2>');
                    }else{
                        $('#newsletter').append(data);
                    }
                });
            }
            return false;
        });
    }
    
    if($('#recentArticles').exists()){
        $("#recentArticles").scrollable({ 
            items:'.article',
            vertical:true,
            next:'a.down', 
            prev:'a.up',
            size: 4 
        }).circular().autoscroll(4000); 
    }
    if($('#masthead').exists()){
        $('#masthead').cycle({
            pager: '#readMore',
            timeout: 8000
        });
    }
    if($('#tabs').exists()){ 
        $("#tabs").tabs("div.panes > div");
    } 
    if($('.scrollable').exists()){
        $(".scrollable").scrollable({ 
            items:'.clips', 
            vertical:true, 
            next:'a.down', 
            prev:'a.up',
            size: 4  
        });
    }
    if($('#testimonialNav').exists()){
        $("#testimonialNav").scrollable({ 
            items:'div',
            size: 5, 
            next:'a.rightButton', 
            prev:'a.leftButton' 
        });   
    }
     if($('#articleNav').exists()){
        $("#articleNav").scrollable({ 
            items:'span',
            size: 3, 
            next:'a.rightButton', 
            prev:'a.leftButton' 
        });   
    }
    if($('#player').exists()){
        if($('div.clips').exists()){
             $f("player","/flowplayer.commercial-3.1.5.swf",{
                clip:{
                    autoPlay: true,
                    baseUrl: '/'
                },
                key: '#@dfb7032a94be1a3b894' 
            }).playlist("div.clips");    
        }else{
            $('#player').each(function(){
                 $f("player","/flowplayer.commercial-3.1.5.swf",{
                    clip:{
                        autoPlay: true,
                        baseUrl: '/'
                    },
                    key: '#@dfb7032a94be1a3b894' 
                });
            });
        }
       
    }
     if($('#videoArea').exists()){
        $f("video","/flowplayer.commercial-3.1.5.swf",{
            clip:{
                autoPlay: true,
                baseUrl: '/'
            },
            key: '#@dfb7032a94be1a3b894' 
        });
     }
    if($('a.player').exists()){
        $f("a.player","/flowplayer.commercial-3.1.5.swf",{
            clip:{
                autoPlay: false
            },
            key: '#@dfb7032a94be1a3b894'
        })
    }  
    if($('.page_item').exists()){  
        $('.page_item:last').css('border','none');
        //get the html from each list item and if the html is found in the page title
        //change the class to active
        var $title = document.title;
        $('.page_item').each(function(){
            if($title.search($(this).text()) > -1){
                $(this).find('a').addClass('active');
                
            }
        });
    }
    
    if($('#productTabs').exists()){ 
        $("#productTabs").tabs("div.panes > div");
    } 
    
    if($('.answer').exists()){
        $('.question').each(function(){
            $(this).next().hide();
        });
        $('.question').click(function(){
            if($(this).next().is(':visible')){
                $(this).next().hide();
            }else{
                $(this).next().show();
            }
        });
        //this is to highlight the right tab
        $('#faq').addClass('active');
    }
});
