$(function (){

	// plugin definition
	$.fn.crmTracFactory = function(formClass) {
		$("." + formClass ).each (function (){
			$form = $(this);
			
			var opts = [];
			
			$form.find("textarea").each(function (){
				$input = $(this);
				opts[$input.attr("name")] = $input.val();
			});
			$(document).crmTrac(opts);
		});
	};

	// plugin definition
	$.fn.crmTrac = function(options) { 
	
		var defaults = {
			page: 'unkwnown'
			, cookieName : 'crmTracId'
			, visitCookieName : 'crmTracVisitId'
			, destination: ''
			, hotel: ''
			, room: ''
			, rating: ''
			, price : ''
			, availability : ''
			, dateFrom : ''
			, dateTo : ''
			, default_dateFrom : ''
			, default_dateTo : ''
			, nbAdults : 2
			, nbChilds : 0

			, firstName : null
			, lastName : null
			, address : null
			, email : null
			, tel : null
			, cp : null
			, town : null
			, country : null
		};
		
		var opts = $.extend(defaults, options);
		// -------------------------------------------

		var currentUrl = document.location.href;
		var cookieId = getCookie(opts.cookieName, 90*24);
		var visitCookieId = getCookie(opts.visitCookieName, 1);
		var commingLoc = getCommingLocation(currentUrl);
		
		if (opts.availability == "%-components.closeup.product.service.unitbreakdown[0].availability.check-%"){
			opts.availability = "S";
		}
		
		var scheme = (("https:" == document.location.protocol) ? "https://www." : "http://www.");
		
		var url = scheme + "hotelissima.com/twpub/index.cgi?m=crm";
		url += "&cookieid=" + cookieId;
		url += "&visitcookieid=" + visitCookieId;
		url += "&commingloc=" + encodeURIComponent(commingLoc);
		url += "&currenturl=" + encodeURIComponent(currentUrl);
		url += "&page=" + opts.page;
		url += "&destination=" + encodeURIComponent(opts.destination);
		url += "&hotel=" + encodeURIComponent(opts.hotel);
		url += "&room=" + encodeURIComponent(opts.room);
		url += "&price=" + encodeURIComponent(opts.price);
		url += "&rating=" + encodeURIComponent(opts.rating);
		url += "&availability=" + opts.availability;
		url += "&datefrom=" + opts.dateFrom;
		url += "&dateto=" + opts.dateTo;
		url += "&datechanged=" + ((opts.dateFrom != opts.default_dateFrom || opts.dateTo != opts.default_dateTo) ? 'true' : 'false');
		url += "&nbadults=" + opts.nbAdults
		url += "&nbchilds=" + opts.nbChilds
		
		if (opts.firstName != "%-account.details.firstname-%")
			url += "&firstname=" + encodeURIComponent(opts.firstName)
		
		if (opts.lastName != "%-account.details.familyname-%")	
			url += "&lastname=" + encodeURIComponent(opts.lastName)
	
		if (opts.address != "%-account.details.address-%")
			url += "&address=" + encodeURIComponent(opts.address)
	
		if (opts.email != "%-account.details.email-%")
			url += "&email=" + encodeURIComponent(opts.email)
		
		if (opts.tel != "%-account.details.phone-%")
			url += "&tel=" + encodeURIComponent(opts.tel)
		
		if (opts.cp != "%-account.details.zip-%")
			url += "&cp=" + encodeURIComponent(opts.cp)
			
		if (opts.town != "%-account.details.city-%")
			url += "&town=" + encodeURIComponent(opts.town)
			
		if (opts.country != "%-account.details.country-%")
			url += "&country=" + encodeURIComponent(opts.country)
		
		var fn = window.onload;
        window.onload = function (){
            if (fn != null)
                fn();
            callCrm (url);
        }
		
		// -------------------------------------------- //
		function getCommingLocation(url){
			var referer = document.referrer;
			
			if (typeof (referer) == "undefined"){
				return "directLink";
			}
			else if (commingFromOtherSite(referer)){
				return referer;
			}
			else {
				var pos = url.indexOf("#");
				if (-1 != pos){
					return url.substring (pos+1);
				}
			}
			return "?";
		}
		
		function commingFromOtherSite(referer){
			var pos = referer.indexOf("www.hotelissima.com");
			if (pos != 7 && pos	!= 8)
				return true;
			
			return false;
		}
		
		function getCookie(name, time){		
			var cookieId = readCookie(name);
			if (null == cookieId){
				cookieId = createCookie(name, hashCookieId(), time);
			}
			else {
				cookieId = createCookie(name, cookieId, time);
			}
			return cookieId;
		}
		
		
		function callCrm(url){
		    var img = document.createElement("img");
		    img.width = 1;
		    img.height = 1;
		    img.src=url;
		    document.body.appendChild(img);
		}
		
		function hashCookieId() {
		  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
		            return v.toString(16);
		  }).toUpperCase();
		};
		
		function createCookie(name,value,hour){var expires="";if(hour){var date=new Date();date.setTime(date.getTime()+(hour*60*60*1000));expires=";expires="+date.toGMTString();}document.cookie=name+"="+value+expires+";path=/";return value;}function readCookie(name){var nameEQ=name+"=";var ca=document.cookie.split(';');for(var i=0;i<ca.length;i++){var c=ca[i];while(c.charAt(0)==' ')c=c.substring(1,c.length);if(c.indexOf(nameEQ)==0)return c.substring(nameEQ.length,c.length);}return null;}function eraseCookie(name){createCookie(name,"",-1);}
		
	}
});




