function advancedSearchInit() {
    $('#advanced_search, #advanced_search_boards, .child_boards, .parent_boards').css('display', 'none');
    $('#adv_search span,#advanced_search span').html('+');
}

$(document).ready(function() {
    
    // initalise main link anims
    main_links();
    
    // popular thrills
    popular_thrills();
    popular_thrills_hover();
    
    // faq accordion
    faq();
    
    // profile names
    // profile_names(); 
    
    
    $('#adv_search').bind('click', function() {
        
        var linkIcon = $(this).children('span');
        
        if( $('#advanced_search').css('display') === 'none' ) {
            linkIcon.html('-');
        } else {
            linkIcon.html('+');
        }
        
        $('#advanced_search').animate({
            height: 'toggle'
            }, 1000, function() {
        });
        return false;
    });     
    
    $('#adv_search_boards').bind('click', function() {
        
        var linkIcon = $(this).children('span');
        
        if( $('#adv_search_boards').css('display') === 'none' ) {
            linkIcon.html('-');
        } else {
            linkIcon.html('+');
        }
        
        $('#advanced_search_boards').animate({
            height: 'toggle'
            }, 1000, function() {
        });
        return false;
    }); 
    
    //cat expand / colapse links
    $('#advanced_search_boards a').bind('click', function() {
        var linkIcon = $(this).children('span');
        var childUl = $('.parent_boards.' + $(this).attr('class'));

        if( childUl.css('display') == 'none' ) {
            linkIcon.html('-');
        } else {
            linkIcon.html('+');
        }
        
        childUl.animate({
            height: 'toggle'
            }, 1000, function() {
        });
        return false;
    }); 
    
    //board expand / colapse links
    $('.parent_boards a').bind('click', function() {
        var linkIcon = $(this).children('span');
        var childUl = $('.child_boards.' + $(this).attr('class'));

        if( childUl.css('display') == 'none' ) {
            linkIcon.html('-');
        } else {
            linkIcon.html('+');
        }
        
        childUl.stop().animate({
            height: 'toggle'
            }, 1000, function() {
        });
        return false;
    });
    

    $('.check_all').bind('click', function(){

        var cat = $(this).attr('class').split(' ');
        
        var link = $(this);
        
        if( link.text() == 'Check all' ) {
            $('.parent_boards.' + cat[1] + '_list').children('li').children('input').each(function(){
                $(this).prop('checked', true);
                
                var child = $('.child_boards.board_' + $(this).val() + '_list');
                
                if(child != 'undifined') {
                    child.children('li').children('input').each(function(){
                        $(this).prop('checked', true);
                    });
                }
                
                link.text('Uncheck all');
            });
        } else {
            $('.parent_boards.' + cat[1] + '_list').children('li').children('input').each(function(){
                $(this).prop('checked', false);
                
                var child = $('.child_boards.board_' + $(this).val() + '_list');
                
                if(child != 'undifined') {
                    child.children('li').children('input').each(function(){
                        $(this).prop('checked', false);
                    });
                }
                
                link.text('Check all');
            });
        }
        
        return false;
    });
    
    $('#userspec').keyup(function() {
        var search = $(this).val();
        console.log(search);
        if( search.length > 2 ) {
            searchUsers( search )
        }
    });
});

function searchUsers( string ) {
    $.ajax({
        type: "POST",
        async: false,
        url: _search_url,
        data: "search=" + string,           
        success: function( data ){   
            obj = $.parseJSON( data );
            
            //create the list of usernames
            var html = '<ul>';
            //loop through our results array to populate the table
            $.each(obj.users, function(i, row){
                html += '<li>' + row.member_name + '</li>';
            });
            html += '</ul>';
            
            var user_search = $('#user_search');
            
            //write the list to the DOM
            user_search.html(html);
            
            //bind the click function for the list
            $('#user_search li').bind('click', function() {
                $('#userspec').val($(this).text());
                user_search.empty();
            });
            
            var mouseIsInside;
            
            //findout where the mouse is
            user_search.hover(function() { 
                mouseIsInside = true; 
            }, function() { 
                mouseIsInside = false; 
            });

            //if the user clicks outside of the list close it
            $("body").mouseup(function() { 
                if(!mouseIsInside) {
                    user_search.empty();
                } 
            });
        }
    });
}

var template_path;
var domain;
var totalImages     = 5;
var currentImage;
var imageArray      = Array();
var character       = String();
var currentImageLoc;
var countdown;

var _newchar = 0;
var _new_rotation = 0;

function main_links()
{
    var lis = $('#main_links li');
    $(lis).mouseover(function()
    {
        var which_link = $(this).attr('id');
                
        $(this).css({
            'cursor'           : 'pointer',
            'background-color' : '#ad0e1b'
        });
        
        $('#'+which_link+'_sub').show();
        
    });
    
    
    $(lis).mouseout(function()
    {
        var which_link = $(this).attr('id');
        
        $(this).css({
            'cursor'           : 'auto',
            'background-color' : '#e1001a'
        });            
        
        $('#'+which_link+'_sub').hide();
                        
    })
    
    $("#news_link_sub").mouseover(function() {
        $(this).css({'cursor' : 'auto'});
    })
    
    $(".news_link_sub_cont").mouseover(function() {
        $(this).css({
            'cursor' : 'pointer',
            'background-color' : '#bb0016'
        });
    })
    
    $(".news_link_sub_cont").mouseout(function() {
        $(this).css({
            'cursor' : 'auto',
            'background-color' : 'transparent'
        });
    })
    
    $(".store_link_sub_cont").mouseover(function() {
        $(this).css({
            'cursor' : 'pointer',
            'background-color' : '#bb0016'
        });
    })
    
    $(".store_link_sub_cont").mouseout(function() {
        $(this).css({
            'cursor' : 'auto',
            'background-color' : 'transparent'
        });
    })    
       
            
}

function popular_thrills_hover()
{
    var info;
    var img_id;
    $('#popular_thrills_include ul li img').mouseover(function()
    {
        info    = $(this).attr('title');
        img_id  = $(this).attr('id');
        $(this).attr('title', '');
        $(this).parent().append('<div class="popular_thrills_hover">'+info+'</div>');
        
        // fade out others
        $('#popular_thrills_include ul li img').each(function()
        {
            if ($(this).attr('id') != img_id)
            {
                $(this).stop().animate({'opacity' : '0.3'}, 'fast');
            }
        })
        
        // $('#main_pic img').hide();
        begin_rotation(img_id); 
        
    });
    
    $('#popular_thrills_include ul li img').mouseout(function()
    {
        $(this).parent().find('.popular_thrills_hover').hide();
        $(this).attr('title', info);
        
        // fade in others
        $('#popular_thrills_include ul li img').each(function()
        {
            if ($(this).attr('id') != img_id)
            {
                $(this).stop().animate({'opacity' : '1'}, 'fast'); 
            }
        })        
          
    });
    
    $('#popular_thrills_include #main_pic').mouseover(function()
    {
        $(this).css('cursor', 'pointer');        
        timer("stop");
    })
    
    $('#popular_thrills_include #main_pic').mouseout(function()
    {
        $(this).css('cursor', 'auto');
        timer("start");
    })    
    
}

function popular_thrills()
{
    
    // preload default or choice
    // begin rotation
    begin_rotation();

}

function begin_rotation(character)
{
    if (isset(character))
    {
        timer("stop");
        load_assets(character); 
    }
    else
    {
        character = "judge_dredd";
        load_assets(character);
    }
}

function load_assets(character)
{
    
    imageArray = [];
        
    for (var i=1; i <= totalImages; i++)
    {
        var imLoc = '<a href="http://www.2000adonline.com/character/'+ character +'/"><img src="' + domain + '/media/profile/263x282/' + character + '/'+i+'.jpg" /></a>';
        imageArray.push(imLoc);      
    }
    
    currentImage = 0;
    
    timer("start");
}

function timer(state)
{
    if (state == "start")
    {
        cycleImage();
        countdown = setInterval("cycleImage()", 5000);
    }
    else
    {
        clearInterval(countdown);
    }
    
    return false;
}

function cycleImage()
{
    var loc = $('#main_pic');
    
    if (currentImage < totalImages)
    {
        currentImageLoc = imageArray[currentImage];
        $(loc).html(currentImageLoc);
        $(loc).stop().hide();
        if (_newchar == 1)
        {
            $(loc).stop().show();
        }
        else
        {
            $(loc).stop().show();
        }
        currentImage++;
    }
    else
    {
        currentImage = 0;
        cycleImage();        
    }
}

function isset(variable)
{
    if ( typeof variable !== "undefined" && variable)
    {
        return true;
    }
    
    return false;
}

function profile_names()
{
    $('.product img').mouseover(function(){
        $('.product_name').css('display', 'block');
        $('.product_name').html(($(this).attr('title')));
        $(this).mousemove(function(e){
            $('.product_name').css({
                'left' : e.pageX + 20,
                'top': e.pageY - 40
            })
        })
    })
    
    $('.product img').mouseout(function(){
        $('.product_name').css('display', 'none');
    })    
}

function faq()
{
    // does faq exist on this page?
    if ($('#faq').length > 0)
    {
        
        $('#faq').accordion();
        
        $('#faq .head').click(function(){
            $(this).next().toggle('slow');
            return false;
        }).next().hide();
    }
    else
    {
        // no FAQ - do nothing...
        return false;
    }
}
