$(document).ready(function() {

	/*$('#italian').click(function() { 
		Magaletti.setCulture("IT") ;
		Magaletti.InitAll() ;
		//Magaletti.getNewsList() ;
		$.unblockUI();
		return false; 
	}); 
 
    $('#english').click(function() { 
		Magaletti.setCulture("EN") ;
		Magaletti.InitAll() ;
		//Magaletti.getNewsList() ;
		$.unblockUI();
		return false; 
	}); 
	
	$('#german').click(function() { 
		Magaletti.setCulture("DE") ;
		Magaletti.InitAll() ;
		//Magaletti.getNewsList() ;
		$.unblockUI();
		return false; 
	});
	
	Magaletti.selectCulture() ;
	*/
	//sActualCulture = Magaletti.currentCulture ;
	//AutoBackGround(5000);
	//Magaletti.getMenu() ;
	//Magaletti.getNewsList() ;
	Magaletti.setCulture("EN") ;
	Magaletti.InitSound() ;
	Magaletti.InitAll() ;
});

function MakeRequest(template, culture, objDom, callback) 
{
	var url = "_utility/getTemplate.asp?template=" + template + "&culture=" + culture ;
	$(objDom).html("") ;
	$.ajax({
	   type: "GET",
	   url: url,
	   dataType: "xml",
	   success: function(Xml) 
	   {
			$(objDom).html($("contentHTML",Xml).text()) ;
	   }
    });
	if(callback) callback() ;
}

function MakeStaticRequest(file, culture, objDom, callback) 
{
	var url = "_static/" + file + "_" + culture + ".xml" ;
	$(objDom).html("") ;
	$.ajax({
	   type: "GET",
	   url: url,
	   dataType: "xml",
	   success: function(Xml) 
	   {
			$(objDom).html($("contentHTML",Xml).text()) ;
	   }
    });
	if(callback) callback() ;
}

var Magaletti = {

	currentCulture : null,
	currentAudioStatus : 'on',
	scrollTo : '',
	scrollToLeft : '',
	videoSection : false,
	
	setCulture : function (culture) {
		Magaletti.currentCulture = culture ;
		sActualCulture = culture ;
	},
	
	getNewsList : function () {
		MakeRequest('getNewsList', Magaletti.currentCulture, "#bodytext", null) ; 	
	},
	
	getNews : function (id) {
		MakeRequest('getNews&id=' + id, Magaletti.currentCulture, "#bodytext", null) ; 	
	},
	
	getMenu : function () {
		//$('#menu').fadeIn(500, function () { MakeStaticRequest("menu", Magaletti.currentCulture, "#menu", null) ; }) ;
		MakeStaticRequest("menu", Magaletti.currentCulture, "#menu", null) ;
	},
	
	getProduction : function (id, section) {
		$('#bodytext').fadeOut(500, function () {MakeRequest('getProduction&id=' + id + '&section=' + section , Magaletti.currentCulture, "#bodytext", function () {$('#bodytext').fadeIn(500) ;}) ;} ) ;
		if (section == "video") Magaletti.setVideoBackground() ;
		else
		{
			if (Magaletti.videoSection && section != "video")
			{
				$('#click_photo').fadeIn(500) ;
				$('#click_music').fadeIn(500) ;
				$('#nota').fadeIn(500) ;
				$('#foto_centro').css('backgroundImage', 'url(background/barra_centro.gif)') ;
				MusicAction() ;
				Magaletti.videoSection = false ;
			}
		}
		//MakeRequest('getProduction&id=' + id + '&section=' + section , Magaletti.currentCulture, "#bodytext", null) ;
	},
	
	setVideoBackground : function () {
		$('#background').fadeOut(500, function(){$('#background').css('backgroundImage', 'url(background/videobackground.jpg)');}) ;
		$('#background').fadeIn(500);
		// server per la sezione video
		Magaletti.setVideoSection() ;
	},
	
	setVideoSection : function () {
		if (Magaletti.videoSection == false)
		{
			$('#foto_centro').css('backgroundImage', 'url(background/videobackground.jpg)') ;
			$('#click_photo').fadeOut(500) ;
			$('#click_music').fadeOut(500) ;
			$('#nota').fadeOut(500) ;
			//$('#nagivation').css('navigation_video') ;
			$('#navigation .navigation_button a').css("color","white") ;
			if (Magaletti.currentAudioStatus == "on")
				MusicAction() ;
			Magaletti.videoSection = true ;
		}
	},
	
	getProductionList : function () {
		$('#bodytext').fadeOut(500, function () {MakeRequest('getProductionList' , Magaletti.currentCulture, "#bodytext", function () {$('#bodytext').fadeIn(500) ;}) ;} ) ;
		//MakeRequest('getProduction&id=' + id + '&section=' + section , Magaletti.currentCulture, "#bodytext", null) ;
	},
	
	getProductionListArchived : function () {
		$('#bodytext').fadeOut(500, function () {MakeRequest('getProductionListArchived' , Magaletti.currentCulture, "#bodytext", function () {$('#bodytext').fadeIn(500) ;}) ;} ) ;
		//MakeRequest('getProduction&id=' + id + '&section=' + section , Magaletti.currentCulture, "#bodytext", null) ;
	},
	
	getFreePage : function (freepageid) {
		$('#bodytext').fadeOut(500, function () { MakeRequest('getFreePage&freepageid=' + freepageid, Magaletti.currentCulture, "#bodytext", null ) ; } ) ;
		$('#bodytext').fadeIn(500) ;
	},
	
	getPhotoGalleryList : function () {
		MakeRequest('getPhotoGalleryList', Magaletti.currentCulture, "#bodytext", null) ; 	
	},
	
	getVideoGalleryList : function () {
		MakeRequest('getVideoGalleryList', Magaletti.currentCulture, "#bodytext", null) ; 	
	},
	
	getLoginPage : function () {
		MakeRequest('getLoginPage', Magaletti.currentCulture, "#bodytext", null) ; 	
	},
	
	getSearchPage : function () {
		MakeRequest('getSearchPage', Magaletti.currentCulture, "#bodytext", null) ; 	
	},
	
	searchProduction : function (keyword) {
		if (keyword != "")
		{
			switch (Magaletti.currentCulture)
			{
				case "IT":
					Magaletti.setSectiontitle('Risultati della ricerca');
					break;
				case "EN":
					Magaletti.setSectiontitle('Search results');
					break;
				case "DE":
					Magaletti.setSectiontitle('Suchen');
					break;
			}
			MakeRequest('searchProductions&keyword=' + keyword, Magaletti.currentCulture, "#bodytext", null) ;
		}
	},
	
	getDocumentList : function () {
		MakeRequest('getDocumentList', Magaletti.currentCulture, "#bodytext", null) ; 	
	},
	
	getPhotoGallery : function (photogalleryid) {
		MakeRequest('getPhotoGallery&photogalleryid=' + photogalleryid, Magaletti.currentCulture, "#bodytext", null) ; 	
	},
	
	getVideoProduction : function (videogallery, videotype) {
		MakeRequest('getVideoProduction&videogallery=' + videogallery + '&videotype=' + videotype, Magaletti.currentCulture, "#video_page", null) ; 		
	},
	
	getVideoGallery : function (videogallery, videotype) {
		Magaletti.setSectiontitle('');
		MakeRequest('getVideo&videogallery=' + videogallery + '&videotype=' + videotype, Magaletti.currentCulture, "#bodytext", null) ;
		window.clearInterval(ourInterval) ;
		ourInterval = 0 ;
		Magaletti.setVideoBackground() ;
		/*if (Magaletti.videoSection)
		{
			$('#click_photo').fadeIn(500) ;
			$('#click_music').fadeIn(500) ;
			$('#nota').fadeIn(500) ;
			$('#foto_centro').css('backgroundImage', 'url(background/barra_centro.gif)') ;
			if (Magaletti.currentAudioStatus == "on")
				MusicAction() ;
			Magaletti.videoSection = false ;
		}*/
	},
	
	changePhoto : function (photoSrc) {
		$('#photo_content').fadeOut(500, function () {$('#photo_content').html('<img src="_files/' + photoSrc + '" />') ;}) ;
		$('#photo_content').fadeIn(500) ;
	},
	
	InitAll : function () {
		sActualCulture = Magaletti.currentCulture ;
		AutoBackGround(40000);
		Magaletti.getMenu() ;
		Magaletti.getNewsList() ;
	},
	
	clearVideo : function () {
		try 
		{
			var oDivPlayer = document.getElementById('video_big') ;
			oDivPlayer.innerHTML = "" ;
		}
		catch (e) {}
	},
	
	restartInterval : function() {
		if (ourInterval == 0)
		{
			ChangeBackGround() ;
			AutoBackGround(40000) ;
		}
		//alert(Magaletti.videoSection) ;
		if (Magaletti.videoSection)
		{
			var oDivPlayer = document.getElementById('video_big') ;
			oDivPlayer.innerHTML = "" ;
			$('#click_photo').fadeIn(500) ;
			$('#click_music').fadeIn(500) ;
			$('#nota').fadeIn(500) ;
			$('#foto_centro').css('backgroundImage', 'url(background/barra_centro.gif)') ;
			$('#nagivation').css('navigation') ;
			MusicAction() ;
			Magaletti.videoSection = false ;
		}
	},
	
	selectCulture : function () {
		var question = $('#question')[0] ;
		$.blockUI(question, { width: '275px' }) ;
	},
	
	setSectiontitle : function (title) {
		$('#titolo').html(title) ;
	},
	
	selectMenuItem : function (objDom) {
		$('#menu .item a').removeClass('active') ;
		$('#menu .item a').addClass('lnk') ;
		$(objDom).removeClass('lnk') ;
		$(objDom).addClass('active') ;
	},
	
	changeBackGround : function (background) {
		window.clearInterval(ourInterval) ;
		ourInterval = 0 ;
		$('#background').fadeOut(500, function(){$('#background').css('backgroundImage', 'url(_files/' + background + ')');}) ;
		$('#background').fadeIn(500);
	},
	
	setPhoto : function (photo) {
		
		$('#photo_content').fadeOut(500, function(){$('#photo_content').html('<img src="_files/' + photo + '">') ;}) ;
		$('#photo_content').fadeIn(500);
	},
	
	scrollFunc : function (id, dis, pos) {
		var obj = document.getElementById(id) ;
		obj.scrollTop = obj.scrollTop + dis ;
		if (pos)
		{
			obj.scrollTop = pos ; 
		}
		else 
		{
			Magaletti.scrollTo = setTimeout( function() { Magaletti.scrollFunc(id ,dis) ; }, 10) ; 
		}
	},
	
	scrollLeftFunc : function (id, dis, pos) {
		var obj = document.getElementById(id) ;
		obj.scrollLeft = obj.scrollLeft + dis ;
		if (pos)
		{
			obj.scrollLeft = pos ; 
		}
		else 
		{
			Magaletti.scrollToLeft = setTimeout( function() { Magaletti.scrollLeftFunc(id ,dis) ; }, 10) ; 
		}
	},
	
	checkLogin : function (login, password) {
		var url = "_utility/getTemplate.asp?template=getLogin&login=" + login + "&password=" + password ;
		$.ajax({
			type: "GET",
			url: url,
			dataType: "xml",
			success: function(Xml) 
			{
				var sResult = $("contentHTML",Xml).text().replace(/\r|\n|\r\n/g, "") ;
				if (sResult == "OK") 
				{
					//$('#error').html('ok ciccio') ;
					switch (Magaletti.currentCulture)
					{
						case "IT":
							Magaletti.setSectiontitle('Lista Documenti') ;
							break;
						
						case "EN":
							Magaletti.setSectiontitle('Document List') ;
							break;
							
						case "DE":
							Magaletti.setSectiontitle('Liste Dokumente') ;
							break;
					}
					Magaletti.getDocumentList() ;
				}
				else
				{ 
					$('#error').html('username o password errati') ;
				}
			}
		});
	},
	
	scrollLeft : function () {
		var obj = document.getElementById('thumb_content') ;
		obj.scrollLeft = obj.scrollLeft - 10 ;
	},
	
	scrollRight : function () {
		var obj = document.getElementById('thumb_content') ;
		obj.scrollLeft = obj.scrollLeft + 10 ;
	},
	
	InitSound : function () {
		//soundManager.debugMode = false ;
		//soundManager.useConsole = false ;
		//soundManager.play('mySound' + iActualItem,'/test.mp3') ;
		//alert('') ;
		/*for (int = 0; i <= asSongs.length; i++)
		{
			alert(asSogns[i]) ;
		}*/
	}
}