/************************************************************************************************************************************
* loadswf
************************************************************************************************************************************/
function loadswf(id, flashvars) {
 	var height = $('div#' + id).css('height').replace('px', '');
 	var width = $('div#' + id).css('width').replace('px', '');
 	var file = 'swf/' + id + '.swf';
 	var so = new SWFObject(file, id, width, height, 10, '#FFF');

 	if (typeof flashvars != 'undefined') so.addParam('flashvars', flashvars);
 	
	so.addParam('wmode', 'transparent'); 
	so.write(id);
}

/************************************************************************************************************************************
* loadsocial
************************************************************************************************************************************/
function loadsocial() {
	$('div.social').each(function () {
		$(this).find('a.facebook_btn').click(function () {
			$('div.social_facebook').fadeIn();
			
			return false;
		});
		
		$(this).find('a.twitter_btn').click(function () {
			$('div.social').children('div').fadeOut();
			$('div.social_twitter').fadeIn();

			$('a.twtr-join-conv').html('Síguenos en Twitter');
			$('#twtr-widget-1 h4').html('@Unimarc');
			
			twitter_box.start();
			return false;
		});

		loadclose();
	});
}

function loadclose() {
	$('a.close_btn').click(function () {
		$(this).parent().fadeOut();
					
		return false;
	});
}

/************************************************************************************************************************************
* loadhome
************************************************************************************************************************************/
function loadhome() {
	$.region('select#region', 'select#comuna');
	
	$('a.otrocupon_btn').click(function () {
		$(this).parent().hide();
		$(this).parent().parent().find('form input#cupon').val('');
		$(this).parent().parent().find('form').fadeIn();
		
		return false;
	});
	
	$('a.jugar_btn').click(function () {
		var classname = 'juego';
		var pos = $('div.' + classname).parent().position();
		var _pos = $('div#content').position();

		$('div#content').stop().animate( { left: -pos.left }, 500 )
		pageTracker._trackPageview('juego-registro');
		
		//t1 = new Tween($('div#content')[0].style, 'left', Tween.regularEaseInOut, _pos.left, -(pos.left), 1, 'px');
		//t1.start();
	});

	$('div.home div.registro form').submit(function () {
		var datafield = '';
		$(this).find('input:not(.checkbox, #cupon), select').each(function () {
			datafield += '&' + $(this).attr('name') + '=' + $(this).val();
		});
		$(this).find('input:checkbox:not(#bases)').each(function () {
			datafield += '&' + $(this).attr('name') + '=' + $(this).attr('checked');
		});
		
		datafield += '&cupon=' + $('input#cupon').val();
		
		$.ajax({
			url: 'ajax/registro.ajax.php',
			data: datafield,
			type: 'post',
			dataType: 'json',
			success: function (json) {
				$('label.error').removeClass('error');
				
				if (json.status) {
					$('div.home form').each(function () {
						$(this).hide();
						$(this).parent().find('div.gracias').fadeIn();
						loadjuego();
					});
				} else {
					for (field in json) {
						if (!json[field]) $('label[for=' + field + ']').addClass('error');
					}
				}
			}
		});
		
		return false;
	});
}

/************************************************************************************************************************************
* loadcontacto
************************************************************************************************************************************/
function loadcontacto() {
	$('div.contacto form').submit(function () {
		var datafield = '';
		$(this).find('input, textarea, select').each(function () {
			datafield += '&' + $(this).attr('name') + '=' + $(this).val();
		});

		$.ajax({
			url: 'ajax/contacto.ajax.php',
			data: datafield,
			type: 'post',
			dataType: 'json',
			success: function (json) {
				$('label.error').removeClass('error');
				
				if (json.status) {
					$('div.contacto form').each(function () {
						$(this).hide();
						$(this).parent().find('div.gracias').fadeIn();
					});
				} else {
					for (field in json) {
						if (!json[field]) $('label[for=' + field + ']').addClass('error');
					}
				}
			}
		});
		
		return false;
	});
}

/************************************************************************************************************************************
* loadmarcas
************************************************************************************************************************************/
function loadmarcas() {
	$('div.marcas').each(function () {
		$($(this).find('div.thumb').children('img')[0]).fadeIn();
	
		$(this).find('div.paginacion').each(function () {
			var pag = 0;
			
			$(this).find('a.next_btn').click(function () {
				pag++;

				if (pag >= $(this).parent().parent().parent().parent().parent().find('div.thumb').children('img').length - 1) pag = $(this).parent().parent().parent().parent().parent().find('div.thumb').children('img').length - 1;
				
				$(this).parent().parent().parent().parent().parent().find('div.thumb').children('img').hide();
				$($(this).parent().parent().parent().parent().parent().find('div.thumb').children('img')[pag]).fadeIn();

				return false;
			});
			$(this).find('a.prev_btn').click(function () {
				pag--;
				if (pag <= 0) pag = 0;

				$(this).parent().parent().parent().parent().parent().find('div.thumb').children('img').hide();
				$($(this).parent().parent().parent().parent().parent().find('div.thumb').children('img')[pag]).fadeIn();

				return false;
			});
			$(this).find('a:not(.prev_btn,.next_btn)').click(function () {
				pag = $(this).html() - 1;
				$(this).parent().parent().parent().parent().parent().find('div.thumb').children('img').hide();
				$($(this).parent().parent().parent().parent().parent().find('div.thumb').children('img')[pag]).fadeIn();
				
				return false;
			});
		});
	})
}

/************************************************************************************************************************************
* loadall
************************************************************************************************************************************/
function loadall() {
	var width = $('body').width();
	var cant = $('div#content div.wrap').length;
	
	$('div#content div.wrap').width(width)
	$('div#content').width(width * cant);
	
	$('div#header ul a').click(function () {
		var classname = $(this).attr('class');
		var pos = $('div.' + classname).parent().position();
		
		pageTracker._trackPageview(classname);
		
//		t1 = new Tween($('div#content')[0].style, 'left', Tween.regularEaseInOut, _pos.left, -pos.left, 1, 'px');
//		t1.start();

		$('div#content').stop().animate( { left: -pos.left }, 500 )
		
//		$('div#content').css('left', -pos.left);
		
		return false;
	});
}

/************************************************************************************************************************************
* .juego
************************************************************************************************************************************/
function loadjuego() {
	$.ajax({
		url: 'ajax/juego.ajax.php',
		success: function (html) {
			$('div.juego').html(html);
			if (typeof $('div.login')[0] != 'undefined') login();
			if (typeof $('div#map')[0] != 'undefined') {
				loadmap();
				loadquestion();
			}
			if (typeof $('div#juego2')[0] != 'undefined') loadswf('juego2');
			if (typeof $('div#juego2_ganador')[0] != 'undefined') loadswf('juego2_ganador');

			$(document).pngFix();
		}
	});
}

function login() {
	$('div.juego div.login form').submit(function () {
		$.ajax({
			url: 'ajax/login.ajax.php',
			data: '&email=' + $('div.juego input#email_login').val() + '&rut=' + $('div.juego input#rut_login').val(),
			dataType: 'json',
			type: 'post',
			success: function (json) {
				if (json.status) {
					loadjuego();
				} else {
					$('div.juego div.login form p').fadeIn();
				}
			}
		});
			
		return false;
	});
}

/************************************************************************************************************************************
* Mapa
************************************************************************************************************************************/
var gmap;

function loadmap() {
	var div = $('#map')[0];
	gmap = new GMap2(div);
	
	gmap.setUIToDefault();
		
	$.ajax({
		url: 'ajax/ubicaciones.ajax.php',
		dataType: 'json',
		success: function (json) {
			if (json.status) {
				gmap.setCenter(new GLatLng(json.lat,json.lng), json.zoom);
			
				for (i = 0; i < json.list.length; i++) {
					var elem = json.list[i];
					var gicon = new GIcon(G_DEFAULT_ICON);
					gicon.image = 'images/icons/' + elem.icon + '.png';
					gicon.shadow = 'images/icons/shadow.png';
					gicon.iconSize = new GSize(30, 50);
					gicon.shadowSize = new GSize(51, 50);
					gicon.iconAnchor = new GPoint(15, 50);
					
					var marker = new GMarker(new GLatLng(elem.lat, elem.lng), { icon: gicon });
					GEvent.addListener(marker, "click", function () {
						var marker = this;
						latlng = marker.getLatLng();			
						loadquestion(latlng.lat(), latlng.lng());
					});
					gmap.addOverlay(marker);
				}
			}
		}
	});
}

function loadquestion(lat, lng) {
	if ((typeof lat == 'undefined') || (typeof lng == 'undefined')) {
		var lat = 0;
		var lng = 0;
	}
	
	$.ajax({
		url: 'ajax/question.ajax.php',
		async: false,
		dataType: 'json',
		type: 'post',
		data: '&lat=' + lat + '&lng=' + lng,
		success: function (json) {
			if (json.status) {
				if (json.num_question == "3") {
					$('div.juego1 div.block_win').each(function () {
						$(this).fadeIn();
						$(this).find('a.continuar_btn').click(function () {
							loadjuego();
						});
					});
				} else {
					if (json.num_question != 0) json.question = '<p>' + (parseInt(json.num_question)+1) + ') ' + 'Acertaste, continúa con la siguiente pregunta: </p><p>' + json.question + '</p>';
				
					if ((lat != 0) && (lng != 0)) {
						gmap.openInfoWindow(new GLatLng(lat, lng), json.question, { maxWidth: 300, noCloseOnClick: true });
					} else {
						$('div.block_instrucciones').fadeIn();
						$('div.block_instrucciones a.continuar_btn').click(function () {
							$(this).parent().fadeOut();
						});
					}

					$('div.juego1 div.block_pregunta p.pregunta').html(json.question);
					$('div.juego1 div.block_pregunta span.oportunidades').html(json.juegos);
				}
			} else {
				if (json.juegos) {
					$('div.juego1 div.block_perdiste').each(function() {
						$(this).fadeIn();
						$(this).find('a.otrocupon_btn').click(function () {
							window.location = 'index.php';
						});
						$(this).find('a.volverjugar_btn').click(function () {
							loadjuego();
						});
					});
				} else {
					$('div.juego1 div.block_sinjuegos').each(function () {
						$(this).fadeIn();
						$(this).find('a.otrocupon_btn').click(function () {
							window.location = 'index.php';
						});
					});
				}
			}
		}
	});
	
	$('div.juego1 a.pregunta_btn').click(function () {
		$('div.juego1 div.block_pregunta').each(function () {
			if ($(this).css('display') == 'none') {
				$(this).fadeIn();
				var pos = $(this).parent().position();
				
				t1 = new Tween($(this).parent()[0].style, 'left', Tween.regularEaseInOut, pos.left, 651, 1, 'px');
				t1.start();
			} else {
				$(this).fadeOut('slow');
				var pos = $(this).parent().position();
				
				t1 = new Tween($(this).parent()[0].style, 'left', Tween.regularEaseInOut, pos.left, 860, 1, 'px');
				t1.start();
			}
		});
	});
}


/************************************************************************************************************************************
* main
************************************************************************************************************************************/
$(document).ready(function () {
	loadall();

	if (typeof $('div.catalogo')[0] != 'undefined') loadswf('catalogo');
	if (typeof $('div.social')[0] != 'undefined') loadsocial();
	if (typeof $('div.home')[0] != 'undefined') loadhome();
	if (typeof $('div.contacto')[0] != 'undefined') loadcontacto();
	if (typeof $('div.marcas')[0] != 'undefined') loadmarcas();
	if (typeof $('div.juego')[0] != 'undefined') loadjuego();

	$(document).pngFix({ blankgif:'images/blank.gif' } );
});
