// hebtv.js
// site-wide javascript functions

// hide javascript-controlled elements from js-capable browsers
document.write('<link rel="stylesheet" href="/css/js.css" type="text/css" media="screen" />');

$(document).ready(function() {
    
    var admin_dash;
    var mod_dash;
    var msg_threads;
    
    //hide login or loggedin boxes
    $('body').click(function() {
	    $('#panels div.login_panel').hide();
	    $('#panels div.loggedin_panel').hide();
	    return true;
    });
    
    //stop events bubbling up on login panels
    $('div.login_panel, div.loggedin_panel').click(function(event) {
	event.stopPropagation();
    });

    // show login box (change for 2010 version)
    $('a#signin').click(function() {
	    $('#panels div.login_panel').toggle();
	    return false;
    });
    
    // show loggedin box (change for 2010 version)
    $('a#myhebridestv').click(function() {
	    $('#panels div.loggedin_panel').toggle();
	    return false;
    });        
    
    // cc licence show stuff
    $('.cc_select').change(function() {
        var term;
        var url;
        
        $('div#terms_info').show();
        
        term = $(this).attr('value');
        url = '/cc_licences/viewText/' + term;
        
        $.get(url, function(html) {
            $('div.cc_info').empty().append(html);
        });
        return false;
    });
    
    // update the cc_terms box
    $(".cc_select").trigger("change");
    
    // clear password / username boxes on clicking
    $('input#UserUsername').click(function() {
        $(this).select();
        $('input#UserPassword').attr('value', '');
    });
    
    // form_section clickable headers
    //$('div.form_section h4').toggle(function() {
    //    $(this).siblings().hide();
    //}, function() {
    //    $(this).siblings().show();
    //});
    
    // select contents of search box on click
    $('input#SearchKeywords').click(function() {
        $(this).select();
    });
    
	 // add AJAX navigation tabs to sampler page
	 $('#sub_content').tabs({ fxFade: true, fxSpeed: 'fast' });
    
    admin_dash = $.cookie('admin_dash'); // get admin_dash status
    mod_dash = $.cookie('mod_dash'); // get mod_dash
    msg_threads = $.cookie('msg_threads'); // get cookie for showing read messages
    
    // show read message threads if cookie set
    if(msg_threads == 'show') {
        $('tr.read').show();
        $('tr.no_unread').hide();
        $('a.toggle_read').html('Hide Read Threads');
    }
    else
    {
        $('tr.read').hide();
        $('tr.no_unread').show();
        $('a.toggle_read').html('Show Read Messages');
    }
    
    // toggle display of read / unread message threads
	$('a.toggle_read').click(function() {
        if(msg_threads == 'show') {
            msg_threads = 'hide';
            $.cookie('msg_threads', 'hide', {expires: 7, path: '/'});
            $('tr.read').hide();
            $('tr.no_unread').show();
            $('a.toggle_read').html('Show Read Messages');
        } else {
            msg_threads = 'show';
            $.cookie('msg_threads', 'show', {expires: 7, path: '/'});
            $('tr.read').show();
            $('tr.no_unread').hide();
            $('a.toggle_read').html('Hide Read Messages');
        }
        return false;
    });
    
    // show admin dash contents if cookie set
    if(admin_dash == 'on') {
        $('div#admin_dash').show();
    }
    
    // show mod dash contents if cookie set
    if(mod_dash == 'on') {
        $('div#moderator_dash').show();
    }
	
	 // show extended video info
	 $("a.more").click(function() {
		  //$('a.more').hide();
		  $("#video_desc").slideDown('fast');
		  return false;
	 });

	 // hide extended video info
	 $("a.less").click(function() {
		  //$('a.more').show();
		  $("#video_desc").slideUp('fast');
		  return false;
	 });
    
	 // hide / show table of contents for help pages
	 $('a.help_box_switch').toggle(function() {
		  $(this).html('Hide Help Table of Contents');
		  $('div.help_contents').slideToggle('fast');
	 }, function () {
		  $(this).html('Show Help Table of Contents');
		  $('div.help_contents').slideToggle('fast');
	 });

	 // hide / show table of contents for help pages
	 $('a.codetoggle').toggle(function() {
		  $(this).html('Hide Embed Code');
		  $('div.embedcode').slideToggle('fast');
	 }, function () {
		  $(this).html('Show Embed Code');
		  $('div.embedcode').slideToggle('fast');
	 });

    // show flash message and fade on click
    //$('#flashMessage').Highlight(500, '#A4C426', function () {
		  //$('body').append('<div class="fader" style="opacity: 0.5; background: transparent; background-color: #777; filter:alpha(opacity = 50); position: absolute; width: 100%; height: 100%; top: 0; left: 0; z-index: 1;"></div>');
		  //$('p.video_box').css('display: none;');
        //$(this).append('<p style="font-size: 60%; margin: 0;padding: 0;">Click here to close this message</p>');
		  //$(this).css('z-index', 10);
    //}).click(function() {
        //$(this).remove();
		  //$('.fader').remove();
    //});
	 
	 //set the initial states for user tag lists
    $('#list ul.tag_items').hide();
    $('#list ul.tag_items').before( '<a href="#" class="toggle">_</a>' );

    //add toggle behaviour for user tag lists
    $('#list .toggle').click( function(){
        $(this).toggleClass("selected");
        $(this).parent().find('ul.tag_items').toggle(500);
        return false;
	 });
	 
	 
	 $('#flashMessage').append('<p style="font-size: 60%; margin: 0;padding: 0;">Click here to close this message</p>').click(function() {
		  $(this).remove();
	 });

	 // toggle admin dash
	 $('a#admin_dash_toggle').click(function() {
		  $('div#admin_dash').slideToggle('fast');
		  if(admin_dash == 'on') {
				$.cookie('admin_dash', 'off', {expires: 7, path: '/'});
				mod_dash = 'off';
		  } else {
				$.cookie('admin_dash', 'on', {expires: 7, path: '/'});
		  }
		  return false;
	 });

	 // toggle mod dash
	 $('a#moderator_dash_toggle').click(function() {
		  $('div#moderator_dash').slideToggle('fast');
		  if(mod_dash == 'on') {
				$.cookie('mod_dash', 'off', {expires: 7, path: '/'});
				mod_dash = 'off';
		  } else {
				$.cookie('mod_dash', 'on', {expires: 7, path: '/'});
		  }
		  return false;
	 });

	 // setup sortable tables
	 $('#postmod_queue').tableSorter();
	 $('#premod_queue').tableSorter();
	 $('#published_content').tableSorter();
	 $('#published_comments').tableSorter();
	 $('#received_messages').tableSorter();

});
