// JavaScript Document
var repeat = 1;
var count = 0;
var total = 0;
var id = 1;

function mover() {
	
	if(repeat < count) {		
		$('#actAgenda').animate({top:"-=140px"}, 1000);		
		repeat++;
	} else {
		$('#actAgenda').animate({top: 0}, 700);
		repeat = 1;
	}
}

function slideShow() {
	
	if(id <= (total - 1)) {
		
		cont = slideshow[id];

		$('#slideShow .imgHidden a img').attr('src',cont.img).load(function() {
			
			var html = $(this).parent();			
			
			$('#slideShow .imgShow').fadeOut('slow', function() {
				
				html.parent().fadeIn(700);
				html.attr('href', cont.links);
				
				$(this).attr('class','imgHidden');
				
				html.parent().attr('class','imgShow');
				
				$('#textSlide').html('<h2>'+cont.title+'</h2><p>'+cont.text+'</p>').css({top: "-80px"});
				
				$('#bSlide li a').each(function(i, val) {
				
					if($(this).attr('href') == (id - 1)) {

						$(this).attr('class', 'select');			
					} else {
						
						$(this).attr('class', '');
					}
				
				});
				
				
			});		
		});
		id++;
		
	} else {
		id = 0;
	}
}

$(function() {
	$("#buscar input[name=d]").datepicker({dateFormat:'yy-mm-dd'});

	count = $('#actAgenda li').length;	
	count = Math.ceil(count / 4);
	
	setInterval('mover()', 6000);
	
	var label = 1;
	
	$.each(slideshow, function(i, val) {
		
		var clase = '';
		if(i == 0) {
			clase = 'select';
		}
		
		$('#bSlide').append('<li><a href="'+i+'" class="'+clase+'">'+label+'</a></li>');
		label++;
	});
	
	total = $('#bSlide li a').length;
	
	setInterval('slideShow()', 7000);
	
	$('#bSlide li a').click(function() {
		
		id = $(this).attr('href');
		slideShow();		
		return false;
	});
	
	$('a[rel=lightbox]').lightBox();
	
/* Cambio de imagenes en moda y muro */
	
	$('.thmb_fashion a').click(function() {
		
		var img = $(this).children('img').attr('src');
		img = img.split("mini/");
		img = img[1];
		
		var img2 = $('img.float').attr('src');
		img2 = img2.split("original/");
		img2 = img2[0] + "original/" + img;
		$('img.float').attr('src', img2);
		
		return false;
	
	});
	
	$('#buscar select[name=t]').change(function() {

		if($(this).val() == 1) {
			$('.inputsearch input#names').css({display:'none'});
			$('.inputsearch input#dates').css({display:'block'});			
		} else {
			$('.inputsearch input#dates').css({display:'none'});
			$('.inputsearch input#names').css({display:'block'});	
		}
	});
	
	$("li.compartir a").click(function() {
		window.open($(this).attr('href'),"Compartir en facebook","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=800, height=450, top=85, left=140");
		return false;
	});
	
	//cache selector
	var images = $("#genteflow a img"),
	  	title = 'Flowmag.com',
		imgId = null;
	//make images draggable
	images.draggable({
		//create draggable helper
	  	helper: function() {
			return $("<div>").attr("id", "helper").html("<span>" + title + "</span><img id='thumb' src='" + $(this).attr("src") + "'>").appendTo("body");
	  	},
	  	cursor: "pointer",
	  	cursorAt: { left: -10, top: 20 },
	  	zIndex: 99999,
	  	//show overlay and targets
	  	start: function() {
			$("<div>").attr("id", "overlay").css({opacity:0.7,height:$(document).height()}).appendTo("body");
			$("#tip").remove();
			$(this).unbind("mouseenter");
			$("#targets").css("left", ($("body").width() / 2) - $("#targets").width() / 2).slideDown();
			imgUrl = $(this).parent("a").attr("rel");
	  	},
	  	//remove targets and overlay
	  	stop: function() {
			$("#targets").slideUp();
			$(".share", "#targets").remove();
			$("#overlay").remove();
			$(this).bind("mouseenter", createTip);
	  	}
	});
		
	//make targets droppable
	$("#targets li").droppable({
		tolerance: "pointer",
		//show info when over target
		over: function() {
			$(".share", "#targets").remove();
		    $("<span>").addClass("share").text("Compartir en " + $(this).attr("id")).addClass("active").appendTo($(this)).fadeIn();
		 },
		 drop: function() {
		 	var id = $(this).attr("id"),
			currentUrl = imgUrl,//window.location.href,
			baseUrl = $(this).find("a").attr("href");

			if (id.indexOf("twitter") != -1) {
				window.location.href = baseUrl + "/home?status=" + title + ": " + currentUrl;
			} else if (id.indexOf("delicious") != -1) {
				window.location.href = baseUrl + "/save?url=" + currentUrl + "&title=" + title;
			} else if (id.indexOf("facebook") != -1) {
				window.location.href = baseUrl + "/sharer.php?u=" + currentUrl + "&t=" + title;
			}
		  }		  
	});
	  
	var createTip = function(e) {
		//create tool tip if it doesn't exist
		($("#tip").length === 0) ? $("<div>").html("<span>Arrastra esta imagen para compartir<\/span><span class='arrow'><\/span>").attr("id", "tip").css({ left:e.pageX + 30, top:e.pageY - 16 }).appendTo("body").fadeIn(2000) : null;
	};
		
	images.bind("mouseenter", createTip);
		
	images.mousemove(function(e) {
		
		//move tooltip
        $("#tip").css({ left:e.pageX + 30, top:e.pageY - 16 });
  	});
	  
	images.mouseleave(function() {
		
		//remove tooltip
		$("#tip").remove();
	});

});

function shareF(id) {
	window.open("http://www.facebook.com/share.php?u=http://www.flowmag.com/new/videos.php?id="+id);
	return false;
}