var date = '';
$(document).ready(function() {

	// set active menu item
	var href = location.href;

	if (href.match(/\/(presentation|action|charte|motdupresident)/)) {
		 $('.menu-main-cbf').addClass('orange-border-selected');
	}
	else if (href.match(/\/(reseau|viedureseau|culture|rejoindre)/)) {
		 $('.menu-main-reseau').addClass('gray-border-selected');
	}
	else if (href.match(/\/(news)/)) {
		 $('.menu-main-actualities').addClass('red-border-selected');
	}
	else if (href.match(/\/(agenda|events\/)/)) {
		 $('.menu-main-agenda').addClass('green-border-selected');
	}
	else if (href.match(/\/(galeries|galleries|videoGallery|photoGallery)/)) {
		 $('.menu-main-galleries').addClass('blue-border-selected');
	}
	else if (href.match(/\/(presse)/)) {
		 $('.menu-main-presse').addClass('purple-border-selected');
	}
	else if (href.match(/\/(contact)/)) {
		 $('.menu-main-contact').addClass('pink-border-selected');
	}


	var response = '';
	//$('.news').crawlLine({crawElement:'.move'});

	if ($('.turn-me-into-datepicker')) {
		$('.turn-me-into-datepicker')
			.datePicker({inline:true, startDate:'2000-01-01', showYearNavigation:false, renderCallback: setEvents})
		/*
			.bind(
				'dateSelected',
				function(e, selectedDate, $td) {
					var d = selectedDate.getDate();
					var m = selectedDate.getMonth()+1;
					var Y = selectedDate.getFullYear();
					date = d+'-'+m+'-'+Y;
					$.get("index.php/articleInDate/"+date,function(data){
						var tmp = data.split(':');
						if(tmp.length>1) {
							$('ul.agenda-content>li.title').html(tmp[0]);
							$('#agenda-date').html(tmp[1]);
							$('#agenda-text').html(tmp[2]+'(...)');
							$('#agenda-link').html('<a href="articles/'+tmp[3]+'">Lire la suite</a>');
						}
						else {
							$('ul.agenda-content>li.title').html('');
							$('#agenda-date').html('');
							$('#agenda-text').html('');
							$('#agenda-link').html('');
						}
					});
				}
			)
			.bind(
				'dpMonthChanged',function(event, displayedMonth, displayedYear) {
					$.get("index.php/articlesInDate/"+(displayedMonth+1)+"-"+displayedYear,function(data){
						var tmp_data = data.split(';');
						//console.log(tmp_data);
						$('#calendar-data-prev').attr("value",tmp_data[0]);
						$('#calendar-data-next').attr("value",tmp_data[1]);
					});
				}
			)
			.bind(
				'dpDisplayed',function(event, $datePickerDiv) {

				}
			);

			*/

	}

	function setEvents($td, thisDate, month, year) {
		var d = thisDate.getDate(),
			m = thisDate.getMonth() + 1,
			y = thisDate.getFullYear();

		var s = y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d),
			ss =  (d < 10 ? '0' + d : d) + '-' + (m < 10 ? '0' + m : m)  + '-' + y;

		if ($.inArray(s, eventDates) > -1) {
			$td.css({
				background: 'orange'
			});

			$td.click(function() {
				/* new $.ajax({
					'url': '/agenda/date/' + s,
					'success': function (r) {
						$('#calendarEvents').html(r);
					}
				}); */
				location.href = '/agenda/date/' + s;
				return false;
			});
		}

		//response = jQuery('#calendar-data-prev').attr("value").split(':');
		//date = Y+'-'+m+'-'+d;

		//if(date==response[0]) $td.addClass('event');
	}

	//presentation
	/*
	jQuery('#myslidemenu a').each(function() {
		if (jQuery(this).attr('href').indexOf(matched_location[1]) != -1) {
			if(jQuery(this).attr('className')=='') {
				var ul = jQuery(this).parent();
				while(ul.attr('tagName')!="UL") {
					ul = ul.parent();
				}
				var anchr = ul.prev();
				var className = anchr.attr('className')+'-selected';
				anchr.attr('className',className);
			}
			else {
				var className = jQuery(this).attr('className')+'-selected';
				jQuery(this).attr('className',className);
			}
		}
	});
	*/
		
});



function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}
function closeLightBox() { 
    document.getElementById('LightBoxWindow').style.display = "none";
	document.getElementById('OpacityLayer').style.display = "none";
	document.body.scroll = "";
    document.body.style.overflow = "";
}
function showDiv(divId)
    {
        var vShowDiv = document.getElementById(divId);
        if(vShowDiv)
        {
            vShowDiv.style.display = "block";
            vShowDiv.style.left = ((window.screen.width-vShowDiv.offsetWidth)/2.1)+"px";
            vShowDiv.style.top = ((window.screen.height-vShowDiv.offsetHeight)/3.7)+"px";
        }
		 document.getElementById('OpacityLayer').style.display = "block";
		 if (document.all)
            {               
                document.body.scroll = "no";
            }
            else
            {
                var oTop = document.body.scrollTop;
                document.body.style.overflow = "hidden";
                document.body.scrollTop = oTop;
            }
}


function showNewsLetterWindow() {

    // open a welcome message as soon as the window loads
    Shadowbox.open({
        content:    jQuery('#SubscribeWindow').html(),
        player:     "html",
        //title:      "Inscription Newsletter",
        height:     172,
        width:      464
    });
	
}

function sendSubscription() {
	var name = jQuery('#sb-content [name=name]').val(),
		email = jQuery('#sb-content [name=email]').val();

	jQuery.ajax({
		url: '/site/saveSubscription/name/' + encodeURIComponent(name) + '/email/' + encodeURIComponent(email),
		success: function(r) {
			eval(r);
		}
	});
}

