$(document).ready(function(){
    rndHomePgBanner();
    addFileIcon();    
    tableHover();
    tableSort();  
    setArchive();
    setLightbox();  
    setupTabs();
    setupFAQ();
    setupEauth();
    collapsism();
    //menueffect();
    linkTracker();
});

/*function menueffect(){
	$('a.local-nav').each(function(){
		$('<div class="fader" />').css('opacity', 0).prependTo(this);
	}).hover(function(){
		$('.fader', this).stop().animate({
			opacity: 0.1
		}, 150);
		
		$(this).stop().animate({
			paddingLeft: 10
		}, 150);
		
	},function(){
		$('.fader', this).stop().animate({
			opacity:0
		}, 150);
		
		$(this).stop().animate({
			paddingLeft: 5
		});
	})
	
	$('a.subnav').hover(function(){
		$(this).stop().animate({
			paddingLeft: 20
		}, 150)
	},function(){
		$(this).stop().animate({
			paddingLeft: 10.5
 		}, 150)
	}
	).find('a.subnav').css('paddingLeft', 10.5)

	$('a.subnavChild').hover(function(){
		$(this).stop().animate({
			paddingLeft: 10.5
		}, 150)
	}, function(){
		$(this).stop().animate({
			paddingLeft: 20
		}, 150)	
	}).find('a.subnavChild').css('paddingLeft', 20)
}*/


function menueffect(){
	$('a.local-nav').each(function(){
		$('<div class="fader" />').css('opacity', 0).prependTo(this);
	}).hover(function(){
		$('.fader', this).stop().animate({
			opacity: 0.1
		}, 150);
		
		//$(this).stop().animate({
		//	paddingLeft: 10
		//}, 150);
		
	},function(){
		$('.fader', this).stop().animate({
			opacity:0
		}, 150);
		
		//$(this).stop().animate({
		//	paddingLeft: 5
		//});
	})
	
	$('a.subnav').hover(function(){
		$(this).css({
			//paddingLeft: 20
			backgroundColor: "#FBF4A9"
		})
	},function(){
		$(this).stop().animate({
			//paddingLeft: 10.5
			backgroundColor: "#CFC98B"
 		}, 400)
	}
	).find('a.subnav').css('backgroundColor', "#CFC98B")
	//.find('a.subnav').css('paddingLeft', 10.5)
	
	$('a.subnavChild').hover(function(){
		$(this).css({
			//paddingLeft: 10.5
			backgroundColor: "#FBF4A9"
		})
	}, function(){
		$(this).stop().animate({
			//paddingLeft: 20
			backgroundColor: "#CFC98B"
		}, 400)	
	}).find('a.subnavChild').css('backgroundColor',"#CFC98B")
	//.find('a.subnavChild').css('paddingLeft', 20)
	
}

function rndHomePgBanner(){
    if($('img#homepgbanner').length > 0){
        var totalNum = 6;
        var rndNum = rand(1,7);
        var imgPath = "../images/incident_www" + rndNum + ".jpg";
        $('img#homepgbanner').attr("src", imgPath);
    }
}

function rand(min, max){
    var offset = min;
    var range = (max - min) + 1;
    var randomNumber = Math.floor( Math.random() * range) + offset;
    return randomNumber;
}

function tableSort(){
    $("tr.zebra").removeClass();
    $.tablesorter.defaults.widgets = ['zebra'];
    
    if($("table.docbydates").length > 0){
        $(".docbydates").tablesorter({ 
            // pass the headers argument and assign an object 
            headers: { 
                // assign the first column (we start counting zero) 
                0: { 
                    // disable it by setting the property sorter to false 
                    sorter: false 
                }  
            } 
        });    
    }else {
        $(".fileTable").tablesorter();    
    }
    tableHover();
}

function tableHover(){
    $("table.fileTable tr").hover(function(){
        $(this).addClass("overRow");
    },function(){
        $(this).removeClass("overRow");
    });
}

function addFileIcon() {
   pathArray = window.location.pathname.split( '/' );
   if(pathArray[1] == 'www-aqm'){
       var sitePath = "/www-aqm/business/images/file_types/";
   } else {
       var sitePath = "/business/images/file_types/";
   }
    
    var docIcon, xlsIcon, pptIcon, pdfIcon;
    
    docIcon = sitePath + "icon_doc.gif";
    xlsIcon = sitePath + "icon_xls.gif";
    pptIcon = sitePath + "icon_ppt.gif";
    pdfIcon = sitePath + "icon_pdf.gif";
    newIcon = sitePath + "new3.gif";
	
    $("a[href$='doc']").append("<img src='" + docIcon + "' alt='file type icon' class='fileicon' />");
    $("a[href$='docx']").append("<img src='" + docIcon + "' alt='file type icon' class='fileicon' />");
    $("a[href$='rtf']").append("<img src='" + docIcon + "' alt='file type icon' class='fileicon' />");
	$("a[href$='xls']").append("<img src='" + xlsIcon + "' alt='file type icon' class='fileicon' />");
	$("a[href$='xlsx']").append("<img src='" + xlsIcon + "' alt='file type icon' class='fileicon' />");
	$("a[href$='ppt']").append("<img src='" + pptIcon + "' alt='file type icon' class='fileicon' />");
	$("a[href$='pdf']").append("<img src='" + pdfIcon + "' alt='file type icon' class='fileicon' />");
	$('a.new').append("<img src='" + newIcon + "' alt='new!' class='fileicon' />")

}

function setArchive() {
    var toggleTag;
    toggleTag = "<input type='button' value='View Archived Correspondence' id='toggleArchive' />";
    $("div.archived").before(toggleTag);
    $("input#toggleArchive").click(function(){toggleArchive();});
    setupArchive();
}

function setupArchive(){
    $("div.archived").slideToggle("slow");
}

function swapButtonLabel(){
    var button = $("input#toggleArchive")
    var label = button.val();
    if(label.indexOf('View')!= -1){
        button.val("Hide Archived Correspondence");
    } else {
        button.val("View Archived Correspondence");
    }
}

function toggleArchive() {
    setupArchive();
    swapButtonLabel();
}

function setLightbox() {
    $('a.lightbox').lightBox();
}

function setupTabs(){
    /* 
     * If Announcements tab is populated it needs to be put on top.
     * Otherwise do this...
     * Using a cookie to track which tab was last viewed.
     * Once we know, we can set that one active for the user
     * so they don't have to select it all over again.
     */
    var cookietest = ($.cookie('lastTab')) || '';    
    /*
     * Hide all but the first content block by default
     */
    $('div.tabContent').each(function(i){
       if(i != 0){
           $(this).addClass('hidden');
       }
    });
    /*
     * Add a listener to the tab links.  The listener fires the changeTabs 
     * function and then disables the mouse click event.
     */
    $('ul.tabList a.tabLink').click(function(){      
        changeTabs(this.id, this.href);
        return false;
    });    
    /*
     * If the lastTab cookie has a value, pass the value to the changeTabs 
     * function so the current tab can be made active.
     */
    if(cookietest != ''){
        changeTabs($.cookie('lastTab'), $.cookie('lastHref'));
    } else {
    	if($('div#announce dl').length > 0) {
	        changeTabs('announceTab','#announce');
	        //$('a#announceTab').addClass('update');
	    }
    }
}

function changeTabs(id,link){

        //Create new string to identify the new active tab
        var newTab = "a#" + id;
        var href = link;
        var splithref = href.split("#");
        var newhref = splithref[1];
        //Create new string to identify the active content
        var contentBlock = 'div#' + newhref;
        /*
     * Remove the active class from the current tab and apply it to the
     * one matching the passed in value 'id'.
     */
        $('a.tabLink').removeClass('active');        
        $(newTab).addClass('active');
        /*
     * Add the 'hidden' class to all contentBlock divs and then
     * removed it from the one that should be active.
     */
        $('div.tabContent').addClass('hidden');
        $(contentBlock).removeClass('hidden');
        // Set the value in the lastTab cookie to the id of the now current tab
        $.cookie('lastTab', id, { expires: 2 });
        $.cookie('lastHref', link, { expires: 2});
    
}

function setupFAQ(){	
	$("ol.faq li dl dd").each(function(){
		var height = $(this).height();
		var dd = $(this)
		dd.hide().css({height: 0});
	    dd.prev('dt').click(function(){
	    	if ( dd.is(':visible') ){
	    		dd.animate({height:0}, {duration: 750, complete: function (){
	    				dd.hide();
    				}
	    		});
	    	} else {
	    		dd.show().animate({height: height}, {duration: 750});
	    	}
	    })
	});
	
	
    
    
    $("ol.faq li dl dt").hover(
        function() {
            $(this).css({'cursor':'pointer','textDecoration':'underline'})
        },
        function() {
            $(this).css({'cursor':'default','textDecoration':'none'});
        }
    )
    
    $(".faqcontrol").bind('click', function(){
        if($(this).html() == 'Show All') { // Show All
            $(this).html('Hide All');
            $("body#faq #centerCol dd").css({height: 'auto'}).slideDown("fast");            
            $(this).toggleClass("open");
        } else { //Hide All
            $(this).html('Show All');
            $("body#faq #centerCol dd").css({height: 0, display: 'none'}).slideUp("fast");
            $(this).toggleClass("open");
        }        
        
        
        
    }).hover(
       function() {
            $(this).css({'cursor':'pointer','textDecoration':'underline'})
        },
        function() {
            $(this).css({'cursor':'default','textDecoration':'none'});
        } 
    );
    
    $('#centerCol ol.faq dd').wrapInner("<div></div>");

}

function setupEauth(){
    $(document).ready(function(){        
        $("h3.eauthq").bind('click',function(){
            $(this).next().slideToggle("slow");
            $(this).toggleClass("open");
        });
    });
    
    $("h3.eauthq").hover(
        function() {
            $(this).css({'cursor':'pointer','textDecoration':'underline'})
        },
        function() {
            $(this).css({'cursor':'default','textDecoration':'none'});
        }
    )
}

function collapsism(){
    $(document).ready(function(){        
        $(".collapsor").bind('click',function(){
            $(this).next().toggle();
            $(this).toggleClass("open");
        });
    });
    
    $(".collapsor").hover(
        function() {
            $(this).css({'cursor':'pointer','textDecoration':'underline'})
        },
        function() {
            $(this).css({'cursor':'default','textDecoration':'none'});
        }
    );
}

function linkTracker(){
	var fileTypes = ['doc','xls','pdf','docx','xlsx','wmv'];
	
	$('a').each(function() {
	  var $a = $(this);
	  var href = $a.attr('href');
		
	  var hrefArray = href.split('.');
	  var extension = hrefArray[hrefArray.length - 1];
	  if ($.inArray(extension,fileTypes) != -1) {
	    $a.click(function() {
	      pageTracker._trackPageview('/documents/' + href);
	    });
	  }
	 
	});

}
