/*
	Classes
	Created by Dimitri Giani and Marcel Colin Gordon on 2008-01-24.
	Copyright (c) 2008 Dimitri Giani and Marcel Colin Gordon. All rights reserved.
*/

// Various Variables valid for the right behaviors
var _DOC_NAME = "custom-doc";

// Results Class
// This Class manages the Ajax functions in a Table
// Needs:
// Table #results
// tr #Dealers-id
// input in TD with class: toggle and id: toggleDealer-id
// -
var Results = {
	// Generic toggle functions
	toggleInit: function() {
		// look for all elements with class "toggle" and make them toggle when clicked
		var tags = $$('#results input.toggle');
		tags.forEach( function (t) {
			t.onclick = function() {
					Results.toggle(t.id);
					return false;
				}
			});
			
		var tags = $$('a.toggle');
		tags.forEach( function (t) {
			t.onclick = function() {
					Results.toggle_a(t.href);
					return false;
				}
			});
	},
	
	toggle: function (id) {
		// extract the numeric Model ID from the element's id
		var modelID = id.split('-').pop();
		
		// strip the word toggle off the front of the id
		var rowId = id.substring(6);
		
		// ajax url
		var url = baseurl + 'toggle/' + modelID;
		
		// data to send
		var status = $(rowId).hasClass('hidden') ? 1 : 0;
		var data = 'data[status]=' + status;
		
		var toggle = new Ajax(url, {
								 method: 'post'
								,data: data
								,onSuccess: function( responseText ) {
												Results.toggleResponse( rowId, responseText );
												}
								});
		toggle.request();
	},
	
	toggleResponse: function( id, responseText ) {
		if (responseText == '1') {
			$(id).removeClass('hidden');
			$('toggle'+id).setProperty('checked', 'checked');
		} else if (responseText == '0') {
			$(id).addClass('hidden');
			$('toggle'+id).setProperty('checked', '');
		}
	},
	
	// Only for the <a> elements
	toggle_a: function (url) {
		
		$('visibility_text').setText('Aggiorno');
		
		// data to send
		var status = $('theToggler').hasClass('hidden') ? 1 : 0;
		var data = 'data[status]=' + status;
		
		var toggle = new Ajax(url, {
								 method: 'post'
								,data: data
								,onSuccess: function( responseText ) {
												Results.toggleResponse_a( responseText );
												}
								});
		toggle.request();
	},
	
	toggleResponse_a: function( responseText ) {
		
		if (responseText == '1') {
			$('theToggler').removeClass('hidden');
			$('visibility_text').setText('Attualmente sei Visibile');
		} else if (responseText == '0') {
			$('theToggler').addClass('hidden');
			$('visibility_text').setText('Attualmente sei Nascosto');
		}
	},
	
	// Featured Dealers
	
	featuredInit: function() {
		var tags = $$('#results a.featured');
		tags.forEach( function (t) {
				t.onclick = function() {
						Results.featuredExecute(t);
						return false;
						}
				});
	},
	
	featuredExecute: function(t) {
		var status = t.hasClass('off') ? 1 : 0;
		var data = 'data[status]=' + status;
		var toggle = new Ajax(t.href, { method: 'post'
										,data: data
										,onSuccess: function(responseText) {
												Results.featuredResponse(responseText, t.id);
												}
										}
							);
		toggle.request();
	
	},
	
	featuredResponse: function(responseText, id) {
		if (responseText == '1') {
			$(id).removeClass('off');
			$(id).addClass('on');
		} else if (responseText == '0') {
			$(id).removeClass('on');
			$(id).addClass('off');
		}
	}
}
window.addEvent('domready', Results.toggleInit);
window.addEvent('domready', Results.featuredInit);

// iConnectBox
// Needs:
// Anchor with a class: preview only for the iFrame Preview
// Anchor with a class: openElementBox only for open a div with a close button
// -
var iConnectBox = {
	
	// Init function
	init: function() {
		// look for all elements with class "preview"
		var tags = $$('a.preview');
		tags.forEach( function (t) {
			t.onclick = function() {
				iConnectBox.viewBox(t.href);
				return false;
			}
		});
		
		// look for all elements with class "imageView"
		var tags = $$('a.imageView');
		tags.forEach( function (t) {
			t.onclick = function() {
				iConnectBox.viewImage(t.href);
				return false;
			}
		});
		
		// look for all elements with class "openElementBox"
		var tags = $$('a.openElementBox');
		if (tags.length > 0) {
			tags.forEach( function (t) {
				t.onclick = function() {
					iConnectBox.viewElement(t.getProperty('href'));
					return false;
				};
				
				// Set Event to the Close Button
				var elm = t.getProperty('href').split('/');
				$$("#"+elm[1]+" .close").addEvent('click', function() {
					iConnectBox.remElement(elm[1], true);
					return false;
				});
				
			});
		};
	},
	
	// View an Element similar to Lightbox
	viewElement: function(url) {
		this.hideFlash();
		
		// Take the elements
		var elms = url.split('/');
		var elm = elms[1];
		var id  = elms[2];
		
		// Set the Overlay
		iConnectBox.setOverlay(elm, true);
		
		var t = getScrollTop()+40;
		var l = ((getScrollWidth()/2)-($(elm).getStyle('width').toInt()).toInt()/2);
		
		// Set the Element
		$(elm).setStyles({
			position: 'absolute',
			top:  t+'px',
			left: l+'px',
			display: 'block',
			opacity: 0
		});
		$(elm).setStyle('z-index', '101');
		
		// Opacity Effect
		var show = new Fx.Style($(elm), 'opacity', {duration: 200});
		show.start(1);
	},
	
	// Create a div with an iframe and show it similar to Lightbox
	viewBox: function(url) {
		// Set the Overlay
		iConnectBox.setOverlay('view', false);
		
		var t = 20;
		var l = (getWidth()/2)-(490);
		
		var view   = new Element( 'div', {
			'id': 'view',
			'styles': {
					'top':  t+'px',
					'left': l+'px',
					'opacity': '0'
				}
			}
		);
		var toolbar = new Element('div', {
			'styles': {
				'padding': '5px',
				'background': '#fff',
				'text-align': 'right'
			}
		});
		var head = new Element('div', { 'class': 'head' });
		var link = new Element('a', { 'class': 'close' });
		link.addEvent('click', function() { iConnectBox.remElement('view', false); });
		var iframe  = new Element( 'iframe', { 'src': url } );
		
		// Insert Elemenet in the Page
		view.injectBefore($(_DOC_NAME));
		toolbar.injectInside(view);
		head.injectInside(toolbar);
		link.injectInside(head);
		iframe.injectInside(view);
		
		// Opacity Effect
		var show = new Fx.Style($(view), 'opacity', {duration: 200});
		show.start(1);
	},
	
	viewImage: function(url) {
		// Set the Overlay
		iConnectBox.setOverlay('view', false);
		
		var view   = new Element( 'div', {
			'id': 'view',
			'styles': {
					'opacity': '0',
					'display': 'none'
				}
			}
		);
		var toolbar = new Element('div', {
			'styles': {
				'padding': '5px',
				'background': '#fff',
				'text-align': 'right'
			}
		});
		var head = new Element('div', { 'class': 'head' });
		var link = new Element('a', { 'class': 'close' });
		link.addEvent('click', function() { iConnectBox.remElement('view', false); });
		var image  = new Element( 'img', { 'src': url, 'id': 'theImageView' } );
		
		// Insert Elemenet in the Page
		view.injectBefore($(_DOC_NAME));
		toolbar.injectInside(view);
		head.injectInside(toolbar);
		link.injectInside(head);
		image.injectInside(view);
		
		image.onload = function()
		{
			var t = (getHeight()/2)-(image.height/2);
			var l = (getWidth()/2)-(image.width/2);
			view.setStyles({
				top: t+'px',
				left: l.toInt()+'px',
				display: 'block'
			});
			var show = new Fx.Style($(view), 'opacity', {duration: 200});
			show.start(1);
		}
		
	},
	
	// Set the Overlay
	setOverlay: function(elm, hide) {
		var firstElement = $(_DOC_NAME);
		var overlay = new Element('div');
		overlay.setProperty('id', 'overlay');
		overlay.setStyles({
			position: 'absolute',
			top: '0',
			left: '0',
			width: '100%',
			height: getScrollHeight()+'px',
			background: '#000'
		});
		overlay.setStyle('z-index', '100');
		overlay.setOpacity(0.75);
		overlay.onclick = function() { iConnectBox.remElement(elm, hide); }

		overlay.injectBefore(firstElement);
	},
	
	// Remove or Hide the Elements
	remElement: function(elm, hide) {
		// Opacity Effect
		var rem = new Fx.Style($(elm), 'opacity', {duration: 200});
		rem.start(0);

		rem.addEvent('onComplete', function() {
			// Check if hide or remove element
			if (hide) {
				$(elm).setStyle('display', 'block');
			} else {
				$(elm).remove();
			}
			
			iConnectBox.delOverlay();
		});
	},
	
	// Delete Overlay
	delOverlay: function() {
		// Opacity Effect
		var rem = new Fx.Style($('overlay'), 'opacity', {duration: 200});
		rem.start(0);

		rem.addEvent('onComplete', this.removeOverlay);
	},
	
	removeOverlay: function() {
		if ($('overlay')) { $('overlay').remove(); }
		iConnectBox.showFlash();
	},
	
	// Hide Flash Elements
	hideFlash: function() {
		
		var tags = $$('div.swfobject');
		if (tags.length > 0) {
			for (a=0;a<tags.length;a++) {
				tags[a].setStyle('visibility', 'hidden');
			};
		};
		
		/*
		// Don't Works ON IE
		var tags = $$('object');
		if (tags.length > 0) {
			for (a=0;a<tags.length;a++) {
				tags[a].setStyle('visibility', 'hidden');
			};
		};
		
		var tags = $$('embed');
		if (tags.length > 0) {
			for (a=0;a<tags.length;a++) {
				tags[a].setStyle('visibility', 'hidden');
			};
		};*/
	},
	
	// Show Flash Elements
	showFlash: function() {
		var tags = $$('div.swfobject');
		if (tags.length > 0) {
			for (a=0;a<tags.length;a++) {
				tags[a].setStyle('visibility', 'visible');
			};
		};
		
		/*
		// Don't Works ON IE
		var tags = $$('object');
		if (tags.length > 0) {
			for (a=0;a<tags.length;a++) {
				tags[a].setStyle('visibility', 'visible');
			};
		};
		
		var tags = $$('embed');
		if (tags.length > 0) {
			for (a=0;a<tags.length;a++) {
				tags[a].setStyle('visibility', 'visible');
			};
		};*/
	}
	
};
window.addEvent('domready', iConnectBox.init);

// Map Class
// This Class displays a Google Map
// Needs:
// div#googleMap  - div to toggle map display
// div#mapwrap    - div to hold map and loading message
// div#maploading - div for the loading icon
// -
var Map = {
	
	init: function() {
		//Map.toggle();
	},
	
	toggle: function() {
		
		if ($('googleMap').getStyle('display') != 'block') {
			
			$('googleMap').setStyle('display', 'block');
			var ss = new Fx.Style('googleMap', 'height', {duration:500});
			ss.addEvent('onComplete', function() {
				$('mapwrap').setStyle('display', 'block');
				Map.render();
			});
			ss.start(415);
			
			$('linkMap').setText("Nascondi la Mappa");
			
		} else {
			
			$('mapwrap').setStyle('display', 'none');
			var ss = new Fx.Style('googleMap', 'height', {duration:500});
			ss.start(0);
			ss.addEvent('onComplete', function() {
				$('googleMap').setStyle('display', 'none');
			});
			$('linkMap').setText("Mostra la Mappa");
			
		}
	},
	
	render: function() {
		
		if ( GBrowserIsCompatible() ) {
			Map.map = new GMap2($("map"));
			
			Map.map.addControl( new GLargeMapControl() );
			Map.map.addControl( new GMapTypeControl() );
			
			var mapCenter = new GLatLng( googleMapLt, googleMapLn);
			Map.map.setCenter(mapCenter, googleMapZm);
			
			var mapMarker = new GMarker(mapCenter);
			Map.map.addOverlay(mapMarker);
			
			if ($('maploading')) { $('maploading').remove(); }
			
			window.addEvent('unload', GUnload);
		}
	}
}


// External Link Class
var ExternalLinks = {
	
	init: function() {
		
		var anchor = $$('a');
		anchor.forEach( function (a) {
			if (a.getAttribute('rel') == 'external') {
				a.target = '_blank';
			}
		});
	}
	
}
window.addEvent('domready', ExternalLinks.init);

// Tools
var Tools = {
	show: function( id ) { $(id).setStyle('display', 'block'); }
	,hide: function( id ) { $(id).setStyle('display', 'none'); }
}


