/* JS Document - jQuery */
$(document).ready(function(){
	var site_url = 'http://www.freshweekend.es/';
	//var site_url = 'http://freshweekend.pedalma.com/';
	//NAV
	var vars = document.location.toString().split(site_url).toString().replace(',','').split('/');
	if(vars==''){ vars = new Array(); vars[0] = 'es'; vars[1] = 'home'; }
	else if(!vars[1])  vars[1] = 'home';
	
	// VARIABLES
	var language = vars[0];
	//DESACTIVADO INGlÉS
	//if (language == 'en') language = 'es';
	var _to = vars[1];
	var _block = vars[2];
	$(".language[val='"+language+"']").addClass("language_selected");
	$(".language").click(function(){
		// DESACTIVADO INGLÉS
		//if(($(this).attr("val") == language) || ($(this).attr("val") == 'en')) return false;
		language = $(this).attr("val");
		parent.location = site_url+language+'/home';
	}).disableSelection();
	
	$.getJSON('/data/fresh.php', {lang: language, pag: 'cabecera'}, function(datahome){
		$.each(datahome, function(pagina, pag_value){
			$.each(pag_value, function(bloque, blo_value){
				$('#'+bloque).attr('to', blo_value.enlace).html(blo_value.title);
			});
		});
		//MENU
		if(_to != 'home' && _to != 'contacto') 
			if(_to == 'que-es-fresh' || _to == 'como-llegar' || _to == 'espacios' || _to == 'alojamiento' || _to == 'mercadillo' || _to == 'aquapark' ||
			   _to == 'que-e-fresh' || _to == 'como-chegar' || _to == 'espazos' || _to == 'aloxamento' || _to == '404' || _to == 'BETA' || _to == 'BETA2' || 
			   _to == 'what-is-fresh' || _to == 'getting-here' || _to == 'areas' || _to == 'aquatic-park' || _to == 'accommodation' || _to == 'stands'){
				$("#bar_selected").css('left', $("#fresh-weekend").position().left +10);
				$("#fresh-weekend").css('color', '#42bdea');
			}
			else{
				$("#bar_selected").css('left', $("#menu [to='"+_to+"']").position().left +10);				
				$("#menu [to='"+_to+"']").css('color', '#42bdea');
			}
		//NAV
		var params = { lang: language, pag: _to, block: _block };
		if( _to == 'noticias' || _to == 'news' || _to == 'novas' ) {
			_file = 'noticias';
		}
		else if( _to == 'programacion' || _to == 'line-up' ){
			_file = 'programacion';
		}
		else _file = 'fresh';
		
		$.getJSON('/data/'+_file+'.php', params, function(data){
			//$("#contentwhite").show(0);
			$.each(data, function(html, bloques){
				if(html=='home' || html=='programacion') {
					var animate_enlaces = Array(); var animate_images = Array(); var animate_titles = Array(); var animate_subtitles = Array(); var last_image;
					if(html=='programacion' && _block) html='programacion_detalle';
				}
				$("#content").load('/pages/'+html+'.html',function(){
					$.each(bloques, function(bloque, elementos){
						$.each(elementos, function(elemento, value){
							var elem = elemento.substring(0,elemento.length -2);
							//alert(elem);
							if(value)
								switch(elem){
									case 'enlace':			
										if (bloque=='principales'){
											var div = "#"+bloque;
											animate_enlaces[elemento.substring(elemento.length -2)] = value;
										}
										else var div = "#"+bloque+" ."+bloque+"_"+elemento.substring(elemento.length -2);
										if (bloque=='destacados' || bloque=='ordenados' || bloque=='lista'){
											if(bloque!='lista'){	
												$(div).attr('permalink', value);
												if(bloque=='destacados' && value){animate_enlaces[elemento.substring(elemento.length -2)] = value;}
											}
											value = 'internal://'+_to+'/'+value;
										}
										$(div).unbind('click').bind('click',function(){
											if(value.substring(0,4)=='http') window.open(value,'new_window');
											else parent.location = site_url+language+value.substring(10);
										}).css('cursor', 'pointer').disableSelection();
										break;
									case 'image':
									case 'imagen':											
										if (bloque=='principales'){
											var div = "#"+bloque+" ."+elem;
											animate_images[elemento.substring(elemento.length -2)] = value;
											$(div).attr('val', elemento.substring(elemento.length -2));
											last_image = elemento.substring(elemento.length -2);
											$(div).html('<img src="'+site_url+'photo/'+value+'_510_345.jpg" border="0" />');
										}
										else if(bloque=='banners'){
											var div = "#"+bloque+" ."+elem+"_"+elemento.substring(elemento.length -2);
											$(div).html('<img src="'+site_url+'photo/'+value+'_180_110.jpg" border="0" />');
										}
										else if(bloque=='entradashome'){
											var div = "#"+bloque+" ."+elem+"_"+elemento.substring(elemento.length -2);
											$(div).html('<img src="'+site_url+'photo/'+value+'.jpg" border="0" />');
											//$(div).html('<img src="/img/'+value+'.jpg" border="0" />');
										}
										else if(html=='patrocinadores'){
											var div = "#"+bloque+" ."+elem+"_"+elemento.substring(elemento.length -2);
											$(div).html('<img src="'+site_url+'photo/'+value+'.jpg" border="0" />');
										}
										else if(html=='noticias' && bloque=='detalle'){
											var div = "#"+bloque+" ."+elem+"_"+elemento.substring(elemento.length -2);
											$(div).html('<img src="'+site_url+'photo/'+value+'_240_300.jpg" border="0" />');
										}
										else if(bloque=='lista'){
											var div = "#"+bloque+" ."+elem+"_"+elemento.substring(elemento.length -2);
											$(div).html('<img src="'+site_url+'photo/'+value+'_140_300.jpg" border="0" />');
										}
										else if(bloque=='destacados'){
											var div = "#"+bloque+" ."+elem+"_"+elemento.substring(elemento.length -2);
											animate_images[elemento.substring(elemento.length -2)] = '<img src="'+site_url+'photo/'+value+'_330_340.jpg" border="0" />';
											if(elemento.substring(elemento.length -2) < '11'){
												$(div).html(animate_images[elemento.substring(elemento.length -2)]);
												last_image = elemento.substring(elemento.length -2); 
											}
										}
										else if(bloque=='ordenados'){
											var div = "#"+bloque;
											$(div).append('<div class="image '+bloque+"_"+elemento.substring(elemento.length -2)+'"><img src="'+site_url+'photo/'+value+'_60_60.jpg" border="0" /></div>');
										}
										else if(html=='programacion_detalle' && bloque=='detalle'){
											var div = "#"+bloque+" ."+elem+"_"+elemento.substring(elemento.length -2);
											$(div).html('<img src="'+site_url+'photo/'+value+'_310_300.jpg" border="0" />');
										}									
										break;
									case 'title':											
										if (bloque=='principales'){
											var div = "#"+bloque+" ."+elem;
											animate_titles[elemento.substring(elemento.length -2)] = value;
											$(div).html(value);
										}
										else if(bloque=='destacados'){
											var div = "#"+bloque+" .lista";
											animate_titles[elemento.substring(elemento.length -2)] = value;
											if($(div).text()!='') $(div).append('<font class="sep">/</font> ');
											$(div).append('<font class="title '+bloque+"_"+elemento.substring(elemento.length -2)+'">'+value+'</font> ');
										}
										else{
											var div = "#"+bloque+" ."+elem+"_"+elemento.substring(elemento.length -2);
											$(div).html(value);
										}
										break;
									case 'subtitle':											
										if (bloque=='principales'){
											var div = "#"+bloque+" ."+elem;
											animate_subtitles[elemento.substring(elemento.length -2)] = value;
										}
										else var div = "#"+bloque+" ."+elem+"_"+elemento.substring(elemento.length -2);
										$(div).html(value);
										break;
									case 'body':
									case 'main':
										var div = "#"+bloque+" ."+elem+"_"+elemento.substring(elemento.length -2);
										$(div).html(value);
										break;
									case 'video':
										var div = "#"+bloque+" ."+elem+"_"+elemento.substring(elemento.length -2);
										$(div).html('<iframe title="YouTube video" width="310" height="220" src="http://www.youtube.com/embed/'+value+'?rel=0" frameborder="0" allowfullscreen></iframe>');
										break;
								}
							//$("#contentwhite").css('height',$("#content").height());
							//alert(animate_enlaces);
						});					
					});
					if(html != 'BETA2' && html != 'que-es-fresh' && html != 'como-llegar' && html != 'espacios' && html != 'aquapark' && html != 'alojamiento' && html != 'mercadillo'){
						$("#loader").fadeOut(750);//$('#contentwhite').fadeOut(1000);
						if(html != 'contacto'){
							if(html != 'programacion_detalle')$("#nav_bar").append($("#menu [to='"+_to+"']").text());
							$("div[id$='data'] .titlebig").text($("#menu [to='"+_to+"']").text());
						}
						else {
							$("#nav_bar").append($("#contacto").text());
							$("div[id$='data'] .titlebig").text($("#contacto").text());
						}
						$(".toHome").mouseover(function(){
							$(this).css('color', "#42bdea");
						}).mouseout(function(){
							$(this).css('color', "#d0f0ff");
						}).click(function(){					
							parent.location = site_url+language+'/home';
						}).disableSelection();
					}
					else{
						$.getScript('/js/freshweekend.js');														
						$("head").append("<link>");
						var css = $("head").children(":last");
						css.attr({ rel:  "stylesheet", type: "text/css", href: "/style/freshweekend.css" });
					}
					// FOLLOW US
					$(".header_facebook").attr('href', 'http://www.facebook.com/FreshWeekend').attr('title', 'facebook official site').attr('target', '_blank');
					$(".header_megusta").html('<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.freshweekend.es&amp;layout=button_count&amp;show_faces=true&amp;width=72&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:72px; height:21px;" allowTransparency="true"></iframe>');
					$(".header_blogger").attr('href', 'http://www.freshweekend.blogspot.com/').attr('title', 'blogger').attr('target', '_blank');
					$(".header_flickr").attr('href', 'http://www.flickr.com/photos/freshweekend/').attr('title', 'flickr').attr('target', '_blank');
					$(".header_youtube").attr('href', 'http://www.youtube.com/user/FreshWeekend').attr('title', 'YouTube').attr('target', '_blank');
					$(".header_twitter").attr('href', 'http://twitter.com/FRESHWEEKEND').attr('title', 'Twitter').attr('target', '_blank');
					$(".header_tuenti").attr('href', 'http://www.tuenti.com/freshweekend').attr('title', 'Tuenti').attr('target', '_blank');
					$(".header_myspace").attr('href', 'http://www.myspace.com/freshweekend').attr('title', 'Myspace').attr('target', '_blank');
					
					$(".gmaps").click(function(){
						window.open('http://maps.google.es/maps?f=q&source=s_q&hl=es&q=AQUAPARK,+Av+MES%C3%93N,+S%2FN,+15185+Cerceda,+La+Coru%C3%B1a,+Galicia&aq=&sll=40.396764,-3.713379&sspn=15.114972,25.861816&ie=UTF8&geocode=FQnVkgId0ch-_w&split=0&hq=&hnear=AQUAPARK,+Av+MES%C3%93N,+S%2FN,+15185+Cerceda,+La+Coru%C3%B1a&ll=43.17686,-8.46827&spn=0.028291,0.050511&t=h&z=15&iwloc=A', 'new_window');
					}).disableSelection();
					
					$(".megusta").html('<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.freshweekend.es%2F'+language+'%2F'+_to+'%2F'+((_block)?_block:'')+'&amp;layout=button_count&amp;show_faces=true&amp;width=72&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:72px; height:21px;" allowTransparency="true"></iframe>');
					
					$(".facebook").attr('href', 'http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.freshweekend.es%2F'+language+'%2F'+_to+'%2F'+((_block)?_block:'')+'&t=Fresh%20weekend%20%7C%20'+escape(($("#menu [to='"+_to+"']").text())?($("#menu [to='"+_to+"']").text()):($("#submenu [to='"+_to+"']").text()))).attr('title', 'facebook').attr('target', '_blank');
					$(".tuenti").attr('href', 'http://www.tuenti.com/share/?url=http%3A%2F%2Fwww.freshweekend.es%2F'+language+'%2F'+_to+'%2F'+((_block)?_block:'')).attr('title', 'tuenti').attr('target', '_blank');
					$(".twitter").attr('href', 'http://twitter.com/home?status=http%3A%2F%2Fwww.freshweekend.es%2F'+language+'%2F'+_to+'%2F'+((_block)?_block:'')).attr('title', 'twitter').attr('target', '_blank');
					$(".myspace").attr('href', 'http://www.myspace.com/freshweekend').attr('title', 'myspace').attr('target', '_blank');
					$(".meneame").attr('href', 'http://www.meneame.net/submit.php?url=http%3A%2F%2Fwww.freshweekend.es%2F'+language+'%2F'+_to+'%2F'+((_block)?_block:'')).attr('title', 'meneame').attr('target', '_blank');
					$(".delicious").attr('href', 'http://www.del.icio.us/post?url=http%3A%2F%2Fwww.freshweekend.es%2F'+language+'%2F'+_to+'%2F'+((_block)?_block:'')+'&title=Fresh%20weekend%20%7C%20'+escape(($("#menu [to='"+_to+"']").text())?($("#menu [to='"+_to+"']").text()):($("#submenu [to='"+_to+"']").text()))).attr('title', 'del.icio.us').attr('target', '_blank');
										 
					if(html=='home'){			
						$("head").append("<link>");
						var css = $("head").children(":last");
						css.attr({ rel:  "stylesheet", type: "text/css", href: "/style/home.css" });
						setTimeout(function animate(){
							var sig = $("#principales .image").attr('val') - 1;
							if(sig <= 0) sig = last_image;//'0'+animate_images.length;
							else sig = '0'+sig;
							$("#principales").css('background', "url('"+site_url+"photo/"+animate_images[sig]+"_510_345.jpg')");
							$("#principales .image").fadeOut(2500, function(){
								$(this).html('<img src="'+site_url+'photo/'+animate_images[sig]+'_510_345.jpg" border="0" width="510" height="345" />').attr('val', sig).fadeIn(0);
							});						
							$("#principales .text").empty().animate({
									//left: '-=520px'
									top: '-=70px'
								  }, 750, function() {
									// Animation complete.
									//$(this).css('left', parseInt($(this).css('left'))+1000);
									$("#principales").unbind('click').bind('click',function(){
										parent.location = site_url+language+animate_enlaces[sig].substring(10);
									}).css('cursor', 'pointer').disableSelection();;
									$(this).animate({
											//left: '+=520px'
											top: '+=70px'
										  }, 1000, function() {
											// Animation complete.
											$("#principales .text").html('<div class="title">'+animate_titles[sig]+'</div><div class="subtitle">'+animate_subtitles[sig]+'</div>');
									});
							});
							setTimeout(function(){animate();}, 7000);
						}, 7000);
					}
					else if(html=='programacion' || html=='programacion_detalle'){									
						$("head").append("<link>");
						var css = $("head").children(":last");
						css.attr({ rel:  "stylesheet", type: "text/css", href: "/style/programacion.css" });
						if(language == 'en') $("a#horario").text('Timetables');
						$.getScript('/js/program.js', function(){			
							if(html=='programacion')
								setTimeout(function animate(){
									$("#destacados [type='anim']").mouseout();
									$(".anim").fadeOut(500, function(){								
										for(i=1;i<=10;i++){
											if(i<10) var pos = '0'+i;
											else var pos = i;
											var sig = parseInt(last_image,10) + 1;
											if(sig < 10) sig = '0'+sig;
											if(!animate_images[sig]) sig = '01';
											last_image = sig;
											$("#destacados .destacados_"+pos+"[type='anim']").unbind('click').bind('click',function(){
												parent.location = site_url+language+'/'+_to+'/'+$(this).attr('permalink');
											}).css('cursor', 'pointer').disableSelection().attr('permalink',animate_enlaces[sig]);
											$("#destacados .image_"+pos).html(animate_images[sig]);
										}
										$(this).fadeIn(500);
									});
									setTimeout(function(){animate();}, 16000);
								}, 20000);
							else {
								$("#nav_bar").append('<span>');
								$("#nav_bar").children(":last").text($("#menu [to='"+_to+"']").text())
								.mouseover(function(){
									$(this).css('color', "#42bdea");
								}).mouseout(function(){
									$(this).css('color', "#d0f0ff");
								}).click(function(){					
									parent.location = site_url+language+'/'+_to;
								}).disableSelection();								
								$("#nav_bar").append(' / '+$("#detalle .title_01").text());
							}
						});
					}
					else if(html=='noticias'){									
						$("head").append("<link>");
						var css = $("head").children(":last");
						css.attr({ rel:  "stylesheet", type: "text/css", href: "/style/noticias.css" });
						$.getScript('/js/news.js', function() {
							$("#nav_bar").append(' / '+$("#detalle .title_01").text());
						});
					}
					else if(html == 'entradas'){									
						$("head").append("<link>");
						var css = $("head").children(":last");
						css.attr({ rel:  "stylesheet", type: "text/css", href: "/style/entradas.css" });
						$("#prebloques").click(function(){
							window.open('https://www.ticktackticket.com/entradas/ver.do?origin=TTT&url=%2Fttt%2Fminternet%2Fpkg_detalle_sesion.inicio%3Fp_evento%3D36837%26p_sesion%3D1%26p_usuario%3D%26p_idioma%3DCAS%26P_ORIGEN%3D0','new_window');
						});
					}
					else{														
						$("head").append("<link>");
						var css = $("head").children(":last");
						css.attr({ rel:  "stylesheet", type: "text/css", href: "/style/"+html+".css" });
					}
				});
			});
		});
	});
	
	$("#menu .button[to]").click(function(){
		var button = $(this);
		if(button.attr('to') == _to) return false;
		var pos = button.position().left  + (button.width() / 2) + 8;
		$("#bar_selected").stop().animate({top: '22px'},0,function(){
			$(this).animate({
					opacity: '0.2',
					left: pos,
					width: '8px',
					height: '4px'
				}, 300, function() {
					$(this).animate({
							opacity: '0.4',
							width: '2px',
							height: '24px'
						  }, 200, function() {
							$(this).animate({
									opacity: '0.8',
									height: '4px',
									width: '2px',
									top: '55px'
								  }, 400, function() {
									$(this).animate({
											width: '18px',
											height: '1px',
											opacity: '0.1',
											left: '-=9px',
											top: '+=2'
										  }, 200, function() {					
											parent.location = site_url+language+'/'+button.attr("to");
									});	
							});
					});
			});
		});
	}).disableSelection();	
	$("#contacto[to]").mouseover(function(){
		$(this).css('color', "#666666");
	}).mouseout(function(){
		$(this).css('color', "#333333");
	}).click(function(){					
		parent.location = site_url+language+'/'+$(this).attr("to");
	}).disableSelection();
	
	$(".logo").click(function(){					
		parent.location = site_url+language+'/home';
	}).disableSelection();
	
});
