//This file is not part of the jQuery library, but is needed to configure the available elements of jQuery
//Use jQuery in no conflict mode. Conflicts with mootools
jQuery.noConflict();
function dateToString(aDate, aFormat){
	var intDate = aDate.getDate();
	var intMonth = aDate.getMonth() + 1; //strangest thing January is month 0 not 1
	var intYear = aDate.getFullYear();
	aFormat = aFormat.toLowerCase();
	var strResult = '';
	
	for (var x = 0; x < aFormat.length; x++){
		if (aFormat.substr(x, 2) == 'dd') {				
			x++; //skip next char
			if (intDate < 10) intDate = '0' + intDate;
			strResult = strResult + intDate;
		}
		else if (aFormat.substr(x, 2) == 'mm') {		
			x++;
			if (intMonth < 10) intMonth = '0' + intMonth;
			strResult = strResult + intMonth;
		}
		else if (aFormat.substr(x, 4) == 'yyyy') {
			x = x + 3;
			strResult = strResult + intYear;
		}
		else if (aFormat.substr(x, 1) == 'd') {
			strResult = strResult + intDate;			
		}
		else if (aFormat.substr(x, 1) == 'm') {
			strResult = strResult + intMonth;
		}
		else {
			strResult = strResult + aFormat.substr(x, 1);
		}
	}
	return strResult;
}
jQuery(function(){	
	// Tabs
	//jQuery('#tabs').tabs();	not needed yet. When needed then un-comment this
	
	//Accordion
	jQuery("#accordion").accordion({
		collapsible: true,
		autoHeight: false
	});

	//Accordion in products
	jQuery("#accordion_main").accordion({
		collapsible: true,
		autoHeight: false
	});
	// Dialog			
	jQuery('#dialog').dialog({
		autoOpen: false,
		width: 600,
		modal: true,
		title: 'Γιορτάζουν:'
		/* we do not need to display any buttons
		buttons: {
			"Ok": function() { 
				$jQuery(this).dialog("close"); 
			}, 
			"Cancel": function() { 
				$jQuery(this).dialog("close"); 
			} 
		}*/
	});
	
	// Dialog Link
	/*jQuery('#dialog_link').click(function(){
		jQuery('#dialog').dialog('open');
		return false;
	});*/
	
	//Dialog popup in some products	
	jQuery('#dlgPopup').dialog({
		autoOpen: false,
		width: 800,
		modal: true
	});

	//The link/button which will cause the dlgPopup to be shown
	jQuery('#dlgPopup_Fire').click(
		function (){
			jQuery('#dlgPopup').dialog('option', 'title', this.title);
			jQuery('#dlgPopup').dialog('open');
			return false;
		}
	);
	//jQuery("a", ".demo").click(function() { return false; });

	// Datepicker
	jQuery('#datepicker').datepicker({
		giortes_property : new giortes(),
		inline: true,
		showOtherMonths: false,
		dateFormat: 'dd/mm/yy',
		beforeShowDay: function(aDate) {
			//decides if a date should be made selectable based on whether there is a giorti on that date
			var selectableDate = 0;
			var today = new Date();
			var myGiortes = jQuery('#datepicker').datepicker('option', 'giortes_property');			
			var giortazoun = myGiortes.get_giorti_on(dateToString(aDate, 'dd/mm'));			
			
			if ((giortazoun != undefined) || (aDate.toDateString() == today.toDateString())){ 
				selectableDate = 1;
			}
			
			return [selectableDate, '', giortazoun];
		},
		onSelect: function(dateText, datePickerInstance) {
			var myGiortes = jQuery('#datepicker').datepicker('option', 'giortes_property');
			var fDay = dateText.substr(0, 2);
			if (fDay.substr(1,1) == "/") {
				fDay = "0" + fDay.substr(0, 1);
				fMonth = dateText.substr(2,2);
			}
			else {
				fMonth = dateText.substr(3, 2);
			}
			if (fMonth.substr(1,1) == "/") {
				fMonth = "0" + fMonth.substr(0,1);
			}
			var fGiortazoun = myGiortes.get_giorti_on(fDay + "/" + fMonth);
			//there must be a jQuery method to set the dialog's contents
			var msgDialog = document.getElementById('dialog');
			msgDialog.innerHTML = '<p style="margin-top:10px"><h3 align="center">Στις ' + dateText + ' γιορτάζουν οι</h3></p><p>&nbsp;</p><p><h2 align="center">'+ fGiortazoun + '</h2></p>';
			jQuery('#dialog').dialog('open');
		}
	});
	
	//hover states on the static widgets
	jQuery('#dialog_link, ul#icons li').hover(
		function() { $jQuery(this).addClass('ui-state-hover'); }, 
		function() { $jQuery(this).removeClass('ui-state-hover'); }
	);
	
});

jQuery(document).ready(function(){
//Athos: when jQuery has finished initialising components
//Lets change the accordions header background 
var jAccordion = document.getElementById('accordion');
if (jAccordion == null) return;	//if an accordion element is not found simply exit

//jAccordion.firstElementChild.className = jAccordion.firstElementChild.className + ' ui-accordion-header-bg';
//for browser combatibility we need to loop through child elements and find the first one with nodeTye == 1 (is the header)
for (var nodeNo = 0; nodeNo < jAccordion.childNodes.length; nodeNo++) {
	var curNode = jAccordion.childNodes[nodeNo];
	if (curNode.nodeType == 1) {
		curNode.className = curNode.className + ' ui-accordion-header-bg';
		break;
	}
}
//jAccordion.childNodes[0].className = jAccordion.childNodes[0].className + ' ui-accordion-header-bg';

//now let's fill also the accordion
var jAccordionContents = document.getElementById('acordion_contents');
var curDate = new Date();
var tmpGiortes = jQuery('#datepicker').datepicker('option', 'giortes_property');
var strResult ='<table border="0" style="margin-left:-17px;margin-top:-10px;">';
var tmpGiortazoun = '';
for (var x=0; x < 5; x++){
	tmpGiortazoun = tmpGiortes.get_giorti_on(dateToString(curDate, 'dd/mm'));
	if (tmpGiortazoun != undefined){
		strResult = strResult + '<tr><td style="vertical-align: top">' + dateToString(curDate, 'dd/mm') + '</td><td>' +
					tmpGiortazoun + '</td></tr>';
	}
	curDate.setDate(curDate.getDate() + 1);
}
strResult = strResult + '</table>';
jAccordionContents.innerHTML = strResult;
});
