//calendar global variables definitions
var date = new Date();
var month = '';
var daysInMonth = '';
var currentYear = date.getFullYear();
var nextYear = currentYear + 1;
var previuousYear = currentYear - 1;

function clear(objectId)
{
    document.getElementById(objectId).value = "";
}

function ErrorResponse(response, textStatus, errorThrown)
{
    alert("Errore nella gestione dei dati XML!");
}

function GetDayTooltipData(day, month, year)
{
   $.ajax({ url: "tooltip.php?month="+month+"&day="+day+"&year="+year, success: SetDayCalendarTooltipContent, dataType: 'xml', error: ErrorResponse});
}

function GetCalendarShowsDaysList(month, year)
{
  month = month != '' ? month : (date.getMonth() + 1);
  daysInMonth = GetDaysInMonth(month);
  
  $.ajax({ url: "calendar.php?month="+month+"&year="+(year != null ? year : currentYear), success: SetCalendarDaysList, dataType: 'xml', error: ErrorResponse});
}

function SetDayCalendarTooltipContent(xmlData)
{
   var toolTipContentContainer = $('.tooltipContentContainer');
   
   if($(xmlData).find('shows').children().size() > 0)
   {
        toolTipContentContainer.children().remove();
        var detailContent = $('<div class="tooltipDetailsItemContent"></div>');
       
        for(var i = 0; i < $(xmlData).find('shows').children().size(); i++)
        {
           var seasonTitle = ($(xmlData).find('shows').find('seasonTitle')[i].textContent != null ? $(xmlData).find('shows').find('seasonTitle')[i].textContent : $(xmlData).find('shows').find('seasonTitle')[i].text);
           var showTitle = ($(xmlData).find('shows').find('title')[i].textContent != null ? $(xmlData).find('shows').find('title')[i].textContent : $(xmlData).find('shows').find('title')[i].text);
           var showSubtitle = ($(xmlData).find('shows').find('subtitle')[i].textContent != null ? $(xmlData).find('shows').find('subtitle')[i].textContent : $(xmlData).find('shows').find('subtitle')[i].text);
           var showImage = ($(xmlData).find('shows').find('imageFileName')[i].textContent != null ? $(xmlData).find('shows').find('imageFileName')[i].textContent : $(xmlData).find('shows').find('imageFileName')[i].text);
           var showDate = ($(xmlData).find('shows').find('customDate')[i].textContent != null ? $(xmlData).find('shows').find('customDate')[i].textContent : $(xmlData).find('shows').find('customDate')[i].text);
           var showStartDate = ($(xmlData).find('shows').find('startDate')[i].textContent != null ? $(xmlData).find('shows').find('startDate')[i].textContent : $(xmlData).find('shows').find('startDate')[i].text);
           var showEndDate = ($(xmlData).find('shows').find('endDate')[i].textContent != null ? $(xmlData).find('shows').find('endDate')[i].textContent : $(xmlData).find('shows').find('endDate')[i].text);
           
           var category = $('<div class="tooltipOnStageDate"></div>').append(seasonTitle);
           var title = $('<div class="tooltipMainTitle"></div>').append(showTitle);
           var subtitle = $('<div class="tooltipMainSubtitle"></div>').append(showSubtitle);
           var image = $('<div class="tooltipImagePreviewContainer"><img alt="'+ showTitle +'" title="'+ showTitle +'" src="media/shows/thumbnail/'+ showImage +'"/></div>');
           var dateLabel = $('<div class="tooltipTextContent"></div>').append(showDate);
           var onStageStartDate = $('<div class="tooltipOnStageDate">in scena dal: </div>').append(showStartDate);
           var onStageEndDate = $('<div class="tooltipOnStageDate">fino al: </div>').append(showEndDate);
           
           //detailContent.append(image);
           //detailContent.append(title);
           //detailContent.append(subtitle);
           //detailContent.append(showDate);
           //detailContent.append(onStageStartDate);
           //detailContent.append(onStageEndDate);
           
           var detailDataContainerTable = $('<table class="detailDataContainerTable"></table>');
           var detailDataContainerRow = $('<tr></tr>');
           var dateailDataContainerFirstCell = $('<td class="tooltipImagePreviewContainer"></td>');
           var dateailDataContainerSecondCell = $('<td></td>');
           
           dateailDataContainerFirstCell.append(image);
           dateailDataContainerSecondCell.append(category);
           dateailDataContainerSecondCell.append(title);
           dateailDataContainerSecondCell.append(subtitle);
           //dateailDataContainerSecondCell.append(showDate);
           
           detailDataContainerRow.append(dateailDataContainerFirstCell);
           detailDataContainerRow.append(dateailDataContainerSecondCell);
           
           detailDataContainerTable.append(detailDataContainerRow);
           detailContent.append(detailDataContainerTable);
           
        }
        
        toolTipContentContainer.append(detailContent);
   }
   
}

function GetLoader()
{
   return $('<div class="tooltipContentContainer"><img class="tooltipLoader" alt="Attendere..." title="Attendere..." src="media/loader.gif"/></div>');
}

function GetDaysInMonth(month)
{
    var days = '';
    
    if(month == 2)
    {
        if((currentYear % 4) == 0)
            days = 29;
        else
            days = 28; 
    }
    else if(month == 4 || month == 6 || month == 9 || month == 11)
    {
        days = 30;
    }
    else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
    {
        days = 31;
    }
   
   return days;
}
 
function SetCalendarDaysList(daysListXML)
{
    var returnedDays = Array();
	
	$(daysListXML).find('days').find('day').each(function(){
		returnedDays.push($(this).text());
	});
	
    //clear calendarContent
	$("div#monthLabelContainer").html($(daysListXML).find('month').find('italianName').text());
		
	  if($("#daysListContainer").children().length > 0)
        $("#daysListContainer").children().remove();
    //end 
	Cufon.refresh();
	Cufon.replace('div#monthLabelContainer'); 
	Cufon.now();	
    
    for(var i = 1; i <= daysInMonth; i++)
	{
		var currentDayText = (i.toString().length < 2 ? "0" + i.toString() : i.toString());
		var currentDayLink = $('<a href="#" class="calendarDayLink"/>');
		var monthNumber = parseInt($(daysListXML).find('month').find('number').text());
		var yearValue = $(daysListXML).find('month').find('year').text();
		currentYear = parseInt(yearValue);
		
		var dayContainer = $('<div class="calendarDayItem"/>');
		
		if($.inArray(currentDayText, returnedDays) > -1)
		{
			$(currentDayLink).append(currentDayText);
			$("#daysListContainer").append(dayContainer.append(currentDayLink));
		}
		else
		{
			$("#daysListContainer").append(dayContainer.html(currentDayText));
		}
		
		
		currentDayLink.attr('href', "index.php?content=13&day="+currentDayText+"&month="+monthNumber+"&year="+yearValue+"");
		
		currentDayLink.tooltip({
			delay: 0, 
			showURL: false,
			opacity: 1,
			bodyHandler: function(){
				GetDayTooltipData($(this).text(), monthNumber, yearValue);
				return GetLoader();
			}});
		
	}
        
	//update month selector link
	 var previousMonthValue = ((monthNumber - 1) == 0 ? 12 : monthNumber - 1);
	 var nextMonthValue = ((monthNumber + 1) > 12 ? 1 : monthNumber + 1);
	 
	 if(monthNumber == 12)
	 {
		nextYear = currentYear + 1;
		previuousYear = nextYear - 1;
		
		$("#previousMonthSelector").attr('href', 'javascript:GetCalendarShowsDaysList('+ previousMonthValue + ', ' + currentYear + ')');
		$("#nextMonthSelector").attr('href', 'javascript:GetCalendarShowsDaysList('+ nextMonthValue + ', ' + nextYear + ')');
		
	 }
	 else if(monthNumber == 1)
	 {
		nextYear = currentYear + 1;
		previuousYear = currentYear - 1;
		
		$("#previousMonthSelector").attr('href', 'javascript:GetCalendarShowsDaysList('+ previousMonthValue + ', ' + previuousYear + ')');
		$("#nextMonthSelector").attr('href', 'javascript:GetCalendarShowsDaysList('+ nextMonthValue + ', ' + currentYear + ')');
	 }
	 else
	 {
		nextYear = currentYear + 1;
		previuousYear = currentYear - 1;
		
		$("#previousMonthSelector").attr('href', 'javascript:GetCalendarShowsDaysList('+ previousMonthValue + ', ' + currentYear + ')');
		$("#nextMonthSelector").attr('href', 'javascript:GetCalendarShowsDaysList('+ nextMonthValue + ', ' + currentYear + ')');
	 }
	 
	 
	//end  
        
}

function sendFormData(inputId, formIndex)
{
    document.getElementById(inputId).value = 1;
    document.forms[formIndex].submit();
}

function operationCompletedProcedureFeedBack(message, pageId)
{
    alert(message);
    document.location.href='index.php?content=' + pageId;
}

function validateSMSInscripritionForm()
{
    var booleanReturn = false;
    var errorMessage = "Attenzione!\n";
    
    if(document.getElementById('name').value != null && document.getElementById('name').value != "")
    {
        booleanReturn = true;
    }
    else
    {
        booleanReturn = false;
        errorMessage += "\nIl Campo nome non puņ essere vuoto.";
    }
    
    if(document.getElementById('surname').value != null && document.getElementById('surname').value != "")
    {
        booleanReturn = true;
    }
    else
    {
        booleanReturn = false;
        errorMessage += "\nIl Campo cognome non puņ essere vuoto.";
    }
    
    if(document.getElementById('phone').value != null && document.getElementById('phone').value != "")
    {
        booleanReturn = true;
    }
    else
    {
        booleanReturn = false;
        errorMessage += "\nIl Campo cellulare non puņ essere vuoto.";
    }
    
    if(document.getElementById('phoneConfirm').value == document.getElementById('phone').value && document.getElementById('phone').value != "")
    {
        booleanReturn = true;
    }
    else
    {
        booleanReturn = false;
        errorMessage += "\nI campi cellulare e conferma cellulare non coincidono.";
    }
    
    var mobilePhonePattern = /^([1-9\+]{1})([0-9])*$/;/*/^3\d{8,}$/g;*/
    if(document.getElementById("phone").value.match(mobilePhonePattern) == null)
    {
        booleanReturn = false;
        errorMessage += "\nFormato numero telefonico non valido";
    }
    
    if(booleanReturn)
    {
       return booleanReturn;
    }
    else
    {
        alert(errorMessage);
        return booleanReturn;
    }
}

function validateNewsletterInscripritionForm()
{
    var booleanReturn = false;
    var errorMessage = "Attenzione!\n";
    
    if(document.getElementById('name').value != null && document.getElementById('name').value != "")
    {
        booleanReturn = true;
    }
    else
    {
        booleanReturn = false;
        errorMessage += "\nIl Campo nome non puņ essere vuoto.";
    }
    
    if(document.getElementById('surname').value != null && document.getElementById('surname').value != "")
    {
        booleanReturn = true;
    }
    else
    {
        booleanReturn = false;
        errorMessage += "\nIl Campo cognome non puņ essere vuoto.";
    }
    
    if(document.getElementById('mail').value != null && document.getElementById('mail').value != "")
    {
        booleanReturn = true;
    }
    else
    {
        booleanReturn = false;
        errorMessage += "\nIl Campo mail non puņ essere vuoto.";
    }
    
    if(document.getElementById('mailConfirm').value == document.getElementById('mail').value && document.getElementById('mail').value !=  "")
    {
        booleanReturn = true;
    }
    else
    {
        booleanReturn = false;
        errorMessage += "\nI campi e-mail e conferma e-mail non coincidono.";
    }
    
    if(booleanReturn)
    {
       return booleanReturn;
    }
    else
    {
        alert(errorMessage);
        return booleanReturn;
    }
}

function isEnterButtonPressed(e)
{
	var event = e ? e : window.event;
	if (event.keyCode == 13) 
		return true;
	else
		return false;	
}

function startSearch(valueToFind)
{
    if(valueToFind != '' && valueToFind != 0 && valueToFind != null)
    {
       document.getElementById('valueToFind').value = valueToFind;
       document.customSearchForm.submit();
    }
}

function StartDumbCrossFadeEffect(itemsToApplyTheEffect)
{
    $(function() {
	    $(itemsToApplyTheEffect).dumbCrossFade({'slideType':'slidevertical','fadeInOut':true, 'showTime':'3000'});
	});
}

function GetFlickrData()
{
    $.ajax({ url: "flickrDataHandler.php", async: true, success: SetFlickrPanelContent, dataType: 'xml', error: ErrorResponse, timeout: 15000});
}

function SetFlickrPanelContent(xmlData)
{
   var panelContainer = $('.flickrPanelContent');
   
   var contentContainer = $('<table class="flickrPanelContentContainer"></table>');
   var contentContainerRow = $('<tr></tr>');
   
   var contentImageItemContainer = $('<td class="flickrPhotoContainer"></td>');
   var imageItemLink = $('<a class="flickrPhotoLink" target="_blank"></a>');
   var imageItem = $('<img />');
   
   var contentTextItemsContainer = $('<td class="flickrTextContainer"></td>');
   var contentTextTitle = $('<div class="flickrTitleContainer"></div>');
   var contentTextContent = $('<div class="flickrTextContainer"></div>');
   
   if($(xmlData).find('photo').children().size() > 0)
   {
       panelContainer.children().remove();
       
       var panelTitle = ($(xmlData).find('photo').find('title')[0].textContent != null ? $(xmlData).find('photo').find('title')[0].textContent : $(xmlData).find('photo').find('title')[0].text);
       var panelContent = ($(xmlData).find('photo').find('description')[0].textContent != null ? $(xmlData).find('photo').find('description')[0].textContent : $(xmlData).find('photo').find('description')[0].text);
       var image = ($(xmlData).find('photo').find('image')[0].textContent != null ? $(xmlData).find('photo').find('image')[0].textContent : $(xmlData).find('photo').find('image')[0].text);
       var link = ($(xmlData).find('photo').find('webContent')[0].textContent != null ? $(xmlData).find('photo').find('webContent')[0].textContent : $(xmlData).find('photo').find('webContent')[0].text);
       var type = ($(xmlData).find('photo').find('type')[0].textContent != null ? $(xmlData).find('photo').find('type')[0].textContent : $(xmlData).find('photo').find('type')[0].text);
       
       imageItemLink.attr('href', link);
       
       imageItem.attr('alt', panelTitle);
       imageItem.attr('title', panelTitle);
       imageItem.attr('src', image);
       
       imageItemLink.append(imageItem);
       contentImageItemContainer.append(imageItemLink);
       
       contentTextTitle.text(panelTitle);
       contentTextContent.text(panelContent);
       
       contentTextItemsContainer.append(contentTextTitle);
       contentTextItemsContainer.append(contentTextContent);
       
       contentContainerRow.append(contentImageItemContainer);
       contentContainerRow.append(contentTextItemsContainer);
       contentContainer.append(contentContainerRow);
       
       panelContainer.append(contentContainer);
   }
}

function initializeUbuBanner()
{
	var banner = $('div#ubuBannerItemsContainer');
	banner.height($(document).height());
	banner.width($(document).width());
	banner.show('slow');
	$('img#ubuBannerCloseButton').click(function(){
		banner.hide('slow');
	});
	
}
