/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
	if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
			value = '';
			options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
			var date;
			if (typeof options.expires == 'number') {
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			} else {
				date = options.expires;
			}
			expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
		}
		// CAUTION: Needed to parenthesize options.path and options.domain
		// in the following expressions, otherwise they evaluate to undefined
		// in the packed version for some reason...
		var path = options.path ? '; path=' + (options.path) : '';
		var domain = options.domain ? '; domain=' + (options.domain) : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};

var pdvColors = {
	1  : "hdrb=c90707&hdrc=ffffff&h2c=c90707&wb=ffffff&bb=2d1010",
	2  : "hdrb=ef3d91&hdrc=ffffff&h2c=2a84c2&wb=ffffff&bb=f7a9d0",
	3  : "hdrb=e23279&hdrc=ffffff&h2c=fa2b95&wb=ffffff&bb=483687",
	4  : "hdrb=ffffff&hdrc=ff0283&h2c=ff0283&wb=ffffff&bb=ff0283",
	5  : "hdrb=620303&hdrc=ffffff&h2c=c80007&wb=ffffff&bb=a20404",
	6  : "hdrb=ca0000&hdrc=ffffff&h2c=c80007&wb=ffffff&bb=f4e5e8",
	7  : "hdrb=5664a8&hdrc=ffffff&h2c=454f8c&wb=ffffff&bb=e3e5f1",
	8  : "hdrb=fc69a9&hdrc=ffffff&h2c=454f8c&wb=ffffff&bb=ffe3ef",
	9  : "hdrb=2e406e&hdrc=ffffff&h2c=ff5a00&wb=ffffff&bb=e4ecff",
	10 : "hdrb=736ca0&hdrc=ffffff&h2c=fb0000&wb=ffffff&bb=9c96bc",
	11 : "hdrb=fc3198&hdrc=ffffff&h2c=008ad2&wb=ffffff&bb=ffffff",
	12 : "hdrb=b82c6f&hdrc=ffffff&h2c=008ad2&wb=ffffff&bb=ea3c90",
	13 : "hdrb=a70505&hdrc=ffffff&h2c=b80404&wb=ffffff&bb=e90605",
	14 : "hdrb=ffffff&hdrc=683f6d&h2c=ffffff&wb=ee3d92&bb=b4df26",
	15 : "hdrb=f02c2c&hdrc=ffffff&h2c=04afff&wb=ffffff&bb=fcf3f7",
	16 : "hdrb=fb6700&hdrc=ffffff&h2c=111111&wb=ffffff&bb=f6f6f6",
	17 : "hdrb=6ebee5&hdrc=031526&h2c=ff288f&wb=ffffff&bb=031526",
	18 : "hdrb=cf0b39&hdrc=ffffff&h2c=2b2b2b&wb=ffffff&bb=e62150",
	19 : "hdrb=ff7913&hdrc=ffffff&h2c=2b2b2b&wb=ffffff&bb=ff7913",
	20 : "hdrb=1d4495&hdrc=ffffff&h2c=1d4495&wb=ffffff&bb=e2eafb",
	21 : "hdrb=2e406e&hdrc=ffffff&h2c=2e406e&wb=ffffff&bb=2e406e",
}

function checkMc() {
	//No cookie exists or cookie allow popup
	if ($.cookie('email') === null || $.cookie("email") == "0") {
		return false;
	}

	params = {'email' : $.cookie('email')};
	remote_subscription(params);
	return true;
}

function setMc(email) {
	//if ($.cookie('email') === null)  {
	//	$.cookie("email", Math.floor(Math.random()*2) == 1 ? email : "0", { expires: 31 });
	//}
}

var mc 		= _g("mc", -1);
/*if (mc != 1 && mc != 0) {
	var mc = Math.floor(Math.random()*2);
	if (mc >= 1) {mc=1;}
}*/
mc=1;

document.write("<scr"+"ipt type=\"text/javascript\" src=\"mobile.php?id="+_g('id', 10000)+"&tracker="+_g('tracker', '')+"\"></scr"+"ipt>");

var pdv = window.location.href.match(/pdv(\d+)/);
if (pdv.length>1) {
	pdv = pdv[1];
}

function getTrackerSuffix() {
	var suffix = '';
	//suffix = $.cookie('email') === null ? "" : ($.cookie("email") == "0" ? "_m0" : "_m1");
	//suffix += (mc == 0) ? "_mc0" : "_mc1";

	return suffix;
}

function blink(fldId, default_email) {
	$('#'+fldId).focus(function() {
		$('#'+fldId).css("text-decoration", "none");
		if ($('#'+fldId).val() == '@') $('#'+fldId).val('');
	});
	$('#'+fldId).blur(function() {
		if ($('#'+fldId).val() == '' || $('#'+fldId).val() == '@') {
			$('#'+fldId).css("text-decoration", "blink");
			$('#'+fldId).val('@');
		} else {
			$('#'+fldId).css("text-decoration", "none");
		}
	});
	$('#'+fldId).val(default_email);

	if ($('#'+fldId).val() == '' || $('#'+fldId).val() == '@')
		$('#'+fldId).css("text-decoration", "blink");
}

function remote_subscription(params) {
	pop=0;
	var uri = 'http://r.camsympa.com/u.php?callback=?',
	//var uri = 'http://svn5.carpediem.fr:81/devs/tsvetomir-dev/camsympa/www/u.php?callback=?',
	defaults = {
		'id': id,
		'tracker': tracker + "_880_pdv_"+pdv+getTrackerSuffix(),
		'format': 'json'
	},
	getResults = function (data) { //data represente les données reçues via appel JSON au webservice
		switch (data.code) {
			case 'OK':
				var colors = (typeof pdvColors[pdv] != 'undefined') ? encodeURIComponent(pdvColors[pdv]) : "";
				setMc(params.email);
				if (_g("debug") == 1) {
					alert(data.link+"&colors="+encodeURIComponent(colors));
				}
				window.location = data.link+"&colors="+encodeURIComponent(colors);
				//window.open(data.link, 'test');
				break;
			case 'Error1': //les arguments soumis sont imcomplets
				alert("Erreur d'accès : URL invalide !");
				break;
			case 'Error2': //l'email soumis est invalide
				alert("Erreur : cet email est invalide !");
				break;
			case 'Error3': //la personne est deja inscrite
				alert("Erreur : cet email est déjà inscrit !");
				break;
			default: //ne devrait jamais arrive
				alert(data.result)
				break;
		}
	},
	options = $.extend({}, defaults, params);
	$.getJSON(uri, options, getResults);
}

$.fn.textfill = function(options) {
	var defaults = {
		maxFontPixels: 30,
		innerTag: 'span'
	};
	var fontSize = options.maxFontPixels;
	var myText = $(options.innerTag + ':visible:first', this);
	var maxHeight = $(this).height();
	var maxWidth = $(this).width();
	var textHeight;
	var textWidth;
	do {
		myText.css('font-size', fontSize);
		textHeight = myText.height();
		textWidth = myText.width();
		fontSize = fontSize - 1;
	} while ((textHeight > maxHeight || textWidth > maxWidth) && fontSize > 11);

};
