/* Marc Boronat                                                               */
/* Allways set up the imageboard. You can use body onload.                    */
/* Example: imageboard_set('element_id', 'image_height_px');                  */

var tag_name = "a";
var position = 0;
var velocity = 50;
var friction = 10;
var moving = false;
var fail_dist;

function imageboard_set(element_id, img_height, store) {

    var the_element = document.getElementById(element_id);
    if (the_element != null) {
        the_element.style.overflow = "hidden";
        the_element.style.position = "relative";
        the_element.style.visibility = "hidden";

        var images = the_element.getElementsByTagName(tag_name);
        var left_init = 0;
        var top_init = 0;
        var the_img;

        for (i=0;i<images.length;i++){
            the_img = images.item(i);
            //if (!store) {the_img.removeAttribute("href");}
            the_img.style.position = "absolute";
            the_img.style.height = img_height;
            the_img.style.width = "auto";
            the_img.style.left = left_init+"px";
            the_img.style.top = top_init+"px";
            left_init += the_img.offsetWidth;
            left_init = Math.floor(left_init);
        }

        toggle_visibility(the_element);
    }
    
}

function imageboard_scroll(element_id, direction, distance) {

    var the_element = document.getElementById(element_id);
    var first_img = the_element.getElementsByTagName(tag_name).item(0);
    var last_img = the_element.lastChild;

    if (moving != true) {
        switch(direction){
            case "L":
                if(first_img.offsetLeft < 0){
                    if(first_img.offsetLeft+distance < 0) {
                        moving = true;
                        slide_x_int = window.setInterval("slide_x("+distance+",'"+element_id+"')",1);
                    } else {
                        moving = true;
                        fail_dist = first_img.offsetLeft;
                        if (fail_dist < 0) {fail_dist = -fail_dist};
                        slide_x_int = window.setInterval("slide_x("+fail_dist+",'"+element_id+"')",1);
                    }
                }
            break;

            case "R":
                if((last_img.offsetLeft+last_img.offsetWidth) > document.body.offsetWidth){
                    if((last_img.offsetLeft+last_img.offsetWidth-distance) > document.body.offsetWidth) {
                        moving = true;
                        slide_x_int = window.setInterval("slide_x(-"+distance+",'"+element_id+"')",1);
                    } else {
                        moving = true;
                        fail_dist = document.body.offsetWidth - (last_img.offsetLeft+last_img.offsetWidth);
                        slide_x_int = window.setInterval("slide_x("+fail_dist+",'"+element_id+"')",1);
                    }
                }
            break;
        }
    }

}



function slide_x(distance, element_id) {

    var the_element = document.getElementById(element_id);
    var childs = the_element.getElementsByTagName(tag_name);

    if (distance<0) {
        if (position <= distance) {
            clearInterval(slide_x_int);
            moving = false;
            position = 0;
            velocity = 50;
        } else {
            for (i=0;i<childs.length;i++) {
                the_img = childs.item(i);
                the_img.style.left = (the_img.offsetLeft-(velocity))+"px";
            }
            position -= velocity;
            velocity -= Math.ceil(velocity/friction);
            if (velocity < 1) {
                clearInterval(slide_x_int);
                moving = false;
                position = 0;
                velocity = 50;
            }
        }
    }

    if (distance>0) {
        if (position >= distance) {
            clearInterval(slide_x_int);
            moving = false;
            position = 0;
            velocity = 50;
        } else {
            for (i=0;i<childs.length;i++) {
                the_img = childs.item(i);
                the_img.style.left = (the_img.offsetLeft+(velocity))+"px";
            }
            position += velocity;
            velocity -= Math.ceil(velocity/friction);
            if (velocity < 1) {
                clearInterval(slide_x_int);
                moving = false;
                position = 0;
                velocity = 50;
            }
        }
    }
    
}




/* Toggle Visibility                                                          */

var toggle_lmnt;

function showimg(url) {
    var popimg = document.createElement("div");
    popimg.setAttribute('class', 'popimg');
    popimg.setAttribute('className','popimg');
    popimg.style.visibility = "hidden";

    var theimage = document.createElement("img");
    theimage.setAttribute('src', url);
    theimage.setAttribute('class', 'theimage');
    theimage.setAttribute('className','theimage');

    var close = document.createElement("img");
    close.setAttribute('onclick', 'toggle_visibility(this.parentNode)');
    close.setAttribute('src', '../media/web_stock/close.png');
    close.setAttribute('class', 'close');
    theimage.setAttribute('className','close');
    close.style.cursor = "pointer";

    popimg.appendChild(theimage);
    popimg.appendChild(close);

    document.documentElement.appendChild(popimg);
    toggle_visibility(popimg);
}

function toggle_visibility(lmnt) {
    toggle_lmnt = lmnt;
    if(lmnt.style.visibility == "hidden"){
        lmnt.style.visibility = "visible";
        lmnt.style.opacity = 0.1;
        lmnt.style.filter = "alpha(opacity=1)";
        appear_int = window.setInterval("appear(toggle_lmnt);", 10);
    } else if (lmnt.style.visibility == "visible"){
        lmnt.style.opacity = 1.0;
        lmnt.style.filter = "alpha(opacity=100)";
        appear_int = window.setInterval("disappear(toggle_lmnt);", 10);
    }
}

function appear(lmnt) {
    if(lmnt.style.opacity >= 1){
        clearInterval(appear_int);
    } else {
        lmnt.style.opacity *= 1.1;
        lmnt.style.filter = "alpha(opacity="+(lmnt.style.opacity*100)+")";
    }
}

function disappear(lmnt) {
    if(lmnt.style.opacity <= 0.1){
        clearInterval(appear_int);
        document.documentElement.removeChild(lmnt);
    } else {
        lmnt.style.opacity -= 0.1;
        lmnt.style.filter = "alpha(opacity="+(lmnt.style.opacity*100)+")";
    }
}




$(document).ready(function() {
	$('#signup').submit(function() {
		// update user interface
		
		$('#response_es').html('A&ntilde;adiendo direcci&oacute;n email...');
		
		// Prepare query string and send AJAX request
		$.ajax_es({
			url: 'inc/store-address.php',
			data: 'ajax=true&email=' + escape($('#email').val()),
			success: function(msg) {
				$('#response_es').html(msg);
			}
		});
		
		
		$('#response_en').html('Adding email address...');
		
		$.ajax_en({
			url: 'inc/store-address.php',
			data: 'ajax=true&email=' + escape($('#email').val()),
			success: function(msg) {
				$('#response_en').html(msg);
			}
		});		
	
		return false;
	});
});
