var actual_photo;
var cnt_photos;
var arr_photos;
var actual_news = -1;
var cnt_news;
var arr_news = new Array();

function onLoadHandler() {
	tooglePhotoPanel();
	var m = new simplemenu( "containerMenuTop", "btsMenuTop", menuTopDef, "curveStyle", forzar_html_menu );
	posicNuevos();
	var combos = ( $$( "select" ) );
	combos[0].id = "cb_categ";
	combos[1].id = "cb_equipos_top";
	if( combos[2] != undefined ) {
		combos[2].id = "cb_equipos_field";
	}
	$( "cb_categ" ).onchange = function() {
		changeCategHandler( this );
	}
	if( location.href.indexOf( "contacto" ) > -1 ) {
		selectComboEquiposContact();
	}
	$$( "div.suggestPanel" ).each( function( s, index ) {
		toogleSuggestPanel( s.id.substring( 13, s.id.length ) );
	} );
	resizeBlockLayer();
	Event.observe( window, "resize", function() { resizeBlockLayer(); } );
	loadNews();
}

function selectComboEquiposContact() {
	var qs = location.href.toQueryParams();
	$( "cb_equipos_field" ).childElements().each( function( s, index ) {
		if( s.value == qs.equip ) {
			s.selected = "selected";
			throw $break;
		}
	} );
}

function resizeBlockLayer() {
	var body_w = $( document.documentElement ).getFullWidth();
	var body_h = 10000;
	$( "blockLayer" ).setStyle( { width:body_w+'px', height:body_h+'px' } );
}

function tooglePhotoPanel() {
	$( "photoPanel" ).toggle();
	$( "blockLayer" ).toggle();
}

function toogleSuggestPanel( id_equip ) {
	$( "suggestPanel_"+id_equip ).toggle();
	$( "suggest_span_"+id_equip ).textContent = "";
}

function movePhoto( dir ) {
	actual_photo = actual_photo+dir;
	var src = arr_photos[actual_photo-1];
	if( actual_photo == cnt_photos || cnt_photos == 0 ) {
		$( "btGaleriaRight" ).disabled = true;
	} else {
		$( "btGaleriaRight" ).disabled = false;
	}
	if( actual_photo == 1 ) {
		$( "btGaleriaLeft" ).disabled = true;
	} else {
		$( "btGaleriaLeft" ).disabled = false;
	}
	$( "imgSrc" ).src = "images/image-galery.php?src="+src;
	$( "nPhoto" ).textContent = ( actual_photo+"/"+cnt_photos );
}

function moveNews( dir ) {
	actual_news = ( actual_news+dir );
	var show = ( actual_news%cnt_news );
	$( "loaderNews" ).src = "";
	$( "counterNews" ).textContent = ( ( show+1 )+"/"+cnt_news );
	$( "newsP" ).textContent = arr_news[show];
}

function loadNews() {
	var lang = global_lang;
	var url ="php/functions/get-news.php";
	var pb = 'lang='+global_lang;
	new Ajax.Request( url, {
		method: 'post',
		postBody:pb,		
		onFailure:function() {
			alert( arrNotif[lang][1002] );
		},
		onSuccess:function( transport ) {
			eval( transport.responseText );
			cnt_news = arr_news.length;
			if( cnt_news>0 ) {
				$( "btNewsLeft" ).disabled = false;
				$( "btNewsRight" ).disabled = false;
			} else {
				cnt_news = 1;
				arr_news[0] = arrNotif[lang][1010];
			}
			moveNews( 1 );
		}
	} );	
}

function loadImages( id_equip ) {
	$( "btGaleriaLeft" ).disabled = true;
	$( "btGaleriaRight" ).disabled = true;
	actual_photo = 0;
	arr_photos = new Array();
	tooglePhotoPanel();
	var url ="php/functions/get-photos.php";
	var pb = 'id='+id_equip;
	new Ajax.Request( url, {
		method: 'post',
		postBody:pb,		
		onFailure:function() {
			alert( arrNotif[lang][1002] );
			tooglePhotoPanel();
		},
		onSuccess:function( transport ) {
			eval( transport.responseText );
			cnt_photos = arr_photos.length;
			movePhoto( 1 );
		}
	} );	
}

function submitSuggest( id_equip ) {
	var lang = global_lang;
	if( !isMail( $( "suggest_field_"+id_equip ).value ) ) {
		$( "suggest_span_"+id_equip ).textContent = arrNotif[ lang ][1005];
		return false;
	}
	$( "suggest_span_"+id_equip ).textContent = arrNotif[ lang ][1006];
	var url = './php/functions/suggest.php';
	var pb = "email="+$( "suggest_field_"+id_equip ).value+"&id="+id_equip;
	new Ajax.Request( url, {
		method:'post',
		postBody:pb,
		onFailure:function() {
			$( "suggest_span_"+id_equip ).textContent = arrNotif[ lang ][1002];
			return false;
		},
		onSuccess:function( transport ) {
			$( "suggest_span_"+id_equip ).textContent = arrNotif[ lang ][1008];
			return true;
		}
	} );
}

function posicNuevos() {
	$$( "div.nuevo" ).each( function( s, index ) {
		var k = s.ancestors()[0].positionedOffset();
		s.setStyle( { top:( k.top - 5 )+"px", left:( k.left - 30 )+"px" } );
	} );
}

function changeCategHandler( cb ) {
	$( "cb_equipos_top" ).childElements().each( function( s, index ) {
		s.remove();
	} );
	equipos_array[cb.value].each( function( s, index ) {
		var opt = equipos_array[cb.value][index];
		$( "cb_equipos_top" ).insert( new Option( opt.label, opt.value ) );
	} );
}

function goEquipo( cb ) {
	if( cb.id == "cb_equipos_top" ) {
		location.href = cb.value;
	}
}

function submitSearch() {
	var lang = global_lang;
	if( $( "search_field" ).value.length < 4 ) {
		alert( arrNotif[ lang ][1009] );
		return false;
	}
	location.href = ( $( "f_search" ).action+"?search="+$( "search_field" ).value+"&" );
}

function searchOnWeb() {
	var url = './php/functions/search-in-web.php';
	var pb = location.href.split("?")[1];
	new Ajax.Request( url, {
		method:'post',
		postBody:pb,
		onFailure:function() {
			$( "results" ).setStyle( { visibility:'hidden' } );
			$( "noresults" ).setStyle( { visibility:'visible' } );
			return false;
		},
		onSuccess:function( transport ) {
			var html = "";
			var found = new Array();
			eval( transport.responseText );
			if( found.length < 1 ) {
				$( "noresults" ).setStyle( { visibility:'visible' } );
			}
			for( var i=0; i<found.length; i++ ) {
				html += ( '<p class="google"><a class="linkTitGoogle" href="'+found[i].weblink+'">'+found[i].title+'</a><br />'+found[i].text+'</p>' );
			}
			$( "results" ).innerHTML = html;
			return true;
		}
	} );
}

function submitMailing() {
	var lang = global_lang;
	var present = $( "email_mailing" ).present();
	var valid = isMail( $( "email_mailing" ).value );
	var form = $( "f_mailing" );
	var notif = $( "noticeMailing" );
	removeAllClasses( notif );
	if( present && valid ) {
		var url = './php/functions/mailing-subs.php';
		var pb = "email="+$( "email_mailing" ).value+"&lang"+lang+"&";
		notif.textContent = arrNotif[ lang ][1006];
		notif.addClassName( "notifCaution" );
		new Ajax.Request( url, {
			method:'post',
			postBody:pb,
			onFailure:function() {
				removeAllClasses( notif );
				notif.textContent = arrNotif[ lang ][1002];
				notif.addClassName( 'notifAlert' );
				return false;
			},
			onSuccess:function( transport ) {
				removeAllClasses( notif );
				notif.textContent = arrNotif[ lang ][transport.responseText];
				notif.addClassName( 'notifOk' );
				return true;
			}
		} );
	} else {
		removeAllClasses( notif );
		notif.textContent = arrNotif[ lang ][1005];
		notif.addClassName( 'notifAlert' );
	}
}

function submitContact() {
	var lang = global_lang;
	var present = $( "nombre_field" ).present() && $( "telefono_field" ).present() && $( "pais_field" ).present() && $( "comentario_field" ).present();
	var valid = isMail( $( "email_field" ).value );
	var form = $( "f_contact" );
	var notif = $( "notif_contact" );
	removeAllClasses( notif );
	if( !present ) {
		removeAllClasses( notif );
		var alertString = "";
		$$( "td.tdlabelobl" ).each( function( s, index ) {
			alertString += ( s.textContent + ", " );
		} );
		alertString += arrNotif[ lang ][1001];
		notif.textContent = alertString;
		notif.addClassName( 'notifAlert' );

	} else if( !valid ) {
		removeAllClasses( notif );
		notif.textContent = arrNotif[ lang ][1005];
		notif.addClassName( 'notifAlert' );
	} else {
		var box = $( "cb_equipos_field" );
		var equipo_text = ( box.selectedIndex >= 0 ) ? box.options[box.selectedIndex].innerHTML : "Todos";
		var url = "./php/functions/contact.php";
		var pb = form.serialize()+"&equip="+equipo_text;
		notif.textContent = arrNotif[ lang ][1006];
		notif.addClassName( "notifCaution" );
		new Ajax.Request( url, {
			method:'post',
			postBody:pb,
			onFailure:function() {
				removeAllClasses( notif );
				notif.textContent = arrNotif[ lang ][1002];
				notif.addClassName( 'notifAlert' );
				return false;
			},
			onSuccess:function( transport ) {
				removeAllClasses( notif );
				notif.textContent = arrNotif[ lang ][transport.responseText];
				notif.addClassName( 'notifOk' );
				return true;
			}
		} );
	}
}

function isMail( _email ) {
	var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,3}$/i;
	return emailReg.test( _email );
}

function removeAllClasses( domobj ) {
	domobj.classNames().each( function( s, index ) {
		domobj.removeClassName( s );
	} );
}

function changeLang( lang ) {
	location.href = ( String( location.href ).replace( new RegExp( global_lang+".html", 'g' ), lang+".html" ) );
}

document.observe( "dom:loaded", function() {
	onLoadHandler();
} );

/* ******************************** */
var arrNotif = new Array();

arrNotif["es"] = new Array();
arrNotif["en"] = new Array();

arrNotif["es"][1001] = "son obligatorios";
arrNotif["en"][1001] = "are mandatories";

arrNotif["es"][1002] = "Error del servidor!";
arrNotif["en"][1002] = "Server error!";

arrNotif["es"][1003] = "Suscripto!";
arrNotif["en"][1003] = "Subscribed!";

arrNotif["es"][1004] = "Ud ya estaba suscripto";
arrNotif["en"][1004] = "You were subscribed already";

arrNotif["es"][1005] = "Indique un email válido";
arrNotif["en"][1005] = "You must fill with a valid email";

arrNotif["es"][1006] = "Enviando...";
arrNotif["en"][1006] = "Sending...";

arrNotif["es"][1007] = "Correo enviado correctamente";
arrNotif["en"][1007] = "eMail sent successfully";

arrNotif["es"][1008] = "Gracias!";
arrNotif["en"][1008] = "Thank you!";

arrNotif["es"][1009] = "Debe introducir al menos 4 caracteres";
arrNotif["en"][1009] = "You must enter 4 chars at least";

arrNotif["es"][1010] = "Sin novedades!";
arrNotif["en"][1010] = "Without news!";

