﻿
// Pasted from golf.js in golf.firsthotels.com
var c9 = Class.create();
c9.googleMaps = Class.create();

c9.googleMaps.core = 
{

    framesetOffsetX : 0,
    framesetOffsetY : 0,

    initialize : function(target, latLang, z)
    {
        if(GBrowserIsCompatible() && $(target)) 
        {
            this.framesetOffsetY = $('mapWrapper').offsetTop;
            this.framesetOffsetX = $('framework').offsetLeft + $('mapWrapper').offsetLeft;
            
            var x = latLang.split(',')[0];
            var y = latLang.split(',')[1];
                         
            this.map = new GMap2($(target));
            this.map.addControl(new GLargeMapControl())
            this.map.setCenter(new GLatLng(parseFloat(x),parseFloat(y)), parseFloat(z));

            mgr = new GMarkerManager(this.map);
        }
    },
    
    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 = 'http://golf.firsthotels.com/UI/Images/icon.' + data[i].type + '.small.png';
            iconBig.image = 'http://golf.firsthotels.com/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()
            {
                c9.firstHotels.map.panTo(this.getLatLng())
                c9.firstHotels.loadWidget('/Templates/Public/Scripts/Widgets/PreviewHotel.js', this.data.id)
            });
            
            GEvent.addListener(markerBig, 'click', function()
            {
                c9.firstHotels.map.panTo(this.getLatLng())
                c9.firstHotels.loadWidget('/Templates/Public/Scripts/Widgets/PreviewHotel.js', this.data.id)
            });
                
            mgr.addMarker(markerSmall,1, 7);
            mgr.addMarker(markerBig,8);
        }
        
        mgr.refresh();
    }
}

c9.firstHotels = Object.extend(c9.googleMaps.core,
{

    init : function()
    {
        this.clang = $('hiddenLang').value;
        this.coordsDataURL = '/Templates/Public/Pages/GetCoords.aspx?pid=' + $('hiddenID').value + '&epslanguage=' + $('hiddenLang').value;
        this.initialize('mapHolder',$('hiddenXY').value,$('hiddenZ').value);
        this.loadData();
    },
    
    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 c9.widgetCollection.activeWidget != 'undefined'){
            if($(c9.widgetCollection.activeWidget.id))
                $(c9.widgetCollection.activeWidget.id).remove();
        }
            
        this.currentWidgetID = id;
		new Ajax.Request(src, {
			method: 'get',
            evalJS: true
        });
	},
	
	unLoadWidget : function(){
	    var id = c9.widgetCollection.activeWidget.id;
	    $(id).remove();
	}
});

c9.widgetCollection = {};
c9.widget = Class.create();
c9.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();
    
		c9.widgetCollection[this.id] = this;
		c9.widgetCollection.activeWidget = this;
	},

	closeWidget : function(){
	    
	    new Effect.Fade(this.id, { duration: 0.5,
	        afterFinish: function(){
	            $('widgetLoader').remove();
	        }
	    });
		
	}
};


c9.util = {
    isIE : Prototype.Browser.IE,
    isIE6 : '',
    isIE7 : '',
    isCompleted : false,
    
    init : function()
    {
        if (this.isIE)
        {
			this.isIE6 = !!(typeof document.body.style.maxHeight == 'undefined' && Prototype.Browser.IE);
			this.isIE7 = !!(typeof document.body.style.maxHeight != 'undefined' && Prototype.Browser.IE);
        }
    },
    
	getCursor : function(dDom)
	{	
		if (document.all)
			dDom.style.cursor='hand';
		else
			dDom.style.cursor='pointer';
	}
};

broken.util.addEvent(window,'load', function(event){
	c9.util.init();
    c9.firstHotels.init();
});
