// wait for the DOM to be loaded 
$(document).ready(function() {	
	//add Class=Last to last child of LIs
	$("li:last-child").addClass('last');
	// To make links external add the external class to them - class="external"
	$("a.external").attr({ target: "_blank"});
	//-------------------------------------------------------------------	
	//---------------------- CONTACT FORM AJAX SUBMISSION ---------------
	//
	var contactFormOptions = { 
        target:        '#contactFormResponse',   // target element(s) to be updated with server response 
        //beforeSubmit:  showRequest,  // pre-submit callback 
        //success:       showResponse,  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        clearForm: true        	 // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#contactForm').ajaxForm(contactFormOptions); 
	
	//-------------------------------------------------------------------	
	//---------------------- PROMO BANNER -------------------------------
	
	//----- Promo Banner Open Close -----

	var $cropbox = $('#promobanner div#crop');
	$cropbox.after('<div id="openclose"><span id="close"><img src="imgs/promo_banner_arrow_up.jpg" alt="Close promo banner" /></span><span id="open"><img src="imgs/promo_banner_arrow_down.jpg" alt="Open promo banner" /></span></div>');
		
	$('div#openclose').toggle(				
		function(){
			$cropbox.fadeOut(400);
			$('div#promobanner img.slideimg').fadeOut(400);
			$('a#backbtn').fadeOut(400);
			$('a#nextbtn').fadeOut(400);
			$('div#navi').fadeOut(400);				
			
			setTimeout(function(){
				$('div#promobanner').animate({height: "26px"}, 500);
				$('span#open').fadeIn('medium');	
				$('span#close').fadeOut('medium');
			}, 410);
		},
		
		function(){
			$('div#promobanner').animate({height: "192px"}, 500);
			$('span#close').fadeIn('medium');
			$('span#open').fadeOut('medium');
			
			setTimeout(function(){
				$cropbox.fadeIn(600);
				$('div#promobanner img.slideimg').fadeIn(300);
				$('a#backbtn').fadeIn(450);
				$('a#nextbtn').fadeIn(450);
				$('div#navi').fadeIn(450);
			}, 650);
			
		}

	);
	
	//----- Promo Banner Slider -----
	
	if (!$("#promobanner div.slide").hasClass("dberror")) {
		//Add Navi and Next/Prev Buttons for navigation
		$("#promobanner #crop").before('<div id="navi"></div>');
		$("#promobanner #crop").after('<a id="backbtn">Back</a>');
		$("#promobanner #crop").after('<a id="nextbtn">Next</a>');

		// initialize scrollable  
	    $('div#crop').scrollable({ 
	        size: 1, 
	        items: 'div#widearea',
			next: 'a#nextbtn',
	   		prev: 'a#backbtn',
			hoverClass: 'hover',
			speed: 500
	    }).circular().autoscroll({
			interval: 10000
		}).navigator({ 
	        navi: "#navi", 
	        activeClass: 'current'
	    });		
	}

	$('a.overlayimg').overlay({
		target: '#overlayimgs',
		//mask: 'black',
		//expose: 'black', 
		closeOnClick: true,
		close: "div.close",
		//effect: 'apple',
		expose: {
			color: 'black',
			loadSpeed: 'normal',
			opacity: 0.5
		},
		onBeforeLoad: function() {
			
			$('#overlayimgs').empty();
			var src=this.getTrigger().attr("href");
			$('#overlayimgs').append("<img src='"+src+"' />");
			$('#overlayimgs').append("<div class='close'></div>");
		
			var self = this;
			$('.close').click(function(){self.close()});					
		}
		
	});
});

// JavaScript Document
//*******************JAVASCRIPT INITIALIZING FUNCTIONS********************************************************
//functions initialize functions

$(window).load(function () { //JQUERY //window.onload = callfunctions;
	if(document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=it" || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=uk"  || 
		
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=it&website=ctassembling"  || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=uk&website=ctassembling"  || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=it&website=meccanicact"  || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=uk&website=meccanicact"  || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=it&website=cttechnology"  || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=uk&website=cttechnology"  
		) {
		load();
	}
	relExternal();
});		
$(window).unload(function () { //JQUERY //window.onunload = removefunctions;
	if(document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=it" || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=uk"  || 
		
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=it&website=ctassembling"  || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=uk&website=ctassembling"  || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=it&website=meccanicact"  || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=uk&website=meccanicact"  || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=it&website=cttechnology"  || 
		document.location.href == "http://www.meccanicact.com/index.php?section=dovesiamo&lang=uk&website=cttechnology"  
		) {
		GUnload();
	}
});		 

//GOOGLE MAPS API FUNCTIONS
function createMarker(point,text) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
	marker.openInfoWindowHtml(text);
  });
  marker.openInfoWindowHtml(text);
  return marker;
}

function load() {
  if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(42.150700,14.466598), 17);
	map.setMapType(G_SATELLITE_MAP);
	//popup
	point = new GLatLng(42.150700,14.466598);
	text = "<div style=\'font-family:Verdana;font-size: 12px;font-color:#6f6c62;text-align:left;\'><strong>CT Group</strong><br />Via Amozzirro,<br />Zona Ind.Le Saletti<br />66020, Paglieta (CH)<br /><strong>Tel:</strong> 0872.809285 <strong><br />Fax:</strong> 0872.808497<br /></div>";
	map.addOverlay(createMarker(point,text));
  }
}
	
//REL EXTERNAL 
//rel external --- overrides the target="_blank" tag for Xhtml validation:
//Before:<a href="document.html" target="_blank">external link</a>
//After:<a href="document.html" rel="external">external link</a>
function relExternal() {
	var anchors = $("a"); //JQUERY
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
	return false;
}