$(function(){
	/* GENERAL */
	ajustar();
	$("a").each(function() {
		$(this).attr("hideFocus", "true").css("outline", "none");
	});
	if($('#btn_buscar').length > 0){
		$('#btn_buscar').val('');
	}
	/* FIN GENERAL */
		
	/* MENU PRINCIPAL */
	$('.header li.facebook, .header li.twitter').mouseenter(function(){
		$('div.redes_desplegable',this).css('display','inline');
		$('a.opener',this).addClass('activo');
	});
	$('.header li.facebook, .header li.twitter').mouseleave(function(){
		$('div.redes_desplegable',this).css('display','none');
		$('a.opener',this).removeClass('activo');
	});	
	$('li.open').mouseenter(function(){
		$('ul.subseccion',this).css('display','inline');
		$('a.open_submenu',this).addClass('activo');
	});
	$('li.open').mouseleave(function(){
		$('ul.subseccion',this).css('display','none');
		$('a.open_submenu',this).removeClass('activo');
	});
	/* FIN MENU PRINCIPAL */
	
	/* NOTICIAS */
	if($('#slideshow').length > 0){
		$('#slideshow').cycle({ 
			fx:     'fade', 
			speed:   'fast', 
			pager:  '#nav_pagination',
			timeout: 0
		});
	}
	/* FIN NOTICIAS */
	
	/* HOME */
	if($('#inicio #carousel div.video').length > 0){
		$("a[rel^='prettyPhoto']").prettyPhoto({
			theme: 'facebook',
			show_title: false,
			modal: true,
			default_width: 840,
			default_height: 500
		});
	}
	/* FIN HOME */
	
	/* CONTACTO */
	if( $('div.form_msg').length > 0){
		$('div.form_msg').slideDown(700,function(){								 
			$.sleep(5,function(){
				$('div.form_msg').slideUp(700);
			});
		});
	}
	
	if ($('form#formulario_edad').length > 0){
		$('#dia').numeric();
		$('#mes').numeric();
		$('#anio').numeric();
	}
	
	if($('#map_canvas').length > 0){
		$coordenadas = $('#map_canvas').html();
		$coordenadas = $coordenadas.split(","); 
		if ( $.browser.msie ) {
			$('#map_canvas').googleMaps({
				latitude: 	$coordenadas[0],
				longitude: $coordenadas[1],
				depth: parseInt($coordenadas[2]),
				markers: {
					latitude: 	$coordenadas[0],
					longitude: $coordenadas[1]
				}	
			});
		} else {
			$('#map_canvas').googleMaps({
				latitude: 	$coordenadas[0],
				longitude: $coordenadas[1],
				depth: parseInt($coordenadas[2]),
				markers: {
					latitude: 	$coordenadas[0],
					longitude: $coordenadas[1],
					icon: { 
						image: base_url + 'images/punto_x.png', 
						iconSize: '12, 20'
					}
				}	
			});
		}
	}
	/* FIN CONTACTO */
	
	/* CARRUSELES */
	if($("#carousel").length > 0){
		$create = true;
		$("#carousel").featureCarousel({
			largeFeatureWidth: 1,
			largeFeatureHeight: 1,
			carouselSpeed: 300,
			autoPlay: 0,
			movedToCenter: function($feature) {
				if($create){centrar_galeria();}
				$create = false;
				$link = $feature.find('a').attr('href');
				$feature.addClass('centrado');				
				$marcar = ($feature.index('.carousel-feature')+1);
				$('li.pag_carrusel').each(function(){
					$(this).css('backgroundPosition','0 0');	
				});
				$('#item_'+$marcar).css('backgroundPosition','-10px 0');
			},
			leavingCenter: function($feature) {
				$feature.removeClass('centrado');
			}
		});
		$cont = 1;
	}
	/* FIN CARRUSELES */
	
	/* PRODUCTOS */
	if($('#dock2').length > 0){
		$('#dock2').Fisheye({
			maxWidth: 45,
			items: 'li',
			itemsText: 'span',
			container: '.dock-container',
			itemWidth: 130,
			proximity: 140,
			alignment: 'center',
			valign: 'top',
			halign : 'center'
		});
	}
	
	if($('a#player_video').length > 0){	
		$("a#player_video").youtubin({
			swfWidth 	: '790',
			swfHeight 	: '454',
			autoplay	: true
		});
	}
	
	$('#productos li.item a').mouseover(function(){ $('img',this).css('display','inline'); });
	$('#productos li.item a').mouseout(function(){ $('img',this).css('display','none'); });
	
	if($('a.preview').length > 0){
		inicia_lightbox($('a.preview'));
		$('ul.lista_fotos a.show').click();
	}
	
	$('li.pag_oculta a').click(function(){ return false; });
	/* FIN PRODUCTOS */
	
	/* eventos */
	$('.detalle_fiesta .lista_fotos li.item').mouseenter(function(){
		$('.redes', this).slideDown(100);
		$link = $('.twitter a', this);
		if($link.attr('class') == ''){
			$link.addClass('loading');
			recortar($link);	
		}
	});
	
	$('.detalle_fiesta .lista_fotos li.item').mouseleave(function(){
		$('.redes', this).slideUp(100);
	});	
	
	var _sleeptimer;
	$.sleep = function( time2sleep, callback ){
		$.sleep._sleeptimer = time2sleep;
		$.sleep._cback = callback;
		$.sleep.timer = setInterval('$.sleep.count()', 1000);
	}
	$.extend ($.sleep, {
		current_i : 1,
		_sleeptimer : 0,
		_cback : null,
		timer : null,
		count : function(){
			if ( $.sleep.current_i === $.sleep._sleeptimer ){
				clearInterval($.sleep.timer);
				$.sleep._cback.call(this);
			}
			$.sleep.current_i++;
		}
	});
	/* FIN eventos */
});
function centrar_galeria(){	
	$obj = $('#carousel .tracker-individual-container');
	if($obj.length > 0){
		$obj.find('li').each(function(){
			$numero = $('div',this).html();
			$(this).replaceWith('<li id="item_'+$cont+'" class="tracker-individual-blip pag_carrusel">'+$numero+'</li>');
			$cont++;
		});
	}
	$width = $('#carousel').width()/2;
	$menu = $obj.width()/2;
	$left = $width-$menu;
	$obj.addClass('importantRule');
	$obj.css('left',$left);
}
window.onresize = function() {
	ajustar();
}
function ajustar(){
	$ancho = $(window).width();
	$alto = $(window).height();
	$('#fondo_web').height($alto);
	$('#fondo_web').width($ancho);
}
function inicia_lightbox($obj){
	if($('#link_lightbox').length > 0){
		$path = $('#link_lightbox').val();
		$facebook = 'http://www.facebook.com/sharer.php?u=';
		$twitter = 'http://twitter.com/share?url={ruta}&text=@ronlegendario';
		$path_fotos = $('#evento_name').val();
	} else {
		$path = $facebook = $twitter = $path_fotos = "";
	}
	$obj.lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.6,
		imageLoading: 	base_url + 'images/lightbox/loading.gif',
		imageBtnClose: 	base_url + 'images/lightbox/close.gif',
		imageBtnPrev: 	base_url + 'images/lightbox/prev.gif',
		imageBtnNext: 	base_url + 'images/lightbox/next.gif',
		containerResizeSpeed: 350,
		txtImage: 'Imagen',
		path: $path,
		facebook: $facebook,
		twitter: $twitter,
		path_foto: $path_fotos,
		txtOf: 'de'
	});	
}
function recortar($obj){
	$link = $obj.attr('href');
	$twitter_share = 'http://twitter.com/home?status=@ronlegendario: ';
	$.ajax({
		type: "POST",
		async:true,
		url: base_url + "twitter.php",
		data: "url=" + $link,
		success: function(datos){
			$obj.removeClass('loading');	
			$obj.addClass('created');		
			$obj.attr("href", $twitter_share + datos );
		} 
	});	
}
function oculta($objeto){
	if ($objeto.attr('title') == $objeto.val()){
		$objeto.val('');
	}	
}
function video($video){
	FlashTransparente("player_video","reproduce_video",'790','445',base_url+'swf/mediaplayer.swf','stretching=fill&autostart=true&file=' + $video);		   
}
function FlashTransparente(DivID,ObjectID,WIDTH,HEIGHT,MOVIE,VALORES){
	document.getElementById(DivID).innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" id="' + ObjectID + '" width="' + WIDTH + '" height="' + HEIGHT +'"><param name="movie" value="' + MOVIE +'"><param name="quality" value="high"><PARAM NAME="wmode" VALUE="transparent"><param name="FlashVars" value="' + VALORES +'"><embed src="' + MOVIE +'" quality="high" wmode="transparent" FlashVars="' + VALORES +'" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="' + WIDTH + '" height="' + HEIGHT +'"></embed></object>';
}
