// ======================
//    Table Tabs
// ======================

function table_tabs(){
	// Hide Content Tables
	$("div.tables > table").hide();
	$("ul.tabs").tabs("div.tables > table", { 
		// Mouse Event
		event: 'click', 
		// Current Tab Active State
		current: 'active',
		//selector used for tabs
		tabs: 'a',
		// Transition effect
		effect: 'default'
	}).history();
	$(".table_header").tabs("div.prices > div", { 
		// Mouse Event
		event: 'click',
		// Current Tab Active State
		current: 'active',
		//selector used for tabs
		tabs: 'a', 
		// Transition effect
		effect: 'default'
	}).history();
}

// ======================
//    Equalize column sizes in product matrix
// ======================

function equal_height_columns() {
	var highestColRow1 = Math.max($(".matrix_widget:eq(0)").height(),$(".matrix_widget:eq(1)").height());
	$(".matrix_widget:eq(0), .matrix_widget:eq(1)").height(highestColRow1);
	
	var highestColRow2 = Math.max($(".matrix_widget:eq(2)").height(),$(".matrix_widget:eq(3)").height());
	$(".matrix_widget:eq(2), .matrix_widget:eq(3)").height(highestColRow2);
	
	var highestColRow3 = Math.max($(".matrix_widget:eq(4)").height(),$(".matrix_widget:eq(5)").height());
	$(".matrix_widget:eq(4), .matrix_widget:eq(5)").height(highestColRow3);
	
}

// ======================
//    Downloads
// ======================
                     
function downloads() {
	if ( $('a[href$=.pdf], a[href$=.xls], a[href$=.doc], a[href$=.ppt], a[href$=.xlsx], a[href$=.docx], a[href$=.pptx]', '#content_col2').length > 0 ) { 
    	// First, create the Downloads widget container after the side nav
		$('#side_nav_wrapper', '#content_col1').after('<div class="widget_wrapper downloads"></div>');
		// flesh out the container
		$('div.downloads').append('<div class="widget_wrapper_top"><div class="widget_wrapper_bottom downloads_bottom"><h3>Downloads</h3><ul></ul></div></div>');

		// find each downloadable file and loop through them
		$('a[href$=.pdf], a[href$=.xls], a[href$=.doc], a[href$=.ppt]', '#content_col2').each(function() {
			//create a variable of 'icon-(file extension)'
			var link_ext = 'icon icon-' + $(this).attr('href').substr(($(this).attr('href').length-3),3);
			//clone the link
			var link = $(this).clone(true).addClass(link_ext);
			//add the link extension variable to the link 
			$(this).addClass(link_ext);
			$(this).addClass("download")
			//add the new link to the downloads list in the sidebar
			$('div.downloads_bottom ul').append(link);
                  
         });
         // wrap the new link in <li> tags
         $('div.downloads_bottom ul > a').wrap('<li></li>');
                           
         // Get filesize and add after link title - borrowed from Paul Grenier http://www.endusersharepoint.com/?p=1041
         // function to find the filesize and make it human readable
		 function hdrDetails(i, elm, cl) {
        	cl = cl/1024;  //divide content-length by 1024 (KB)
            var sz = cl>1024?"MB":"KB";  //if cl is still big, set to MB
            cl = cl>1024?cl/1024:cl;  //if cl is still big, divide again
            var len = $(elm).eq(i).text().length;  //check the link's text length
            if(len > 0) {
            	//add a file size - toFixed # sets the number of decimals shown
                $(elm).eq(i).append('<span class="filesize">&nbsp;<NOBR>('+cl.toFixed(0)+' '+sz+')</NOBR></span>');
            }
         }
         // find the files and put them in a var
		 $(function() {
         	var elm="a[href$='.pdf'],"+ //only the file types we want
                    "a[href$='.doc'],"+
                    "a[href$='.ppt'],"+
                    "a[href$='.xls'],"+
                    "a[href$='.docx'],"+ //don't forget 2007 versions
                    "a[href$='.pptx'],"+
                    "a[href$='.xlsx']";
            $(elm, '#content_wrapper').each(function(i, e) {
            	//query the document host and run the filesize function if the file is hosted locally
				if (e.hostname && e.hostname == location.hostname) {
                	$.ajax({
                    	type: "HEAD",
                        url: $(this).attr("href"),
                        complete: function(xhr, textStatus) {
                        	var cl=xhr.getResponseHeader("content-length");
                            if(textStatus=="success") {
                            	var cl=xhr.getResponseHeader("content-length");
                                hdrDetails(i, elm, cl); //call the calculation fn
                            }else{
                            	$(elm).eq(i).after("&nbsp;");
                            }
                        }
                    });
                 }
            });
        });
     }
}
							
							
$(document).ready(function(){
	
	/* Initialize Table Tabs */
	table_tabs();
	/* Initialize Downloads Widget */
	//downloads();
	//equal_height_columns();

});