var siteVersions = {"normal": "normal", "visuallyImpaired": "visuallyImpaired", "blind": "blind"};

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 = "";
	cookieString = name + "=" + value + expires + "; path=/";
	document.cookie = cookieString;
}

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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function adjustCSS(siteLanguage){
	var siteVersionCookie = readCookie("site_version");
	if (siteVersionCookie){
	  if (siteVersionCookie==siteVersions["normal"]){
		if (siteLanguage=="arabic"){
			$("style_sheet").href = "/stylesheets/layout-ar.css";
		}else{
			$("style_sheet").href = "/stylesheets/layout-en.css";
		}
	  }else if (siteVersionCookie==siteVersions["visuallyImpaired"]){
		if (siteLanguage=="arabic"){
			$("style_sheet").href = "/stylesheets/layout-impaired-ar.css";
		}else{
			$("style_sheet").href = "/stylesheets/layout-impaired-en.css";
		}
	  }else if (siteVersionCookie==siteVersions["blind"]){
		$("style_sheet").href = "/stylesheets/layout-blind-ar.css";
		$('blind_link').innerHTML = "";
	  }
	}else{
		$("style_sheet").href = "/stylesheets/layout-ar.css";
		createCookie("site_version",siteVersions["normal"],400);
	 }
}

function switchVersion(siteLanguage, version){
	createCookie("site_version", version, 400);
	adjustCSS(siteLanguage);
}

function rotateBanner(){
	var banner = $('side_ad');
	if (banner){
		var ads = banner.childElements();
		var index = Math.floor(Math.random() * ads.length);
		for (i=0; i<ads.length; i++){
			ads[i].style.display = 'none';
		}
		ads[index].style.display = 'block';
	}
}

function getEvents(language, date){
	new Ajax.Request( "/events/day_events", { method: 'get', parameters:{'date': date, 'language': language} } );
}

function switchLanguage(language){
	if ( language == 'en' ){
		var newLocation = window.location.href.replace("/ar", "/en");
	}else{
		var newLocation = window.location.href.replace("/en", "/ar");
	}
	window.location = newLocation;
}

sound_playing = null;
function toggleSound(soundId, soundURL, lang){

  Sound.play('',{replace:true}); 
  Sound.disable();
  if(sound_playing != null) {
	$('span-'+sound_playing).className = "guestbookAudio";
	if(lang=="en"){
		$('link-'+sound_playing).title = 'Listen to Audio comment'; 
		$('link-'+sound_playing).innerHTML = 'Listen to Audio comment';
	}else{
		$('link-'+sound_playing).title = 'استمع إلى تعليق صوتي'; 
		$('link-'+sound_playing).innerHTML = 'استمع إلى تعليق صوتي';
	}
  }
  
  if(sound_playing == soundId) {
    sound_playing = null;
  }
  else {
	Sound.enable(); 
	Sound.play(soundURL);
    sound_playing = soundId;
	$('span-'+sound_playing).className = "guestbookAudio-stop";
	if(lang=="en"){
		$('link-'+sound_playing).title = 'Stop'; 
		$('link-'+sound_playing).innerHTML = 'Stop';
	}else{
		$('link-'+sound_playing).title = 'اغلق الصوت'; 
		$('link-'+sound_playing).innerHTML = 'اغلق الصوت';
	}
  }
}

