// JavaScript Document
var pageWidth, pageHeight;
/*  ListArticles  */
        
$(document).ready( function() {
    pageWidth = $(window).width();
    pageWidth = pageWidth / 2;
    pageHeight = $('html').height();
    pageHeight = pageHeight / 2;
    $("#print_button").click(function(){
        $("div#print_Area").printArea();
    });
    popupGMap();
    
    $('#book').click(function(){
        
        var form = $('#parken_wrap form');
        form.submit();
        $(this).hide();
        
        return false;
    });
	if($('.translateable .languageswitch').length > 0)
	{
		var link = $('.languageswitch a');
		var url = document.location.href;
		if(url.search(/\/en\//) != -1)
		{
			url = url.replace(/\/en\//, '/de/', url);
		}
		else if(url.search(/\/en$/) != -1)
		{
			url = url.replace(/\/en$/, '/de/', url);
		} 
		else if(url.search(/\/de\//) != -1)
		{
			url = url.replace(/\/de\//, '/en/', url);
		}
		else if(url.search(/\/de$/) != -1)
		{
			url = url.replace(/\/de$/, '/en/', url);
		}
		else if(url.search(/\/$/) == -1)
		{
			url = url + '/en';
		}
		else if(url.search(/\/$/) != -1)
		{
			url = url + 'en';
		}
		link.attr('href', url);
	}
	/* close function for search box */
	$('.searchBoxErrorClose').click(function() {
		$(".searchBoxError").hide();
	});
    
    /* centerize object */
	jQuery.fn.center = function () {
		this.css("position","absolute");
		this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
		this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
		return this;
	}

    /* postitioni of mouse */
    $().mousemove(function(e){
        mX = e.pageX;
        mY = e.pageY;
    });
    
    /* infoLayer ausm Fenster */
    
    /* infoLayer für Parkplätze */
    $('.infoLink').hover(
        function() {$(this).parent('div').find('.infoLayer').show();

            $(this).mousemove(function(){
                $(this).parent('div').find('.infoLayer').center();
                /*$(this).parent('div').find('.infoLayer').css('top',mY-190+'px');*/
                $(this).parent('div').find('.infoLayer').css('left',mX+40+'px');
            });
        },
    function() {
        $(this).parent('div').find('.infoLayer').hide();
        });
    /* */
    
    /* infoLayer für i-buttons */
    $('.line .i-btnblue').hover(
        function() {$(this).find('.i-con').css('visibility','visible');
            $(this).mousemove(function(){
                $(this).find('.i-con').css('top',0+'px');
                $(this).find('.i-con').css('right',-150+'px');
            });
        },
    function() {
        $(this).find('.i-con').css('visibility','hidden');
        });
    $('.ecinfo .i-btnblue').hover(
        function() {$(this).find('.i-con').css('visibility','visible');
            $(this).mousemove(function(){
                $(this).find('.i-con').css('top',0+'px');
                $(this).find('.i-con').css('right',-220+'px');
            });
        },
    function() {
        $(this).find('.i-con').css('visibility','hidden');
        });
    $('#check .i-btnblue').hover(
        function() {$(this).find('.i-con').css('visibility','visible');
            $(this).mousemove(function(){
                $(this).find('.i-con').css('top',-200+'px');
                $(this).find('.i-con').css('right',-150+'px');
            });
        },
    function() {
        $(this).find('.i-con').css('visibility','hidden');
        });
    /* */

    $('.listarticle').each( function() {
        var listNum = 6;
        var pageNum = 0;
        $(this).find('li').mouseover( function() {
            $(this).addClass('listOver');                     
        });
        $(this).find('li').mouseout( function() {
            $(this).removeClass('listOver');            
        });
        
        var listCant = $(this).find('li').length;
        $(this).find('span span.nrArt').html(listCant);
        if (listCant%listNum == 0) {
            pageCant = parseInt(listCant/listNum);
        }
        else {
            pageCant = parseInt(listCant/listNum)+1;
        }
        $(this).find('li').each( function(i) {
            $(this).addClass('list'+(parseInt(i)+1));
        });
        for ( var k=0; k<=pageCant; k++) {
            for ( var j=(k*listNum)+1; j<=(k*listNum)+listNum; j++) {
                $(this).find('.list'+j).addClass('page'+(parseInt(k)+1));
            }
            if (k>=2) {
                $(this).find('.pager').append('<span class="divisor">|</span><a id="pagNr'+k+'" class="pag">&nbsp;'+k+'&nbsp;</a>');
            }
        }
        
        $(this).find('.pager a').click( function() {
            var clickedPag = $(this).attr('id').substr(5,10);
            if ($(this).hasClass('pagAct')) {
            }    
            else {
                $(this).parent().find('a.pagAct').removeClass('pagAct');
                $(this).addClass('pagAct');
                $(this).parents('.listarticle').find('li').css({
                    display: 'none'
                });
                $(this).parents('.listarticle').find('li.page'+clickedPag).css({
                    display: 'block'
                });
                
            }
        });
    
    });
    /*  Footer Bottom  */ /*Leave me at the end*/
    footerbottom();
    $(window).resize( function() {
        footerbottom();                
    });
    
    $('#footer .footercontent .partner').jCarouselLite({
        auto: 500,
        speed: 2000,
        visible: 6
    });
    
    /*butSend*/

    if($('#skybooking').length > 0)
    {
        $('.periodSelectorButtonIFrame, .auswahl-aendern-btn').click(function(){
            if($('select#layoutTheme_input').val() == 0)
            {
                alert('Bitte wählen Sie einen Airport');
            }
            else
            {
                booking();
            }
        });
    }
    function parseUILanguage() {                                 
        var languageTag = document.getElementsByTagName('html')[0].getAttribute('lang') || document.getElementsByTagName('html')[0].getAttribute('xml:lang');
        
        if(!languageTag) {
                languageTag = "en";
        } else {
                languageTag = languageTag.toLowerCase();
        };
                                                    
        return languageTag.search(/^([a-z]{2,3})-([a-z]{2})$/) != -1 ? [languageTag.match(/^([a-z]{2,3})-([a-z]{2})$/)[1], languageTag] : [languageTag];                       
    };
    
    function paymentType()
    {
        $('#payment input[type=radio]').click(function(){
            var url = '/frontend_dev.php/' + parseUILanguage() + '/ajax/payment/?payment[payment_type]=' + $(this).val();
            $('#payment').load(url, function(){
                paymentType();
                entrylikePayment();
                /* infoLayer für i-buttons */
                $('#payment .i-btnblue').hover(
                    function() {$(this).find('.i-con').css('visibility','visible');
                        $(this).mousemove(function(){
                            $(this).find('.i-con').css('top',0+'px');
                            $(this).find('.i-con').css('right',-150+'px');
                        });
                    },
                function() {
                    $(this).find('.i-con').css('visibility','hidden');
                    });
                /* */
            });
        });
    }
    paymentType();
    
    function entryType()
    {
        $('#entry input[type=radio]').click(function(){
            var url = '/frontend_dev.php/de/ajax/entry/?entry[entry_type]=' + $(this).val();
            $('#entry').load(url, function(){
                entryType();
                /* infoLayer für i-buttons */
                $('#entry .i-btnblue').hover(
                    function() {$(this).find('.i-con').css('visibility','visible');
                        $(this).mousemove(function(){
                            $(this).find('.i-con').css('top',0+'px');
                            $(this).find('.i-con').css('right',-150+'px');
                        });
                    },
                function() {
                    $(this).find('.i-con').css('visibility','hidden');
                    });
                /* */
            });
        });
    }
    entryType();
    
    function entrylikePayment()
    {
        $('#entry_entry_like_payment').click(function(){
            if($(this).attr('checked'))
            {
                $('#entry span.entry').hide();
            }
            else
            {
                $('#entry span.entry').css('display', 'block');
            }
            
        });
    }
    entrylikePayment();
});

//Alert-Style
jQuery.altAlert = function (options)
{
    var defaults = { };
 
    jQuery.extend(defaults, options);
 
    delete defaults.autoOpen;
 
    window.alert = function ()
    {
        jQuery("<div />").html(arguments[0].replace(/\n/, "<br />")).dialog(defaults);
    };
};
 
$(function ()
{
    $.altAlert();
});

/*Navigation
$(function(){
        $('.naviHauptnavigation').superfish({
                delay: 500,
                autoArrows: false,
                dropShadows: false
        });
});*/

function footerbottom() {
    if($('#cmsEditor').length == 0)
    {
        var heightPage = $('#page').attr('clientHeight');
        var heightContent = $('#content').attr('clientHeight');
        var heightHtml = $('html').attr('clientHeight');
        if ( heightPage < heightHtml) {
            $('#content').attr('style','min-height:'+(heightHtml-(heightPage-heightContent+1))+'px');
        }
        else {
            $('#content').removeAttr('style');
            var heightPage = $('#page').attr('clientHeight');
            var heightContent = $('#content').attr('clientHeight');
            var heightHtml = $('html').attr('clientHeight');
            if ( heightPage < heightHtml) {
                $('#content').attr('style','min-height:'+(heightHtml-(heightPage-heightContent+1))+'px');
            }
        }
    }
    else
    {
        var heightPage = $('#page').attr('clientHeight');
        var heightContent = $('#content').attr('clientHeight');
        var heightHtml = ($('html').attr('clientHeight')-70);
        if ( heightPage < heightHtml) {
            $('#content').attr('style','min-height:'+(heightHtml-(heightPage-heightContent+1))+'px');
        }
        else {
            $('#content').removeAttr('style');
            var heightPage = $('#page').attr('clientHeight');
            var heightContent = $('#content').attr('clientHeight');
            var heightHtml = ($('html').attr('clientHeight')-70);
            if ( heightPage < heightHtml) {
                $('#content').attr('style','min-height:'+(heightHtml-(heightPage-heightContent+1))+'px');
            }
        }
    }
}
/****browse button style****/
(function($) {
    
    $.fn.filestyle = function(options) {
                
        /* TODO: This should not override CSS. */
        var settings = {
            width : 250
        };
                
        if(options) {
            $.extend(settings, options);
        };
                        
        return this.each(function() {
            
            var self = this;
            var wrapper = $("<div>")
                            .css({
                                "width": settings.imagewidth + "px",
                                "height": settings.imageheight + "px",
                                "background": "url(" + settings.image + ") 0 0 no-repeat",
                                "background-position": "right",
                                "display": "inline",
                                "position": "absolute",
                                "overflow": "hidden"
                            });
                            
            var filename = $('<input class="file">')
                             .addClass($(self).attr("class"))
                             .css({
                                 "display": "inline",
                                 "width": settings.width + "px"
                             });

            $(self).before(filename);
            $(self).wrap(wrapper);

            $(self).css({
                        "position": "relative",
                        "height": settings.imageheight + "px",
                        "width": settings.width + "px",
                        "display": "inline",
                        "cursor": "pointer",
                        "opacity": "0.0"
                    });

            if ($.browser.mozilla) {
                if (/Win/.test(navigator.platform)) {
                    $(self).css("margin-left", "-142px");                    
                } else {
                    $(self).css("margin-left", "-168px");                    
                };
            } else {
                $(self).css("margin-left", settings.imagewidth - settings.width + "px");                
            };

            $(self).bind("change", function() {
                filename.val($(self).val());
            });
      
        });
        

    };
    
    
})(jQuery);

var typeface=0;

function popupGMap() {
    $('#map').mousemove(function(e){
        if (e.clientX<=775) {
            var left = e.clientX + 50;
        } else {
            var left = e.clientX - 350;
        }
        var top = e.pageY - 20 - $('#markerInfo').width() / 2;
        if(typeface==0){
            $('#map_wrap .typeface-js').css('visibility','hidden');
            $('.gMapComponent #markerInfo .typeface-js').css('visibility','hidden');
            typeface=1;
        }
        $('#markerInfo').css('top',top);
        $('#markerInfo').css('left',left);
    })
}


function initializeMain() {
    if (GBrowserIsCompatible()) {
    
        var map = new GMap2(document.getElementById("googleMapsAPI"));
        map.setCenter(new GLatLng(50.545552, 9.664306), 5);
        
        map.addControl(new GSmallMapControl());
        
        // Icons
        var stdIcon = new GIcon(G_DEFAULT_ICON);
        stdIcon.shadow = "/images/project/mapsIcon/shadowIcon.png";
        stdIcon.iconSize = new GSize(30, 22);
        stdIcon.shadowSize = new GSize(30, 25);
        stdIcon.iconAnchor = new GPoint(12, 11);
        stdIcon.infoWindowAnchor = new GPoint(22, 2);
        stdIcon.imageMap = [0, 0, 30, 0, 30, 22, 0, 22];
        
        var strIcon = new GIcon(stdIcon);
        strIcon.image = "/images/project/mapsIcon/str.png";
        var sxfIcon = new GIcon(stdIcon);
        sxfIcon.image = "/images/project/mapsIcon/sxf.png";
        var mucIcon = new GIcon(stdIcon);
        mucIcon.image = "/images/project/mapsIcon/muc.png";
        var fkbIcon = new GIcon(stdIcon);
        fkbIcon.image = "/images/project/mapsIcon/fkb.png";
        var markerSTR = new GMarker(new GLatLng(48.7666667, 9.1833333), {
            icon: strIcon
        });
        var language = '', url = '' + window.location;
        url = url.split('/');
        if(url[3] in {de : true, en : true, pl : true}){
            language = '/' + url[3];
        }
        GEvent.addListener(markerSTR, "mouseover", function(){
            $("#googleMapsInfo").css('background', 'transparent url(/images/project/maps/str.jpg) center no-repeat');
            $('#googleMapsInfo').show();
        });
        GEvent.addListener(markerSTR, "mouseout", function(){
            $("#googleMapsInfo").hide();
        });
        GEvent.addListener(markerSTR, "click", function(){
            var urlSTR = '' + window.location;
            urlSTR = urlSTR.split('/');
            window.location = 'http://' + urlSTR[2] + language + '/flughafen-stuttgart-parken/175';
        });
        var markerSXF = new GMarker(new GLatLng(52.5, 13.50833333), {
            icon: sxfIcon
        });
        GEvent.addListener(markerSXF, "mouseover", function(){
            $("#googleMapsInfo").css('background', 'transparent url(/images/project/maps/sxf.jpg) center no-repeat');
            $('#googleMapsInfo').show();
        });
        GEvent.addListener(markerSXF, "mouseout", function(){
            $("#googleMapsInfo").hide();
        });
        GEvent.addListener(markerSXF, "click", function(){
            var urlSXF = '' + window.location;
            urlSXF = urlSXF.split('/');
            window.location = 'http://' + urlSXF[2] + language + '/flughafen-berlin-schoenefeld-parken/177';
        });
        var markerMUC = new GMarker(new GLatLng(48.354128, 11.782876), {
            icon: mucIcon
        });
        GEvent.addListener(markerMUC, "mouseover", function(){
            $("#googleMapsInfo").css('background', 'transparent url(/images/project/maps/muc.jpg) center no-repeat');
            $('#googleMapsInfo').show();
        });
        GEvent.addListener(markerMUC, "mouseout", function(){
            $("#googleMapsInfo").hide();
        });
        GEvent.addListener(markerMUC, "click", function(){
            var urlMUC = '' + window.location;
            urlMUC = urlMUC.split('/');
            window.location = 'http://' + urlMUC[2] + language + '/flughafen-muenchen-parken/218';
        });
        var markerFKB = new GMarker(new GLatLng(48.7807998657, 8.09045982361), {
            icon: fkbIcon
        });
        GEvent.addListener(markerFKB, "mouseover", function(){
            $("#googleMapsInfo").css('background', 'transparent url(/images/project/maps/fkb.jpg) center no-repeat');
            $('#googleMapsInfo').show();
        });
        GEvent.addListener(markerFKB, "mouseout", function(){
            $("#googleMapsInfo").hide();
        });
        GEvent.addListener(markerFKB, "click", function(){
            var urlFKB = '' + window.location;
            urlFKB = urlFKB.split('/');
            window.location = 'http://' + urlFKB[2] + language + '/flughafen-karlsruhe-baden-parken/210';
        });
        
        //map.addOverlay(markerDUS);
        map.addOverlay(markerSTR);
        map.addOverlay(markerSXF);
        map.addOverlay(markerMUC);
        map.addOverlay(markerFKB);
    }
}
function booking() {
        var airport = $('#layoutTheme_input').val();
        airport = airport.substr(airport.length - 3);
        
        var fromDay = $('input#fromDate_day').val();
        var fromMonth = $('input#fromDate_month').val();
        var fromYear = $('input#fromDate_year').val();
        var fromHour = $('select[name="fromDate_hour"]').val();
        var fromMin = $('select[name="fromDate_minute"]').val();
        
        var fromMidnight = new Date(fromYear, fromMonth, fromDay, 0, 0, 0);
        var fromAll = new Date(fromYear, fromMonth, fromDay, fromHour, fromMin, 0);
        var ariInSec = (fromMidnight.getTime());
        var ariExSec = (fromAll.getTime());
        
        var toDay = $('input#toDate_day').val();
        var toMonth = $('input#toDate_month').val();
        var toYear = $('input#toDate_year').val();
        var toHour = $('select[name="toDate_hour"]').val();
        var toMin = $('select[name="toDate_minute"]').val();
        
        var toAll = new Date(toYear, toMonth, toDay, toHour, toMin, 0);
        var depExSec = (toAll.getTime());
        
        var parkSec = (depExSec - ariExSec) / 1000;
        
        if(fromHour.length==1){fromHour='0'+fromHour}
        if(fromMin.length==1){fromMin='0'+fromMin}
        if(toHour.length==1){toHour='0'+toHour}
        if(toMin.length==1){toMin='0'+toMin}
        
        var parkDay = 0;
        var parkHour = 0;
        
        parkSec = ''+parkSec;
        
        parkSec = parkSec * 1;
        
        parkHour = parkSec / 3600;
        
        parkDay = parseInt(parkHour / 24);
        parkHour = parkHour - (parkDay * 24);
        
        if(fromDay.substr(0, 1)==0) {fromDay = fromDay.substr(1)}
        if(fromMonth.substr(0, 1)==0) {fromMonth = fromMonth.substr(1)}
        if(toDay.substr(0, 1)==0) {toDay = toDay.substr(1)}
        if(toMonth.substr(0, 1)==0) {toMonth = toMonth.substr(1)}
        
        //alert(airport);
        if (airport=='MUC' || airport=='apcoade_MUC') {
            window.open('http://www.munich-airport.de/de/consumer/anab/auto/parken/online_rab/index.jsp?submit=WEITER&status=makeParkList&depTime='+fromHour+''+fromMin+'&depDay='+fromDay+'&depMonth='+fromMonth+'&depYear='+fromYear+'&ariTime='+toHour+''+toMin+'&ariDay='+toDay+'&ariMonth='+toMonth+'&ariYear='+toYear+'&parkSec='+parkSec+'&ariInSec='+ariInSec+'&ariExSec='+ariExSec+'&depExSec='+depExSec+'&dayTxt='+parkDay+'&hourTxt='+parkHour+'');
        }
        else if (airport=='FKB' || airport=='apcoade_FKB') {
            if($('.iframeBooking').length > 0) {
                var target = $('#periodSelectorFormIFrame').attr('target', '_blank');
            }
            else
            {
                var target = $('#periodSelectorFormIFrame').attr('target', '_self');
            }
            var action = $('#periodSelectorFormIFrame').attr('action');
            $('#periodSelectorFormIFrame #iataCode').val(airport);
            $('#periodSelectorFormIFrame').attr('action', action);
            $('#periodSelectorButtonIFrame').trigger('click');
        }
        else if (airport=='MU2' || airport=='apcoade_MU2') {
            var target = $('#periodSelectorFormIFrame').attr('target', '_self');
            var action = $('#periodSelectorFormIFrame').attr('action');
            $('#periodSelectorFormIFrame #iataCode').val(airport);
            $('#periodSelectorFormIFrame').attr('action', action);
            $('#periodSelectorButtonIFrame').trigger('click');
        }
        else {
            var actionOld = $('#periodSelectorFormIFrame').attr('action');
            $('#periodSelectorFormIFrame').attr('action', 'http://booking.skyparking.com/carPark/list');
            $('#periodSelectorButtonIFrame').trigger('click');
            $('#periodSelectorFormIFrame').attr('action', actionOld);
        }
        
}

function footerTarget() {
    $('#naviFooter_Navigation .node3 li').each(function() {
        text = $(this).find('a').text();
        if(text.indexOf(' ')>-1) {
            text = text.replace(' ', '_');
        }
        href = $(this).find('a').attr('href');
        href = href + '#' + text;
        
        $(this).find('a').attr('href', href);
    })
}

