/*  
	Copyright
	----------
	
	mLanguage©2009 by Gnomx.at.
	
    This file is part of InfoLink.

    mLanguage is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    mLanguage is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with mLanguage.  If not, see <http://www.gnu.org/licenses/>.
*/

	  
function setCookie(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 getCookie(name)
{
var i,n,v,carr=document.cookie.split(";");
for (i=0;i<carr.length;i++)
{
  n=carr[i].substr(0,carr[i].indexOf("="));
  v=carr[i].substr(carr[i].indexOf("=")+1);
  n=n.replace(/^\s+|\s+$/g,"");
  if (n==name)
    {
    return unescape(v);
    }
  }
}



jQuery(document).ready(function($){
	
	var dlang = getCookie('WPmlang');
	if(!dlang || dlang == ''){
		dlang = 'en';
	} 
	jQuery('.'+dlang).show();
	
	jQuery('.mlang-menu-link').click(function(){
	
		var lang = $(this).attr('data-lang');
		var img = $(this).children('img').attr('src');
		var ce = this;
		
		if(!jQuery(this).hasClass('mlang-active')){
			setCookie('WPmlang',lang,360);
			
			jQuery('.mlang-menu-link').each(function(){ 
				if(jQuery(this).hasClass('mlang-active')){
					var dimg = jQuery(this).children('img').attr('src');
					dimg = dimg.substring(0, dimg.lastIndexOf('/')+1)+'d'+dimg.substring(dimg.lastIndexOf('/')+1);
			
					//$(this).children('img').attr('src', dimg);
					jQuery(this).removeClass('mlang-active');
				}
				jQuery('.'+jQuery(this).attr('data-lang')).hide();
			});
			
			setTimeout(function(){
				img = img.substring(0, img.lastIndexOf('/')+1)+img.substring(img.lastIndexOf('/')+2);
				//$(ce).children('img').attr('src', img);
				jQuery(ce).addClass('mlang-active');
			},300);
			
			jQuery('.'+lang).show();
		}
		return false;
	});
});
