var lang = 'fr';

function getHTTPObject(myFunction, myArgs)	{
	var xmlhttp = false;
  /*@cc_on
  @if (@_jscript_version >= 5)
     try	{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)	{
        try	{
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)	{
           xmlhttp = false;
        }
     }
  @else
     xmlhttp = false;
  @end @*/
  
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')	{
     try	{
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)	{
        xmlhttp = false;
     }
  }

  if (xmlhttp)	{
     xmlhttp.onreadystatechange=function()	{
        if (xmlhttp.readyState == 4)	{
           if (xmlhttp.status == 200)	{
				call = myFunction+'(';
				for(i=0;i<myArgs.length;i++)	{
					if (i>0) call += ',';
					call += myArgs[i];
				}
				call += ','+xmlhttp.responseText;
				call += ')';
				//alert(call);
				eval(call);
           }
        }
     }
  }
  return xmlhttp;
}


$(document).ready(function() {

	/* t-shirt */
	
	$('div.tshirt-models input').each(function() {
		$(this).click(function() {
			model = $(this).attr('value');
			size = $('#prevsize').attr('value');
			color = $('#prevcolor').attr('value');
			$('div.tshirt-sizes').load("/js/tshirt-sizes.php?model="+model+"&size="+size);
			$('div.tshirt-colors').load("/js/tshirt-colors.php?model="+model+"&color="+color);
		});
	});

	/* login */
	
	var labelLogin = $('#login').attr('value');
	var labelPassword = $('#password').attr('value');
	
	$('#login').focus(function() {
		if($(this).attr('value') == labelLogin) $(this).attr('value', '');
		else $(this).select();
	});
	$('#login').blur(function() {
		if($(this).attr('value') == '') $(this).attr('value', labelLogin);
	});

	$('#password').focus(function() {
		if($(this).attr('value') == labelPassword)	{
			$(this).replaceWith('<input type="password" name="password" class="password" id="password" />');
			$('#password.password').focus();
		/*	$('#password.password').blur(function() {
				if($(this).attr('value') == '')	{
				$(this).replaceWith('<input type="text" name="password" id="password" value="'+labelPassword+'" />');
				}
			}); */
		}
	});

	/* contest */
	
	$('div.contest a.button').click(function() {
		$('div.contest form').submit();
	});

	/* create */

	$('ul.create-type li input').each(function() {
		$(this).click(function() {
			type = $(this).attr('id').replace(/type-/ig,'');
			$('div.create-blocks > fieldset').hide();
			$('fieldset.'+type).show();
		});
	});
	
	$('div.create-blocks fieldset').each(function() {
		$(this).hide();
		// $(this).css('position', 'absolute');
	});
	

	/* decode/phone */
	
	// hide models and readers
	$('li.models').css('display', 'none');
	$('li.readers').css('display', 'none');

	// brands are clickable
	$('li.brands ul li').each(function() {
		$(this).click(function() {
			$('li.readers').css('display', 'none');
			$('li.brands ul li').each(function() { $(this).removeClass('on'); });
			$(this).addClass('on');
			brand = $(this).attr('id').replace(/brand-/ig,'');
			$('li.models div').load("/js/phone-models.php?brand="+brand, refreshReaders);
		});
	});

	// models are clickable
	function refreshReaders()	{
		$('li.models').css('display', 'block');
		$('li.models ul li').each(function() {
			$(this).removeClass('on');
			$(this).click(function() {
				$('li.models ul li').each(function() { $(this).removeClass('on'); });
				$(this).addClass('on');
				model = $(this).attr('id').replace(/model-/ig,'');
				$('li.readers div').load("/js/phone-readers.php?model="+model, function() {$('li.readers').css('display', 'block');});
			});
		});
	}

	$('#description').click(function () { this.select();});
	$('form.qr-library div.small-button').click(function () { $('form.qr-library').submit()});

	$('form.qr-email div.small-button').click(function () { $('form.qr-email').submit()});

	$('form.qr-email input.text').click(function () { this.select()});

	$('#topmsg').slideDown("slow");
	$('#topmsg').click( function () {$('#topmsg').slideUp("fast")});
			
});

function googlego()	{
	showAddress($('#address').attr('value'));
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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);
}

function confirmRemove(short_code) {

	var deleteConf = confirm("Êtes-vous sûr de vouloir effacer ce QR code ?");

	if (deleteConf== true) {
	   window.location="/library/?remqr="+short_code;
	} else {
		window.location="/library/";
	}
}

var map = null;
var geocoder = null;

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(50.82445,  4.35995), 13);
		map.addControl(new GLargeMapControl());
		geocoder = new GClientGeocoder();
	}
}

function fbs_click(url) {
	u=url;
	t="SEQRET code";
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function showAddress(address) {
	createCookie('ckaddress', address, 1);

	if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  alert(address + " not found");
		} else {
		  map.setCenter(point, 13);
		  var marker = new GMarker(point);
		  map.addOverlay(marker);
		  marker.openInfoWindowHtml(address);
		}
	  }
	);
  }
}

