var imgDir = '/img/alexmartin/';

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

var	showmenu = true;
var homeImages = Array();

var showNuImages = function(response) {
    var json = eval(response);
	var images = json.images;
	for (var i=0; i<images.length; i++) {
		homeImages[i] = {src: images[i].medium};
	}
	slideShow();
}

var homeEvents = function () {
	// carga las fotos para el slideshow
	var url = "/ajax/get_feat_image_list.php";

	var idsite = document.getElementById("id_site");
	if (idsite != null) {
		var _data = "idsite="+idsite.value+"&max=15";
		$.ajax({
				url: url, 
				type: 'get', 
				dataType: "text", 
				data: _data, 
				success: showNuImages
			}
		);
	}
}

var slideShow = function () {
	$('#imagetainer').crossSlide({
		sleep: 3,
		fade: 1
	}, homeImages);
}

var imageMobileEvents = function() {
	resizeImage();
	
	window.onorientationchange = function () {
		resizeImage();
	}
}

var resizeImage = function () {
	var vp_width = window.innerWidth;
	var img = document.getElementById("mainimg");
	if (img != null) {
		var img_width = img.width;
		img.width = parseInt(vp_width*.90);
	}
}

var showdialog = true;

var imageEvents = function() {
	// paginadores sobre la foto
	$("#imagedesc").dialog({autoOpen: false, width: 560, closeText: ''});
	
	// + info
	// $("#imagedesc").hide();
	$("#imginfo").click(
		function() {
			if (showdialog) {
				$("#imagedesc").dialog('open');
				showdialog = false;
			} else {
				$("#imagedesc").dialog('close');
				showdialog = true;
			}
		}
	);
	
	// comandos
	var show = true;
	var foto_left = $("#mainimg").offset().left;
	var foto_top = $("#mainimg").offset().top;
	var foto_w = $("#mainimg").width();
	var foto_h = $("#mainimg").height();
	
	$(".prev div").height(foto_h);
	$(".next div").height(foto_h);
	
	// previous
	var prev_top = foto_top;
	var prev_left = foto_left;
	var link_top = foto_top + foto_w/2 - 200;
	// next
	var next_top = foto_top;
	var next_left = foto_left + foto_w - 38;
	
	// muevo el div
	$(".prev").offset({top: prev_top, left: prev_left});
	$(".next").offset({top: next_top, left: next_left});
	$(".prev a").offset({top: link_top, left: prev_left});
	$(".next a").offset({top: link_top, left: next_left});
	
	$(".commands").hide();
	
	
	$("#mainimg").hover(
		function() {
			$(".commands").show();
		}, 
		function(){
			$(".commands").hide();
		}
	);
	
	$(".commands").hover(
		function() {
			$(".commands").show();
		}, 
		function(){
			$(".commands").hide();
		}
	);
	
	/*
	$("#number1 div").hide();
	
	// click
	$("#action").bind("click", function() {
		if (show) {
			$("#number1 div").show();
			show = false;
		} else {
			$("#number1 div").hide();
			show = true;
		}
	});
	
	$("#number1").hover(
		function() {
			$("#number1 div").show();
		}, 
		function(){
			$("#number1 div").hide();
		}
	);
	* */
}

var carrouselEvents = function () {
	// busca que elemento es el .current
	if (!$.browser.webkit) {
		var cur_pos = 0;
		$("#imglist img").each(function(index) {
			if (this.className == 'current' && index) {
				cur_pos = index + 1;
			}
		});
		
		$('#imglist').jcarousel({
	        vertical: true,
	        scroll: 4,
	        start: cur_pos
	    });
	} else {
		// usa el plugin en carousel.min
		// http://thomlx.free.fr/jquery/jquery_carousel.htm
		$("#imglistcontainer").carousel({
			direction: "vertical", 
			dispItems: 5
		});
	}
}