function init() {
	$('.scrollbar').jScrollPane({showArrows:true, scrollbarWidth: 17});
}



$(document).ready(function(){
    setupPrintLinks();
    setupFileTypeMenu();
});
/**
 * replacePrintLinks
 */
//function replacePrintLinks() {
//    var textModules = $('.textmodule p');
//    textModules.filter(function(){
//        return $(this).text().indexOf('[print_link]') != -1;
//    }).css('color', 'red');
//}

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}



cc = new Object();

cc.scroller = {

	count: 0,
	itemHeight: 0,
	visibleHeight: 0,
	base_id: '',
	curently_displayed: 0,
	
	init: function(count, itemHeight, visibleHeight, base_id) {
		cc.scroller.count = count;
		cc.scroller.itemHeight = itemHeight;
		cc.scroller.visibleHeight = visibleHeight;
		cc.scroller.base_id = base_id;
		
		$('.'+cc.scroller.base_id+'_up').click(cc.scroller.click_up);
		$('.'+cc.scroller.base_id+'_down').click(cc.scroller.click_down);
		$('#video_listing_dropdown').change(cc.scroller.switchView);
	},
	
	reInit: function() {
		cc.scroller.init(cc.scroller.count, cc.scroller.itemHeight, cc.scroller.visibleHeight, cc.scroller.base_id);
		cc.scroller.curently_displayed = 0;
	},
	
	click_up: function(e) {
		if(e && e.preventDefault) e.preventDefault();
		else if (window.event && window.event.returnValue) window.eventReturnValue = false;
		pos = $('#'+cc.scroller.base_id).position();
		if(pos.top >= 0) return;
		cc.scroller.curently_displayed--;
		newtop = 0 - cc.scroller.curently_displayed * cc.scroller.itemHeight;
		$('#'+cc.scroller.base_id).animate({top: newtop+'px'});
	},
	
	click_down: function(e) {
		if(e && e.preventDefault) e.preventDefault();
		else if (window.event && window.event.returnValue) window.eventReturnValue = false;
		pos = $('#'+cc.scroller.base_id).position();
		if(pos.top <= -1 * cc.scroller.count * cc.scroller.itemHeight + cc.scroller.visibleHeight) return;
		cc.scroller.curently_displayed++;
		newtop = 0 - cc.scroller.curently_displayed * cc.scroller.itemHeight;
		$('#'+cc.scroller.base_id).animate({top: newtop+'px'});
	},
	
	switchView: function(e) {
		className = this.options[this.selectedIndex].value;
		$('#'+cc.scroller.base_id+' .video_listing_item').css('display', 'none');
		$('#'+cc.scroller.base_id+' .video_listing_item.'+className).css('display', 'block');
	}

};




var originalFontSize = null;
// Reset Font Size
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}				
originalFontSize = $('html').css('font-size');
originalFontSize = parseFloat(originalFontSize, 10);
originalLineHeight = $('html').css('line-height');
originalLineHeight = parseFloat(originalLineHeight, 10);

function original(){
	var newFontSize = originalFontSize*1.0;
	$('html').css('font-size', newFontSize);
	$('.normal_size').attr("class", "normal_size_on");
	$('.bigger_size_on').attr("class", "bigger_size");
	$('.biggest_size_on').attr("class", "biggest_size");
	createCookie("cookieFontSize","original",30);
	return false;
};
function bigger(){
	var newFontSize = originalFontSize*1.2;
	$('html').css('font-size', newFontSize);
	$('.normal_size_on').attr("class", "normal_size");
	$('.bigger_size').attr("class", "bigger_size_on");
	$('.biggest_size_on').attr("class", "biggest_size");
	createCookie("cookieFontSize","bigger",30);
	return false;
};
function biggest(){
	var newFontSize = originalFontSize*1.4;
	$('html').css('font-size', newFontSize);
	$('.normal_size_on').attr("class", "normal_size");
	$('.bigger_size_on').attr("class", "bigger_size");
	$('.biggest_size').attr("class", "biggest_size_on");
	createCookie("cookieFontSize","biggest",30);
	return false;
};
function setTextSize(){
	var currentFontSize = readCookie("cookieFontSize");
	if (currentFontSize == "original")
		original();
	else if (currentFontSize == "bigger")
		bigger();
	else if (currentFontSize == "biggest")
		biggest();
};

/**
 * printPage
 */
function printPage() {
    print();
}


/**
 * setupPrintModuleLinks
 */
function setupPrintLinks() {
    var links = $("a[href$='printModule']");
    links.bind('click', function(e){
        e.preventDefault();
        moduleContent = $(this).parents('.module.textmodule:first').html();
        printWindow = window.open();
        printWindow.document.write('<link rel="stylesheet" type="text/css" href="/css/site.generic.css" />');
        printWindow.document.write('<style>body {background: none; margin: 0 0 40px 20px;}</style>');
        printWindow.document.write(moduleContent);
        printWindow.document.close();
        printWindow.print();
        printWindow.close();
    });

    var links = $("a[href$='printPage']");
    links.bind('click', function(e){
        e.preventDefault();
        print();
    });

}



function setupFileTypeMenu() {
    var menu = $('#transfer_category_selector');
    if(menu.length == 0) {
        return;
    }
    
    menu.bind('change', function() {
        var item = menu.find('option:selected');
        var val = item.val();
        
        log('menu has changed! ' , val);
        if(val == 0) {
            window.location = '/download/';
        } else {
//            window.location = '/download/' + val + '/';
            window.location = '/download/' + val;
        }
    });
}




window.log = function(){
    log.history = log.history || [];   // store logs to an array for reference
    log.history.push(arguments);
    if(this.console){
        console.log( Array.prototype.slice.call(arguments) );
    }
};

