/*
Base JS pour Tops Cars
Copyright(c) 2008, Skalpel.

Author : Michael
michael@skalpel.fr

Note : 
	-
ToDo :
	- 
*/

var SKjs = { 
	version: '1.0',
	
	loaderAjax: 'http://pro.tops-cars.fr/javascript/ajax.php',
	loaderJSON: 'http://pro.tops-cars.fr/javascript/json.php',
	encoding: 'iso-8859-1'
};

window.addEvent('domready', function() {
	// Fiche Details
	if($('box-annonce-details')) {
		var SKLightBox = new SKjs.LightBox($$('a[rel=lightbox]'));
		var SkMiniGarage = new SKjs.MiniGarage();
		
		var SKBoxFrame = new SKjs.LightBox($$('a.boxed'), {
			openFromLink: false,
			showTitle: false,
			showNumbers: false,
			showNav: false
		});
	}
	// GMAP
	if($('gmap')) var gmap = new SKjs.GMap();
});


/* 
Class : GMap
	- Gestion de l'affichage de la GMAP
Arguments : 
	options {object} - Objet Options

Options :
	elmGMap {String} - Id du conteneur de la GMAP
	gmapKey {String} - Key GMap
*/
SKjs.GMap = new Class ({
	Implements: [Events, Options],
	options: {
		elmGMap: 'gmap',
		elmProAdress: 'proAdress',
		gmapKey: 'ABQIAAAAqgiQ5moOGneAI65v-xjO8xSgpY7HRYS-_Qu6ugxg6Uj7EsySJxSsm23KTsDoCq6ycO16siZXMJX34g',
	},
	
	initialize: function(options) {
		this.setOptions(options);
		
		this.setGMapApi();
	},
	
	/*
	Property :
		Initialisation et inclusion de l'API de Google Maps
	*/
	setGMapApi: function() {
		var self = this;
		var source = 'http://maps.google.com/maps?file=api&v=2.x&key='+ this.options.gmapKey +'&async=2';
		var script = new Element('script', {'src': source, 'type': 'text/javascript'}).inject(document.head);
		
		waitForExistence();
		function waitForExistence() {
			if(typeof(GMap2) != 'undefined') self.prout();
			else setTimeout(waitForExistence, 10);
		}
	},
	
	/*
	Property :
		Creation de la Map
	*/
	prout: function() {
		var map = new GMap2($(this.options.elmGMap));
		var geocoder = new GClientGeocoder();
		var adress = $(this.options.elmProAdress).get('text');
		
		map.setCenter(new GLatLng(0, 0), 12);
		map.addControl(new GSmallMapControl());
		
		geocoder.getLocations(adress, function(response) {
			// 200 : No errors occurred
			if(response.Status.code == 200) {
				var place = response.Placemark[0];
				var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
				
				map.setCenter(point, 14);
				
				var marker = new GMarker(point);
				map.addOverlay(marker);
			} else {
				$(this.options.elmGMap).empty();	
			}
		});
	}
});


/* Gestion de la Transparence */
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
    window.attachEvent("onload", alphaBackgrounds);
}

function alphaBackgrounds(){
    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
    for (i=0; i<document.all.length; i++){
        var bg = document.all[i].currentStyle.backgroundImage;
        if (itsAllGood && bg){
            if (bg.match(/\-fix.png/i) != null){
                var mypng = bg.substring(5,bg.length-2);
                document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
                document.all[i].style.backgroundImage = "url(no-image)";
            }
        }
    }
}
