$(document).ready(function() {
//    cdn = $('#cdn').val();
//    $('#mycarousel').jcarousel({
//        vertical: true,
//        scroll: 1,
//        auto: 25,
//        animation: 3500,
//        start: parseInt($('#jcarouselStart').val()),
//        wrap: 'circular',
//        buttonNextHTML: null,
//        buttonPrevHTML: null,
//        itemFirstInCallback: {
//            //onBeforeAnimation: callback1,
//            onAfterAnimation: mycarousel_itemFirstInCallback
//        },
//        initCallback: mycarousel_initCallback,
//    });
//    
//    $('#mycarousel img').click(function(){
//        changeBGImage(this, 'headerImg', cdn);
//    });
    $('.slideshow').cycle({
        fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    });
    
    var resourceUrl = $('#resourceUrl').val();
    var options = {
            dateFormat: 'dd.mm.yy',
            numberOfMonths: 1,
            minDate: 0,
            maxDate: +331,
            defaultDate: 0,
            firstDay: 1,
            showOtherMonths: false, 
            selectOtherMonths: false,
            showOn: 'both',
            buttonImage: resourceUrl + 'images/calendar.gif',
            buttonImageOnly: false,
            onSelect: function(selectedDate) {
                if ('arrival-date' == this.name) {
                    var fDate = $(this).datepicker('getDate');
                    fDate.setDate(fDate.getDate()+ 1);
                    $("input#departure-date").datepicker('setDate', fDate);
                    $("input#departure-date").datepicker('option', 'minDate', fDate);
                }
            }
        };
        
    $("input#arrival-date").datepicker(options);
    $("input#departure-date").datepicker(options);
    
    
    originalFontSize = parseInt($('body').css('font-size'));
    
    $('.chgSize').click(function(){
        var fontSize = originalFontSize;
        var a = $(this);
        var inc = 0;
        if (a.hasClass('small')) inc = 1.4;
        if (a.hasClass('middle')) inc = 1.6;
        if (a.hasClass('big')) inc = 1.8;
        if (a.hasClass('reset')) {
            $('body').attr('font-size', '0.8em');
        }
        
        if (inc > 0)
            $('body').css('font-size', (originalFontSize * inc));
        //$('body').css('font-family', 'arial');
//        $('img').each(function () {
//            this.width *= 1.5;
//        });

    })
    
    //addLoadEvent(loadGoogleMap);
    
    var interval = null;
	//booking box on hover reveals quick booking form
	$('.bookbox').mouseover(function(){
		if (interval) {
			clearInterval(interval);
		}	
		if ($(".book").css('display') != 'block') {
			$(".price").slideUp(200,function(){
				$(".book").slideDown(200);
});
		}

	
	});
    
  //booking box on mouse out returns to price
	$('.bookbox').mouseleave( function(){
		if (interval) {
			clearInterval(interval);
		}
		interval = setTimeout(function () {
			if ($(".book").css('display') == 'block') {
				$(".book").slideUp(200,function(){
					$(".price").slideDown(200);
				});
			}
		}, 1500);
		
	});
	  
});

/**
* We use the initCallback callback
* to assign functionality to the controls
*/
function mycarousel_initCallback(carousel) 
{
    var prevAnimation = carousel.options.animation;
    
    $('#mycarousel-next').bind('mouseover click', function() {
        carousel.options.animation = 500;
        carousel.next();
        carousel.options.animation = prevAnimation;
        return false;
    });
    
    $('#mycarousel-prev').bind('mouseover click', function() {
        carousel.options.animation = 500;
        carousel.prev();
        carousel.options.animation = prevAnimation;
        return false;
    });
};

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 */
function mycarousel_itemFirstInCallback(carousel, item, idx, state) 
{
    cdn = $('#cdn').val();
    img = $(item).children();
    changeBGImage(img[0], 'headerImg', cdn);
}

/**
 * Changes background image of a div
 *
 */
function changeBGImage(el, div, cdn)
{
    src = el.getAttribute('src');
    srcParts = src.split('/');
    img = srcParts[srcParts.length-1];
    imgTmp = img.substr(6);
    imgNew = 'url(' + cdn + 'uploads/' + imgTmp + ') no-repeat scroll left top transparent';
    
    if (imgTmp) {
        $('#' + div).css('background', imgNew);
    }
}

function addLoadEvent(newFunc)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = newFunc;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            newFunc();
        }
    }
}

function loadGoogleMap() 
{
    var resourceUrl = $('#resourceUrl').val();
    
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("contactmap"));
        map.setCenter(new GLatLng(52.508423,13.327418), 13); 
    }

    // Create our "tiny" marker icon
    var point = new GLatLng(52.508423,13.327418);
    var icon = new GIcon();
    icon.image = resourceUrl + "img/favicon.png";
    icon.shadow = resourceUrl + "img/favicon.png";
    icon.iconSize = new GSize(24, 24);
    icon.shadowSize = new GSize(24, 24);
    icon.iconAnchor = new GPoint(1, 5);
    icon.infoWindowAnchor = new GPoint(1, 1);
    var marker = new GMarker(point,icon);

    map.addOverlay(marker);
}