var calendarDates;

var rightCalendar;

var submitInProgress = false;

jQuery(document).ready(function()
{
	jQuery("a[rel='slideshow']").colorbox({previous: "előző", next: "következő", close: "bezár", current:"{current}/{total}"});
    initPlaceholders();
    if (printMode) {
        getPrintContents();
        setTimeout(function() {window.print()}, 300);
    } else {
        initPrint();
    }
    initCalendar();
});

function openTanfolyam(title)
{
	var div = title.parentNode;
	
	var details_btn = div.getElementsByTagName("div")[3];
	var close = div.getElementsByTagName("div")[4];
	var content = div.getElementsByTagName("div")[6];
	
	title.className = "indulo_tanfolyamok_list_item_row_opened";
	title.setAttribute("onclick", "closeTanfolyam(this);");
	details_btn.style.cssText = "display:none";
	close.style.cssText = "display:block";
	content.style.cssText = "display:block";
}

function closeTanfolyam(title)
{
	var div = title.parentNode;
	
	var details_btn = div.getElementsByTagName("div")[3];
	var close = div.getElementsByTagName("div")[4];
	var content = div.getElementsByTagName("div")[6];
	
	title.className = "indulo_tanfolyamok_list_item_row";
	title.setAttribute("onclick", "openTanfolyam(this);");
	details_btn.style.cssText = "display:block";
	close.style.cssText = "display:none";
	content.style.cssText = "display:none";
}

function startMap()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(jQuery('#companyMap')[0]);
		map.enableScrollWheelZoom();
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
		
		if (null == mapLong || null == mapLat)
		{
			geocoder = new GClientGeocoder();
			geocoder.getLatLng(mapAddr + ", " + mapCity, addPin);
			
		}
		else
		{
			addPin(new GLatLng(mapLat,mapLong));
		}
	}
}

function addPin(latLong)
{
	if (null == latLong)
	{
		geocoder = new GClientGeocoder();
		geocoder.getLatLng('Budapest', addPin);
	}
	
	lastLatLong = latLong;
	map.setCenter(latLong, 13);
	map.addOverlay(new GMarker(latLong));
	var divElement = document.createElement('div');
	divElement.className = 'maps_company_info';
	divElement.innerHTML = '<strong>'+companyName+'</strong><br />'+mapAddr+"<br />"+mapCity;
	map.openInfoWindow(latLong, divElement);
}

function openVideo(id)
{
	var youtubePlayer = document.getElementById("youtube_player");
	youtubePlayer.innerHTML = "<object width=\"665\" height=\"395\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + id + "&hl=hu_HU&fs=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/" + id + "&hl=hu_HU&fs=1\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"665\" height=\"395\"></embed></object>";
	scroll(0, 220);
}

function removePlaceholder(element) {
    if(jQuery(element).val() == jQuery(element).attr('placeholder')) {
        jQuery(element).val('');
    }
}

function replacePlaceholder(element) {
    if (jQuery(element).val() == '') {
        jQuery(element).val(jQuery(element).attr('placeholder'));
    }
}

function initPlaceholders() {
    var i = document.createElement('input');
    if('placeholder' in i) {
        return
    }
    jQuery('input[placeholder]').add('textarea[placeholder]').each(function() {
        jQuery(this).focus(function() {removePlaceholder(this)}).blur(function() {replacePlaceholder(this)});
        replacePlaceholder(this);
    });
    jQuery('form').bind('submit', clearPlaceholdersOnSubmit);
}

function clearPlaceholdersOnSubmit() {
    jQuery('input[placeholder]', this).add('textarea[placeholder]', this).each(function() {
        removePlaceholder(this);
    });
}

function initPrint() {
    jQuery('.box_content_functions a:first').click(function() {
        window.open('/print.html', 'printWin', 'width=800,height=600,scrollbars=yes');
    })
}

function getPrintContents() {
    jQuery('.content').html(jQuery('.content_container', window.opener.document).html());
}

function initCalendar() {
    rightCalendar = new CalendarEightysix('calendarEngine', {
        'injectInsideTarget': true,
        'alwaysShow': true,
        'pickable': false,
        'startMonday': true
    });
    rightCalendar.addEvent('rendermonth', function(e) {
        e.elements.each(calendarRenderMonth);
    })
    
    MooTools.lang.set('hu-HU', 'Date', {
        months: ['Január', 'Február', 'Március', 'Április', 'Május', 'Június', 'Július', 'Augusztus', 'Szeptember', 'Október', 'November', 'December'],
        days: ['Vasárnap', 'Hétfő', 'Kedd', 'Szerda', 'Csütörtök', 'Péntek', 'Szombat'],
        dateOrder: ['year', 'month', 'date', '/']
    });

    MooTools.lang.setLanguage('hu-HU');

    rightCalendar.render();
}

function calendarRenderMonth(day) {
    hideCalendarDetail();
    day.set('title', day.retrieve('date').format('%A %d %B'));
    var dayDate = day.retrieve('date');
    var year = dayDate.get('year');
    if (year in calendarDates) {
        var month = dayDate.get('month');
        if (month in calendarDates[year]) {
            var dateNum = dayDate.get('date');
            if (dateNum in calendarDates[year][month] && calendarDates[year][month][dateNum].length > 0) {
                var dateData = calendarDates[year][month][dateNum];
                 day.setStyles({'color': 'firebrick', 'font-weight': 'bold', 'cursor': 'pointer'}).addEvent('click', function() {
                     showCalendarDetail(dayDate, dateData);
                 });
            }
        }
    }
}

function showCalendarDetail(dateObj, data) {
    var dataStr = '';
    for(var i=0; i<data.length; i++) {
        dataStr += '<div class="calendar_list_item"><a href="'+data[i].url+'">'+data[i].name+'</a></div>';
    }
    jQuery('#calendar_list_item_container').html(dataStr);
    jQuery('#calendar_list_date').html(dateObj.format('%Y %B %d.'));
    jQuery('#calendar_list').show('slow');
}

function hideCalendarDetail() {
    jQuery('#calendar_list').hide('slow');
}

function initCourseSearch() {
    jQuery('.indulo_tanfolyamok_step_box select').add('.indulo_tanfolyamok_step_box input').change(function() {
        this.form.submit();
    });
    jQuery('.indulo_tanfolyamok_step_box input').click(function() {
        this.form.submit();
    });
}

function initCocktailSend() {
    jQuery('#cocktailsend_cocktail').change(cocktailChanged);
}

function cocktailChanged() {
    var cocktailId = jQuery('#cocktailsend_cocktail').val();
    var cocktail = cocktailData[cocktailId];
    if (null == cocktail) {
        jQuery('#cocktailsend_image').hide();
        return;
    }
    jQuery('#cocktailsend_image').attr('href', cocktail.pic).show();
    jQuery('#cocktailsend_image img').attr('src', cocktail.thumb);
}

function questionListPublishChanged(id, element) {
    if (submitInProgress) {
        return;
    }
    submitInProgress = true;
    if(element.checked) {
        jQuery('#questionListPublishStatus').val('1');
    } else {
        jQuery('#questionListPublishStatus').val('0');
    }
    jQuery('#questionListPublishId').val(id);
    jQuery('#questionListForm').submit();
}
