/*///////////////////////////////////////////////////////////////////
    File        ipak.js
    Author      Richard Leland
    Purpose     Site-wide UI javascript enhancements
    Requires    jquery 1.2.6
                jqueryUI 1.5.2
///////////////////////////////////////////////////////////////////*/

// TODO: shadowbox code, and Bob's XHR code

$(document).ready(function(){
    
    // navigation accordion
    // TODO: figure out how to have the accordion not animate on page load
    $('#nav-channel').accordion({
        header: 'a.toggler-link',
        autoHeight: false,
        alwaysOpen: false,
        active: false
    });
    // open the appropriate accordion item
    $('#nav-channel').accordion('activate', 'a.current');
    
    // determine if we're on the login page
    if($('body').hasClass('login')){
        var emailField = $('#id-email');
        var passField = $('#id-password');
        
        // hide the error msgs initially
        $('.error-msg').hide();
        
        // focus on the username field
        emailField.focus();
        
        // perform some simple validation
        $('#frm-login').submit(function(){
            var emailValue = jQuery.trim(emailField.val());
            var passValue = jQuery.trim(passField.val());
            
            if(emailValue != '' && passValue != ''){
                return true;
            } else {
                if(emailValue == ''){
                    $('#id-email-msg').fadeIn("medium").fadeOut(2000);
                    emailField.focus();
                }
                if(passValue == ''){
                    $('#id-password-msg').fadeIn("medium").fadeOut(2000);
                }
                return false;
            }
        });
    }
    
    // determine if the page has the search field
    if($('#id-search')){
        var strSearchField = 'Search...';
        var searchField = $('#id-search');
        // tell the search field what to do onfocus
        searchField.bind('focus', function(){
            if(this.value == strSearchField) this.value = '';
        });
        // tell the search field what to do onblur
        searchField.bind('blur', function(){
            if(this.value == '') this.value = strSearchField;
        });
    }
    
    // tabs for the programming highlights pages
    if($('#tabs-container')){
        $('#tabs-container > ul').tabs();
    }
    
    // Open the schedule in a new window
    $('a[href*=includes/schedule.cfm]').click(function(event) {
    	var dialogdiv;
    	
    	// Prevent the link from opening by itself
    	event.preventDefault();
    	
		// Load the dialog contents and create the dialog
    	dialogdiv = $("<div id='schedule'/>").load($(this).attr("href")).dialog({
			width: '943px',
			height: '90%',
			modal: true,
			resizable: false,
			draggable: false,
			bgiframe:true,
			overlay: {
				opacity: 0.5,
				background: "black"
			}
		});
    	
    	// Explicitly set the height of the #schedule div.
    	$("#schedule").height("100%");
    	
    	// Get rid of the dialog's close button
    	$(".ui-dialog-titlebar-close > span").empty();
    	
    	// When the schedule dialog is open, add the schedule-open class to the body tag (for print layout)
    	$("body").addClass("schedule-open");
    });
    
    $('#flash-adn').each(function() {
    	$(this).html('<iframe src="/systemimages/ADN/adndownload.cfm?networkid=' + $('#networkid').text() + '" width="620" height="595" frameborder="0" scrolling="no"></iframe>');
    });
});