broken.googleMaps.core = 
{
    initialize : function(target, lat, lang, zoom)
    {
        if(GBrowserIsCompatible() && $(target)) {
            
            this.map = new GMap2($(target));
            this.map.setCenter(new GLatLng(lat, lang), zoom);
            GEvent.addListener(this.map, 'zoomend', function()
            {
                if(broken.golfSite.map.getZoom() > 5)
                {
                    $$('.ad').invoke("toggle");
		            $$('.changeLangeAd').invoke("show");
		            $('toggle_ads').toggleClassName('toggle_ads_max', '');
		            $('introtext').hide();
			        $('welcome').setStyle({height: '80px'});
			        $('welcome_background').setStyle({height:'80px'});
			        $('welcome_minimize').addClassName('folded');
			        welcome_folded = true;
                }
            });
            
            mgr = new GMarkerManager(this.map);
            markers = new Array();  
        }
    },
    
    changeZoom : function(way)
	{
		if(this.map.getZoom() > 5 || way == 'in')
		{	
			if(way == 'in')
				this.map.zoomIn();
			else
				this.map.zoomOut();
		}
	},
    
    addMarkers : function(data)
    {
        for(var i in data){
            var iconSmall = new GIcon();
            iconSmall.iconSize = new GSize(11, 19)
            iconSmall.iconAnchor = new GPoint(5, 19);
            
            var iconBig = new GIcon();
            iconBig.iconSize = new GSize(33, 60)
            iconBig.iconAnchor = new GPoint(16, 60);
            iconSmall.image = '/UI/Images/icon.' + data[i].type + '.small.png';
            iconBig.image = '/UI/Images/icon.' + data[i].type + '.big.png';
            var point = new GLatLng(data[i].lat, data[i].lng);
            var markerSmall = new GMarker(point, iconSmall);
            var markerBig = new GMarker(point, iconBig);
            markerSmall.data = data[i];
            markerBig.data = data[i];
            
            GEvent.addListener(markerSmall, 'click', function()
            {
                broken.golfSite.map.panTo(this.getLatLng())
                if(this.data.type== 'golf')
                    broken.golfSite.loadWidget('/UI/Pages/PreviewGolf.js', this.data.id)                    
                else
                    broken.golfSite.loadWidget('/UI/Pages/PreviewHotel.js', this.data.id)
                
            });
            
            GEvent.addListener(markerBig, 'click', function()
            {
                broken.golfSite.map.panTo(this.getLatLng())
                if(this.data.type== 'golf')
                    broken.golfSite.loadWidget('/UI/Pages/PreviewGolf.js', this.data.id)                    
                else
                    broken.golfSite.loadWidget('/UI/Pages/PreviewHotel.js', this.data.id)
            });
                
            mgr.addMarker(markerSmall,5, 7);
            mgr.addMarker(markerBig,8);
        }
        
        //mgr.addMarkers(markers, 5);
        mgr.refresh();
    },
    
    handleErrors : function(message)
    {
        if(typeof message == 'string')
            $('errorDirections').innerHTML = (message);
        else if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	        $('errorDirections').innerHTML = ("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	        $('errorDirections').innerHTML = ("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	        $('errorDirections').innerHTML = ("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
       
        else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	        $('errorDirections').innerHTML = ("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	        $('errorDirections').innerHTML = ("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	    else $('errorDirections').innerHTML = ("An unknown error occurred."+message);
	   
    
    }  
}

broken.golfSite = Object.extend(broken.googleMaps.core,
{
    init : function()
    {
        this.clang = strLang;
        this.coordsDataURL = '/UI/Pages/GetCoords.aspx?lang='+strLang;
        this.initialize('mapHolder', 61.5, 8, 5); 
        this.loadData();
    },
    
    resetZoom : function()
    {
        this.map.setCenter(new GLatLng(61.5, 8), 5);
    },
    
    loadData : function()
    {
        var _this = this;
        new Ajax.Request(this.coordsDataURL, { onSuccess: _this.storeData.bind(this) });
    },
    
    storeData : function(response){
        var data = response.responseText.evalJSON();
        this.addMarkers(data);
    },
        
    loadWidget : function(src, id){
        if(typeof broken.widgetCollection.activeWidget != 'undefined'){
            if($(broken.widgetCollection.activeWidget.id))
                $(broken.widgetCollection.activeWidget.id).remove();
        }
            
        this.currentWidgetID = id;
		new Ajax.Request(src, {
			method: 'get',
            evalJS: true
        });
	},
	
	unLoadWidget : function(){
	    var id = broken.widgetCollection.activeWidget.id;
	    $(id).remove();
	},
	
	togglePackage : function(sender){
	    var li = sender.parentNode.parentNode;
	    if($(sender).hasClassName('down')){
	        sender.removeClassName('down');
	        $(li).select('.information')[0].hide();
	    }else{
	        sender.addClassName('down');
	        $(li).select('.information')[0].show();	        
	    }    
	}, 
	
	setGolfId : function(id){
	    this.golfId = id;
	},
	
	setHotelId : function(id){
	    this.hotelId = id;
	},
	
	bookGolf : function(id)
    {
        var hotelId = false;
        var packages = document.getElementsByName('hoteloffers');
        for(var i = 0; i < packages.length; i++){
            if(packages[i].checked)
                hotelId = packages[i].value;
        }
        
        if(!hotelId)
            throw new Error('Can\'t get packages ID');
        
        var h = hotelId.split('|')[0];
        var g = hotelId.split('|')[1];
        
        var url = 'http://www.firsthotels.com/EntranceMaps.aspx?hotelId='+h+'&offerPageId='+g;
        document.location.href=url;
    },
    
    bookHotel : function(id)
    {   
        var golfId = false;
        var packages = document.getElementsByName('golfofferpackages');
        for(var i = 0; i < packages.length; i++){
            if(packages[i].checked)
                golfId = packages[i].value;
        }
        
        if(!golfId)
            throw new Error('Can\'t get packages ID');
        
        var url = 'http://www.firsthotels.com/EntranceMaps.aspx?hotelId='+id+'&offerPageId='+golfId;
        document.location.href=url;
    }
});

broken.widgetCollection = {};
broken.widget = Class.create();
broken.widget.prototype = {
	initialize: function(){
		if(!$('widgetLoader')) this.createWidgetLoader();
		
		this.arguments = arguments[0];
		
		for(var x in this.arguments.widgets){
			this.id = this.arguments.widgets[x].id;
			this.target = this.arguments.widgets[x].target;
				this.widget = this.arguments.widgets[x]; 
				this.applyStyleSheets(this.arguments.widgets[x].style);
				this.loadHtml(this.arguments.widgets[x].html);
			
        }
    },
    
	createWidgetLoader : function(){
		$$('body')[0].appendChild(Builder.node("div", { id: 'widgetLoader', className: 'widgetWrapper' }));
		$('widgetLoader').style.height = $$('html')[0].scrollHeight + 'px';
	},

    applyStyleSheets : function(url){
        $$('head')[0].appendChild(Builder.node("link", { href: url ,rel: 'stylesheet' }));
    }, 
    
    loadHtml : function(src){
        var _this = this;
        
		new Ajax.Request(src, {method: 'get', onSuccess: _this.onLoaded.bind(this) });       
    },
    
    onLoaded : function(transport){
		$(this.target).innerHTML = transport.responseText;
		this.html = transport.responseText;
		
		if(typeof this.arguments.onSuccess == 'function')
	    	this.arguments.onSuccess();
    
		broken.widgetCollection[this.id] = this;
		broken.widgetCollection.activeWidget = this;
	},

	closeWidget : function(){
	    
	    new Effect.Fade(this.id, { duration: 0.5,
	        afterFinish: function(){
	            $('widgetLoader').remove();
	        }
	    });
		
	}
};

broken.util.addEvent(window,'load', function(event){
    broken.golfSite.init();  
});


var welcome_folded = false;
		
Event.observe(window, 'load', function() {
	
	// Welcome window
	Event.observe('welcome_minimize', 'click', function() {
		if (welcome_folded) { 
			// Unfold window
			$('welcome').setStyle({height: '427px'});
			$('welcome_background').setStyle({height:'427px'});
			$('introtext').show();
			$('welcome_minimize').removeClassName('folded');
			welcome_folded = false;
		}
		else {
			// Fold window
			$('introtext').hide();
			$('welcome').setStyle({height: '80px'});
			$('welcome_background').setStyle({height:'80px'});
			$('welcome_minimize').addClassName('folded');
			welcome_folded = true;
		}
	});
	
	$('languages').hide();
	Event.observe('change_lang', 'click', function() { $('languages').toggle() });
	
	Event.observe('toggle_ads', 'click', function() { 
		$$('.ad').invoke("toggle");
		$$('.changeLangeAd').invoke("show");
		$('toggle_ads').toggleClassName('toggle_ads_max', '');
	});
});
