$(document).ready(function(){
//The following is a bunch of initializations
//for various uses of jQuery
var options = { min: -4, max: 0};
//Initialize the font sizer for the site.
$jquery.FontSizer.Init(options);
//Initialize the style switcher for the site
$jquery.SwitchStyle.Init();
});

$jquery = jQuery;

$jquery.FontSizer = {

        level: 0,

        options : {            
                min: -4,
                max: 0
        },

        Init : function(options) {
                if(options)
		    $jquery.FontSizer.options = $jquery.extend($jquery.FontSizer.options, options);

                //Get the current level from cookies.
                var level = ($jquery.cookie('font_level') != null && parseInt($jquery.cookie('font_level'))) ? parseInt($jquery.cookie('font_level')) : -2;                      
				// alert($.cookie('font_level'));
                //Set the font size to the current leve.
                $jquery.FontSizer.SetSize(level);

        },

        IncreaseSize : function() {

                if(($jquery.FontSizer.level) + 1 <= $jquery.FontSizer.options.max) {            
                        //If we have not exceded the max level,
                        //Get the next level and the set the size to this level.
                        var next = (parseInt($jquery.FontSizer.level) + 1);
                        $jquery.FontSizer.SetSize(next);  
                }

        },

        DecreaseSize : function() {
                if(($jquery.FontSizer.level - 1) >= $jquery.FontSizer.options.min) {
                        //If we have not exceded the min level,
                        //Get the next level and the set the size to this level.
                        var next = (parseInt($jquery.FontSizer.level) - 1);
                        $jquery.FontSizer.SetSize(next);  
                }      
        },      

        SetSize: function(level) {

                //Set the current level in the member variable and the cookie.
                $jquery.FontSizer.level = level;      
                $jquery.cookie('font_level', level, {path: '/'});

                //Work out the new em value and set it.
                var level = (level / 10) + 1;
                $jquery("body").css("fontSize", level+"em");    

        },

        Reset: function() {
            
            //Reset the level back to 0
            $jquery.FontSizer.SetSize(0);
        
        }

}; 

$jquery.SwitchStyle = {

	style: '',

	lookup: {textversion : 'http://www.maine.gov/portal/css/text.css',
		mobile : 'http://www.maine.gov/portal/css/handheld.css',
		normal : 'http://www.maine.gov/portal/css/temp.css'},

	Init : function() {
		var style = $jquery.cookie('switchable_style') != null ? $jquery.cookie('switchable_style') : $jquery.SwitchStyle.normal;
	        $jquery.SwitchStyle.SetStyle(style);
		var initialized = true;
		var minWidth=640
		if ($jquery.cookie('switchable_style') == null && (screen.width < minWidth || screen.width == "" || screen.width == null)){
			$jquery.SwitchStyle.SetMobile();
		}
	},

	SetText: function()
        {
                $jquery.SwitchStyle.SetStyle('textversion')
        },

	SetMobile: function()
        {
                $jquery.SwitchStyle.SetStyle('mobile')
        },


	SetStyle: function(styleName)
	{
		$jquery('link#switchable').attr("href", $jquery.SwitchStyle.lookup[styleName]);
		$jquery.cookie('switchable_style',styleName, {path: '/'});
	},
	
	Reset: function()
	{
		$jquery.SwitchStyle.SetStyle('normal')
	}
};

/*function getLanguage ( selectedLanguage )
	{
	  document.transLang.pageTools.value = selectedLanguage ;
	  document.transLang.submit() ;
	}

function getLanguage ( selectedLanguage )
	{
	  document.transLang.pageTools.value = selectedLanguage ;
	  document.transLang.submit() ;
	} */
	
$jquery.Translate = { 
	
	ToSpanish: function(){
		var current_url = window.location.href;
		if( current_url.indexOf("u=") > -1 ) {
			window.location = "http://www.google.com/translate_p?langpair=en|es&u=" + current_url.substr(current_url.indexOf("u=")+2);
		}
		else{
			window.location = "http://www.google.com/translate_p?langpair=en|es&u=" + current_url;
		}
	},
	
	ToFrench: function(){
		var current_url = window.location.href;
		if( current_url.indexOf("u=") > -1 ) {
			window.location = "http://www.google.com/translate_p?langpair=en|fr&u=" + current_url.substr(current_url.indexOf("u=")+2);
		}
		else{
			window.location = "http://www.google.com/translate_p?langpair=en|fr&u=" + current_url;
		}
	},
	
	ToEnglish: function(){
		var current_url = window.location.href;
		if( current_url.indexOf("u=") > -1 ) {
			window.location = current_url.substr(current_url.indexOf("u=")+2);
		}
		else{
			window.location = current_url;
		}
	}

		
};