//document.domain = "heinekenexperience.com";

var imageCycle = {
    init: function() {
        var videos = ['1_about', '2_brewing', '3_brew_u', '4_taste', '5_raised', '6_interactive'];
        for (var i = 1; i <= 6; i++) {
            var flashvars = {
                video: '/assets/local/video/' + videos[i-1] + '.flv',
				still: '/assets/local/images/experiences/video/' + videos[i-1] + '.jpg'
            };
            var params = {};
            params.wmode = "opaque";
            swfobject.embedSWF("/assets/local/flash/experience_player.swf", "experience-" + i, "583", "331", "9.0.124", false, flashvars, params, {});
        }
    }
}

var slider = {
    frameWidth: 996,
    navFrameWidth: 93,
    currentIndex: 0,
    listCount: 0,
    nav: '#hdp-experience-bottom #navigation',

    init: function() {
        //var previousBtn = $('<a/>').attr('href', '#').addClass('prev-opacity');
        var previousBtn = $('<a/>').attr('href', '#').css('display', 'none');
        var nextBtn = $('<a/>').attr('href', '#');
        var navActive = $('<div/>').attr('id', 'nav-active');

        //inject active frame for navigation
        navActive.appendTo(this.nav);

        //setters for the slides
        this.listCount = $('ul#hdp-experiences li').size();
        $('ul#hdp-experiences').css({ 'overflow': 'hidden', width: this.listCount * this.frameWidth });
        this.listCount--;
        $('ul#hdp-experiences li').css({ background: 'none' });
        //$(this.nav).find('li:eq(0) a').addClass('active');

        //navigation binds
        previousBtn.addClass('previous').text('previous').appendTo('#hdp-experience-top').click(function() {

            slider.slide('previous');

            return false;
        });

        nextBtn.addClass('next').text('next').appendTo('#hdp-experience-top').click(function() {

            slider.slide('next');
            return false;
        });

        $(this.nav).find('li a').click(function() {
            index = $(this).parent().index();
            slider.selectSlide(index);
            return false;
        });
    },
    slide: function(direction) {

        if ((this.currentIndex != 0 && direction == 'previous') || (this.listCount != this.currentIndex && direction == 'next')) {
            if (direction == 'previous') {
                $('ul#hdp-experiences').animate({ marginLeft: '+=' + this.frameWidth }, 800);
                $('#nav-active').animate({ left: '-=' + this.navFrameWidth }, 800);

                this.currentIndex--;

                //sets the opacity for the navigation
                if (this.currentIndex == 0) {
                    $('a.previous').css('display', 'none');
                } else {
                    $('a.next').css('display', 'block');
                }

            } else if (direction == 'next') {
                $('ul#hdp-experiences').animate({ marginLeft: '-=' + this.frameWidth }, 800);
                $('#nav-active').animate({ left: '+=' + this.navFrameWidth }, 800);

                this.currentIndex++;


                //sets the opacity for the navigation
                if (this.currentIndex == this.listCount) {
                    $('a.next').css('display', 'none');
                } else {
                    $('a.previous').css('display', 'block');
                }

            }
        }
    },
    selectSlide: function(i) {

        this.currentIndex = i;
        if (this.currentIndex == 0) {
            $('a.previous').css('display', 'none');
            $('a.next').css('display', 'block');
        } else if (this.currentIndex == this.listCount) {
            $('a.next').css('display', 'none');
            $('a.previous').css('display', 'block');
        } else {
            $('a.next').css('display', 'block');
            $('a.previous').css('display', 'block');
        }
        $('ul#hdp-experiences').animate({ marginLeft: "-" + i * this.frameWidth }, 800);

        //move the small frame, 220 represent the initial left
        $('#nav-active').animate({ left: i * this.navFrameWidth + 220 }, 800);
    },

    hidePlayers: function(i) {
        for (var i = 1; i <= 6; i++) {
            $('#experience-' + i).hide();
        }
    },

    showPlayers: function(num) {
        //console.log(num);
        $('#experience-' + (num+1)).show();
    }
}

var contactMap = {
	map: null,
	container: '#hdp-map',

	init: function() {
		if($(contactMap.container).length == 0)
			return;
		
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("hdp-map"));
			map.setCenter(new GLatLng(52.3585, 4.895), 16);
			map.setUIToDefault();

			contactMap.addMarker(new GLatLng(52.3574, 4.8913));
		}
	},

	addMarker: function(latlng) {
		var customIcon = new GIcon(G_DEFAULT_ICON);
		customIcon.image = '/assets/local/images/mne/contact-marker.png';
		customIcon.transparent = '/assets/local/images/mne/contact-marker-ie.png';
		customIcon.iconSize = new GSize(69,58);
		customIcon.imageMap = [36,0,36,1,37,2,38,3,39,4,40,5,40,6,41,7,42,8,58,9,68,10,68,11,68,12,68,13,68,14,67,15,67,16,67,17,67,18,65,19,64,20,62,21,60,22,58,23,56,24,55,25,56,26,57,27,57,28,58,29,59,30,60,31,60,32,61,33,62,34,63,35,64,36,64,37,65,38,65,39,65,40,65,41,65,42,65,43,65,44,65,45,65,46,65,47,65,48,64,49,63,50,5,50,4,49,3,48,3,47,3,46,3,45,3,44,3,43,3,42,3,41,3,40,3,39,3,38,4,37,4,36,5,35,6,34,7,33,7,32,8,31,9,30,10,29,11,28,11,27,12,26,13,25,12,24,10,23,8,22,6,21,4,20,3,19,1,18,1,17,1,16,0,15,0,14,0,13,0,12,0,11,0,10,25,9,26,8,27,7,28,6,28,5,29,4,30,3,31,2,32,1,32,0];

		var markerOptions = {};
		markerOptions.icon = customIcon;

		var marker = new GMarker(latlng, markerOptions);

		GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindowHtml('<form action="http://maps.google.com/maps" target="_blank" method="get"><input type="hidden" name="daddr" value="Stadhouderskade 78, Amsterdam (Heineken Experience)" /><input type="text" name="saddr" /><input type="submit" class="direction silver-button" value="" /></form>');
		});

		map.addOverlay(marker);
	}
}

function disposeHomePlayer() {
    if ($('#hdp-home-video').length) {
        $('#hdp-home-video').hide();
    }
}

$(document).ready(function() {


    /* ------ states ------*/

    //since div:hover AND div:active states are messy in ie6
    $('.glass-button').hover(function() {
        $(this).css({ 'cursor': 'pointer' });
    }, function() {
        $(this).css({ 'cursor': 'auto' });
    })

    //since div:hover AND div:active states are messy in ie6
    $('.glass-button').mousedown(function() {
        objectHeight = $(this).outerHeight();
        $(this).css({ backgroundPosition: '0px -' + objectHeight + 'px' });
    }).mouseup(function() {
        $(this).css({ backgroundPosition: '0px 0px' });
    })

    $('.glass-button').click(function(e) {
        e.preventDefault();
        clickThrough = $(this).find('a').attr('href');
        window.location = clickThrough;
    })


    /* ------ fancybox init ------ */

    //close the fancybox
    $("#hdp-wrapper-newsitem a#close-iframe").click(function() {
        parent.$.fancybox.close();
        return false;
    })

    /* ------ experience ------*/
    if ($('ul#hdp-experiences li .experience-mediaplayer').length > 0) {
        slider.init();
        imageCycle.init();
    }

    //loads extra images and starts cycling
    //array item = number of images per slides
    //imageCycle.init([7,4,2,1,2,2]);


    /* ----- meeting & events - contact ------ */
    contactMap.init();

    /* ----- wall of fame ----- */
    if ($('#hdp-walloffame-bottom').length) {
        $('.playbutton').hide();
            $('.walloffame-item').hover(function() {
                $(this).find('.playbutton').fadeIn('fast');
            },
            function() {
                $(this).find('.playbutton').fadeOut('fast');
            }
        );
    }

    /* ----- 360 popup ----- */
    if ($('a#360').length) {
        $("a#360").fancybox({
            'hideOnContentClick': false,
            'autoDimensions': false,
            'width': 960,
            'height': 374,
            'titleShow': false,
            'transitionIn': 'elastic',
            'transitionOut': 'fade',
            'speedIn': 600,
            'speedOut': 200,
            'type': 'iframe',
            'padding': 0,
            'margin': 0,
            'overlayShow': true,
            'overlayColor': '#000',
            'overlayOpacity': 0.7,
            'opacity': true,
            'scrolling': 'no',
            'autoScale': false
        });
    }
});