﻿var activeElement = null;
var myTimer = null;
var rotationTimeOut = null;
var splashScreenId = null;
var imgPath = null;
var kraftwerkInfo = null;
var longTextDiv = null;
var longTextLink = null;
var selectorImg = null;
var selectorObject = null;
var hasOffset = true;

/**
slider startpage
**/
function startPageInit(selector, setImageClick) {
    $(document).ready(function () {
        myTimer = window.setTimeout("startFadeOut()", 3000);
        resizeContainer($(selector).get(0).getElementsByTagName('div')[0]);
        $(selector).splitUp(1, '<div />').cycle({
            fx: 'scrollLeft',
            timeout: rotationTimeOut,
            pager: '#nav',
            speed: 1500,
            cleartype: true,  // true if clearType corrections should be applied (for IE) 
            cleartypeNoBg: true,


            pagerAnchorBuilder: function (idx, slide) {
                return '<li><a href="#"><img src="' + imgPath + 'Transparent.gif" width="13" height="20" /></a></li>';
            },
            onPagerEvent: function (zeroBasedSlideIndex, slideElement) {
                $(selector).css("overflow", "hidden");
                resizeContainer(slideElement, selector)
                nextPrevSlideEvent(slideElement);
            },
            onPrevNextEvent: function (isNext, zeroBasedSlideIndex, slideElement) {
                $(selector).css("overflow", "hidden");
                resizeContainer(slideElement, selector)
                nextPrevSlideEvent(slideElement);
            },
            after: function (currSlideElement, nextSlideElement, options, forwardFlag) {
                $(selector).css("overflow", "hidden");
                resizeContainer(nextSlideElement, selector)
                nextPrevSlideEvent(nextSlideElement);
            }
        });

        if (setImageClick) {
            $("#image").click(function (e) {
                x = e.pageX - $(this).offset().left

                if (x >= 350)
                    $('#image').cycle('next');
                else
                    $('#image').cycle('prev');
            });
        }


        //show hidden
        displayAll()
    });
}

/**
slider works
**/
function pageInit(visibleItems, loadGoogleMaps) {
    $(document).ready(function () {
        if (loadGoogleMaps) {
            mapPrepareLoad();
            mapLoad(0);
            mapLoad(1);
            tilesloadedListener = GEvent.addListener(maps[1], "tilesloaded", function () {
                $('#itemsContainer').css("visibility", "visible");
                pageInitStep2(visibleItems, loadGoogleMaps)
            });

        }
        else {
            pageInitStep2(visibleItems, loadGoogleMaps)
        }
    });
}

function pageInitStep2(visibleItems, loadGoogleMaps) {
        //remove gmap listener 
        if(tilesloadedListener)
            GEvent.removeListener(tilesloadedListener)
    
        $('#itemsContainer').splitUp(visibleItems, '<div />').cycle({
            fx: 'fade',
            timeout: rotationTimeOut,
            speed: 300,
            pager: '#nav',

            pagerAnchorBuilder: function (idx, slide) {
                return '<li><a href="#"><img src="' + imgPath + 'Transparent.gif" width="13" height="20" /></a></li>';
            },
            onPagerEvent: function (zeroBasedSlideIndex, slideElement) {
                resizeDivContainer(slideElement)
            },
            onPrevNextEvent: function (isNext, zeroBasedSlideIndex, slideElement) {
                resizeDivContainer(slideElement)
            },
            after: function (currSlideElement, nextSlideElement, options, forwardFlag) {
                resizeDivContainer(nextSlideElement)
            }

        });

        //show hidden
        if (!loadGoogleMaps) {
            displayAllDiv()
        }
}

/**
pager spliter
**/
$.fn.splitUp = function (splitBy, wrapper) {
    $all = $(this).find('>*');
    var fragment = Math.ceil($all.length / splitBy);
    for (i = 0; i < fragment; i++)
        $all.slice(splitBy * i, splitBy * (i + 1)).wrapAll(wrapper);
    return $(this);
} 


/**
splash fade out
**/
function startFadeOut() {
    window.clearTimeout(myTimer);
    $(splashScreenId).fadeOut(1000)
}

/**
this items are hidden at the start, so we have to show them
**/
function displayAll() {
    $(selectorImg).css("display", "block");
    $(selectorObject).css("display", "block");
}
/**
this items are hidden at the start, so we have to show them
**/
function displayAllDiv() {
    $("#itemsContainer div").removeClass("noDisplay");
}

/**
just resize item container, you get footer animation
**/
function resizeDivContainer(nextSlideElement) {
if (nextSlideElement) {
    $(nextSlideElement).css("overflow", "hidden");
    $("#itemsContainer").animate({ height: $(nextSlideElement).height() }, 'slow');
    }
}

/**
resize container on startpage
**/
function resizeContainer(nextSlideElement, selector) {
    if (nextSlideElement) {
        //image
        obj = nextSlideElement.getElementsByTagName("img")[0];
        addHeight = 0
        if (!obj) {
            //video
            obj = nextSlideElement.getElementsByTagName("object")[0];
            if(hasOffset)
                addHeight = 100;
        }

        if (obj) {
            newHeight = $(obj).height() + addHeight
            $(selector).animate({ height: newHeight }, 'slow');
        }
    }
}

/**
call back for start page slider changed
**/
function nextPrevSlideEvent(slideElement) {
    //stop/start video
    stopPreviousVideo(activeElement)
    startVideo(slideElement)
    activeElement = slideElement;
}

/**
auto start for movie
**/
function startVideo(elem) {
    myPlayer = elem.getElementsByTagName("object")[0];
    if (myPlayer) {

        if (myPlayer != null && myPlayer.id.indexOf("_true") > -1) {
            jwplayer(myPlayer.id).play(true);
        }
    }
}

/**
if you slide elements than you have to stop previous video
**/
function stopPreviousVideo(elem) {
    if (elem != null) {
        myPlayer = elem.getElementsByTagName("object")[0];
        if (myPlayer != null) {
            jwplayer(myPlayer.id).stop();
        }
    }
}

/**
default slider show more
**/
function showMore() {
    document.getElementById(longTextDiv).className = "show";
    document.getElementById(longTextLink).style.display = "none";
}

/**
default slider show less
**/
function showLess() {
    document.getElementById(longTextDiv).className = "hide";
    document.getElementById(longTextLink).style.display = "inline";
}

/**
icon switcher for social
**/
function switchSocialImage(el, isActive) {
    el.src = (isActive)
            ? imgPath+ "Social/icon-" + el.id + "-on.png"
            : imgPath+ "Social/icon-" + el.id + ".png";
}



/**
GOOGLE Maps
**/
var maps = new Array(null, null);
var markers = new Array(null, null);
var myPoint;
var tilesloadedListener;
var address;


function mapPrepareLoad() {
    if (GBrowserIsCompatible()) {
        myPoint = new GLatLng(48.20823, 16.355427);
        maps[0] = new GMap2(document.getElementById("map0"));
        maps[1] = new GMap2(document.getElementById("map1"));
        
        
        ico = new GIcon();
        ico.image = '/Images/Navigation/markers/image.png';
        ico.shadow = '/Images/Navigation/markers/shadow.png';
        ico.iconSize = new GSize(68, 95);
        ico.shadowSize = new GSize(116, 95);
        ico.iconAnchor = new GPoint(34, 95);
        ico.infoWindowAnchor = new GPoint(34, 0);
        ico.printImage = '/Images/Navigation/markers/printImage.gif';
        ico.mozPrintImage = '/Images/Navigation/markers/mozPrintImage.gif';
        ico.printShadow = '/Images/Navigation/markers/printShadow.gif';
        ico.transparent = '/Images/Navigation/markers/transparent.png';
        ico.imageMap = [67, 0, 67, 1, 67, 2, 67, 3, 67, 4, 67, 5, 67, 6, 67, 7, 67, 8, 67, 9, 67, 10, 67, 11, 67, 12, 67, 13, 67, 14, 67, 15, 67, 16, 67, 17, 67, 18, 67, 19, 67, 20, 67, 21, 67, 22, 67, 23, 67, 24, 67, 25, 67, 26, 67, 27, 67, 28, 67, 29, 67, 30, 67, 31, 67, 32, 67, 33, 67, 34, 67, 35, 67, 36, 67, 37, 67, 38, 67, 39, 67, 40, 67, 41, 67, 42, 67, 43, 67, 44, 67, 45, 67, 46, 67, 47, 67, 48, 67, 49, 67, 50, 67, 51, 67, 52, 67, 53, 67, 54, 67, 55, 67, 56, 67, 57, 67, 58, 67, 59, 67, 60, 67, 61, 67, 62, 67, 63, 67, 64, 67, 65, 67, 66, 43, 67, 42, 68, 42, 69, 42, 70, 41, 71, 41, 72, 41, 73, 40, 74, 40, 75, 40, 76, 39, 77, 39, 78, 39, 79, 38, 80, 38, 81, 37, 82, 37, 83, 37, 84, 36, 85, 36, 86, 36, 87, 35, 88, 35, 89, 35, 90, 34, 91, 34, 92, 34, 93, 33, 94, 33, 94, 33, 93, 32, 92, 32, 91, 32, 90, 31, 89, 31, 88, 31, 87, 30, 86, 30, 85, 30, 84, 29, 83, 29, 82, 29, 81, 28, 80, 28, 79, 28, 78, 27, 77, 27, 76, 27, 75, 26, 74, 26, 73, 26, 72, 25, 71, 25, 70, 25, 69, 24, 68, 24, 67, 0, 66, 0, 65, 0, 64, 0, 63, 0, 62, 0, 61, 0, 60, 0, 59, 0, 58, 0, 57, 0, 56, 0, 55, 0, 54, 0, 53, 0, 52, 0, 51, 0, 50, 0, 49, 0, 48, 0, 47, 0, 46, 0, 45, 0, 44, 0, 43, 0, 42, 0, 41, 0, 40, 0, 39, 0, 38, 0, 37, 0, 36, 0, 35, 0, 34, 0, 33, 0, 32, 0, 31, 0, 30, 0, 29, 0, 28, 0, 27, 0, 26, 0, 25, 0, 24, 0, 23, 0, 22, 0, 21, 0, 20, 0, 19, 0, 18, 0, 17, 0, 16, 0, 15, 0, 14, 0, 13, 0, 12, 0, 11, 0, 10, 0, 9, 0, 8, 0, 7, 0, 6, 0, 5, 0, 4, 0, 3, 0, 2, 0, 1, 0, 0];
        

        address = '<font size="2" face="Arial">' + kraftwerkInfo + '</font>';
        markers[0] = new GMarker(myPoint, { icon: ico, draggable: false });
        markers[1] = new GMarker(myPoint, { icon: ico, draggable: false });
    }
}

function mapLoad(mapIndex) {
    if(mapIndex==0)
        load(mapIndex, G_NORMAL_MAP);
    else
        load(mapIndex, G_SATELLITE_MAP);
}


function load(mapIndex, mapArt) {
    if (GBrowserIsCompatible()) {
        
        
        maps[mapIndex].enableDoubleClickZoom();
        maps[mapIndex].enableScrollWheelZoom();
        maps[mapIndex].addControl(new GMapTypeControl());
        maps[mapIndex].addControl(new GLargeMapControl());

        createContextMenu(mapIndex);

        maps[mapIndex].setCenter(myPoint, 17);
        maps[mapIndex].addOverlay(markers[mapIndex]);
        
        mapArtInverse = (mapArt == G_NORMAL_MAP) ? G_SATELLITE_MAP : G_NORMAL_MAP;
        maps[mapIndex].setMapType(mapArt); //G_NORMAL_MAP || G_SATELLITE_MAP
        maps[mapIndex].removeMapType(mapArtInverse);
        maps[mapIndex].removeMapType(G_HYBRID_MAP);
        
        GEvent.addListener(markers[mapIndex], "click", function () { markers[mapIndex].openInfoWindowHtml(address); });
    }
}

function createContextMenu(mapIndex) {
    contextmenu = document.createElement("div");
    contextmenu.style.visibility = "hidden";
    contextmenu.style.background = "#ffffff";
    contextmenu.style.border = "1px solid #8888FF";

    contextmenu.innerHTML = '<a href="javascript:zoomIn(' + mapIndex + ')"><div class="context">&nbsp;&nbsp;Zoom in&nbsp;&nbsp;</div></a>'
                    + '<a href="javascript:zoomOut(' + mapIndex + ')"><div class="context">&nbsp;&nbsp;Zoom out&nbsp;&nbsp;</div></a>'
                    + '<a href="javascript:zoomInHere(' + mapIndex + ')"><div class="context">&nbsp;&nbsp;Zoom in here&nbsp;&nbsp;</div></a>'
                    + '<a href="javascript:zoomOutHere(' + mapIndex + ')"><div class="context">&nbsp;&nbsp;Zoom out here&nbsp;&nbsp;</div></a>'
                    + '<a href="javascript:centreMapHere(' + mapIndex + ')"><div class="context">&nbsp;&nbsp;Centre map here&nbsp;&nbsp;</div></a>';

    maps[mapIndex].getContainer().appendChild(contextmenu);
    GEvent.addListener(maps[mapIndex], "singlerightclick", function (pixel, tile) {
        clickedPixel = pixel;
        var x = pixel.x;
        var y = pixel.y;
        if (x > maps[mapIndex].getSize().width - 120) {
            x = maps[mapIndex].getSize().width - 120
        }
        if (y > maps[mapIndex].getSize().height - 100) {
            y = maps[mapIndex].getSize().height - 100
        }


        var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x, y));
        pos.apply(contextmenu);
        contextmenu.style.visibility = "visible";
    });
    GEvent.addListener(maps[mapIndex], "click", function () {
        contextmenu.style.visibility = "hidden";
    });
}
function zoomIn(mapIndex) {
    maps[mapIndex].zoomIn();
    contextmenu.style.visibility = "hidden";
}
function zoomOut(mapIndex) {
    maps[mapIndex].zoomOut();
    contextmenu.style.visibility = "hidden";
}
function zoomInHere(mapIndex) {
    maps[mapIndex].zoomIn(maps[mapIndex].fromContainerPixelToLatLng(clickedPixel), true);
    contextmenu.style.visibility = "hidden";
}
function zoomOutHere(mapIndex) {
    maps[mapIndex].setCenter(maps[mapIndex].fromContainerPixelToLatLng(clickedPixel), maps[mapIndex].getZoom() - 1);
    contextmenu.style.visibility = "hidden";
}
function centreMapHere(mapIndex) {
    maps[mapIndex].setCenter(maps[mapIndex].fromContainerPixelToLatLng(clickedPixel));
    contextmenu.style.visibility = "hidden";
}


