var popup = {
    open: function(src) {
        popup.close();

        $('#mainContainer').append('<div class="popup popupVideo">' +

        '<div class="popupA"><div class="popupB"><div class="popupC"><div class="popupD"><div class="popupE"><div class="popupF">' +
        '<div class="popupInner">' +
        '<a href="#x" class="close" onclick="popup.close();return false;"><span class="forBlind">X</span></a>' +

        '<!--[if !IE]> -->' +
        '<object class="flashPlayerObject" type="application/x-shockwave-flash" data="' + src + '" width="530" height="327" id="flashPlayerObject">' +
        '<!-- <![endif]-->' +
        '<!--[if IE]>' +
        '<object id="flashPlayerObject" class="flashPlayerObject" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="530" height="327" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0">' +
        '<param name="movie" value="' + src + '" />' +
        '<!--><!-- -->' +
        '<param name="loop" value="true" />' +
        '<param name="menu" value="false" />' +
        '<param name="wmode" value="transparent" />' +
        '<param name="allowscriptaccess" value="always" />' +
        '<param name="allowfullscreen" value="true" />' +
        '<div class="flashAlternative">' +

        '</div>' +
        '</object>' +
        '<!-- <![endif]-->' +

        '</div>' +
        '</div></div></div></div></div></div>' +
        '</div>');
    },
    close: function(){
        if (popup.parentOfOpened) {
            $(popup.parentOfOpened).append($('#main').find('.popupContent'));
            $(popup.parentOfOpened).removeClass('selected');
            popup.parentOfOpened = false;
        }
        $('#mainContainer').children('.popup').remove();
    },
    parentOfOpened: false
}

var places = {
    config: {},
    init: function(){
        places.el.wrapper = $('#places');
		// no places
        if (!places.el.wrapper.length)
            return false;
		// no google maps
        if (typeof(google.maps) !== 'object')
            return false;

        places.el.wrapper.hide();
        // create map
        places.createMap();
        // find items and create locations
        places.el.items = places.el.wrapper.children('.item');
        places.el.items.each(function(){
            places.createLocation($(this));
        });
    },
    createMap: function() {
	var pmc = $('#placesMapCenter').text();
	var pos = "";
	var zoom = 12;

	if (pmc != undefined && pmc.indexOf("|") >= 0) {
		var arr = pmc.split("|");
		pos = arr[0];
		zoom = (parseInt(arr[1]) <= 0) ? zoom : parseInt(arr[1]);
	} else {
		pos = pmc;
	}

        // get gps coords
        var latlng = places.parseGps(pos);
        // define center of map
        var mapCenter = new google.maps.LatLng(latlng[0], latlng[1]);
        // define map options
        var mapOptions = {
            zoom: zoom,
            center: mapCenter,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
			      disableDefaultUI: true,
			      mapTypeControl: false,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                position: google.maps.ControlPosition.BOTTOM_LEFT
            },
            navigationControl: false,
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.ZOOM_PAN,
                position: google.maps.ControlPosition.BOTTOM_LEFT
            },
            scaleControl: false,
            scaleControlOptions: {
                position: google.maps.ControlPosition.BOTTOM_LEFT
            }
        };
        // create map from options and center
        places.map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    },
    createLocation: function(el){
        var locoptions = {};

        var ico = "gmap-marker.png";
        var loc = el.find('.location').text();
        if (loc == undefined || loc == "") {
        	ico = "gmap-marker1.png";
        	loc = el.find('.location1').text();
	 }

        // get gps coords
        var latlng = places.parseGps(loc);
        // create gps location
        locoptions.markerPos = new google.maps.LatLng(latlng[0], latlng[1]);

        // create marker
        locoptions.marker = new google.maps.Marker({
            position: locoptions.markerPos,
            title: el.find('.title').text(),
            icon: './public/images/' + ico,
            map: places.map
        });

    },
    parseGps: function(text){
        return text.replace(/\s/, '').split(/,|;/);
    },
    el: {},
    locations: []
};

window.onload = function(){
    //    init_map();
    places.init();
};

$(document).ready(function() {

	Cufon.replace('h1 ,h2, h3', { fontFamily: 'NIF' });
	Cufon.replace('#leftMenu a', { fontFamily: 'NIF', hover: true });


	$('ul#leftMenu>li.selected').each(function(index) {
		if($(this).children("ul").size() == 0) {
			$(this).css("margin-bottom","0px");
		}
	});

	$(function(){
			if($('#slides').length) {
				$('#slides').slides({
					preload: true,
					preloadImage: '../slides/img/loading.gif',
					play: 5000,
					pause: 2500,
					hoverPause: true,
					animationStart: function(){
						$('.caption').animate({
							right:-340
						},100);
					},
					animationComplete: function(current){
						$('.caption').animate({
							right:0
						},200);
						if (window.console && console.log) {
							// example return of current slide number
							console.log(current);
						};
					}
				});
			}
		});

	if ($('.galleryslide').length) {
		hs.graphicsDir = '/global/js/galleryslide/graphics/';
		hs.align = 'center';
		hs.transitions = ['expand', 'crossfade'];
		hs.fadeInOut = true;
		hs.dimmingOpacity = 0.8;
		hs.wrapperClassName = 'borderless floating-caption';
		hs.captionEval = 'this.thumb.alt';
		hs.marginRight = 100; // make room for the thumbstrip
		hs.marginBottom = 80 // make room for the controls and the floating caption
		hs.numberPosition = 'caption';
		hs.lang.number = '%1/%2';

		// Add the slideshow providing the controlbar and the thumbstrip
		hs.addSlideshow({
			//slideshowGroup: 'group1',
			interval: 5000,
			repeat: false,
			useControls: true,
			overlayOptions: {
				className: 'text-controls',
				position: 'bottom center',
				relativeTo: 'viewport',
				offsetX: 50,
				offsetY: -5
			},

			thumbstrip: {
				position: 'middle right',
				mode: 'vertical',
				relativeTo: 'viewport'
			}
		});

		// Add the simple close button
		hs.registerOverlay({
			html: '<div class="closebutton" onclick="return hs.close(this)" title="Close"></div>',
			position: 'top right',
			fade: 2 // fading the semi-transparent overlay looks bad in IE
		});
	};

	GVid = function (obj, src) {
		popup.close();
		//var parent1 = $('#carousel .selected');
		//parent1.removeClass('selected');

        var parent = $(obj).parents('li');
        popup.open(src);
        popup.parentOfOpened = parent;
        //parent.addClass('selected');

		return false;
	};
});



