// WHO WE ARE LOVIN INIT SETS'
var animate_in_process = false;
var autoanimate_direction = false;
var liWidth = false;
var VisibleAreaWidth = false;
var visibleLis = false

jQuery(document).ready(function($) {
    
    jQuery('a.new-window').click(function(){
	window.open(this.href);
	return false;
    });
    
    jQuery('#menu-main').supersubs({
        minWidth: 		13,						// requires em unit.
        maxWidth: 		25,						// requires em unit.
        extraWidth: 	1						// extra width can ensure lines don't sometimes turn over due to slight browser differences in how they round-off values
    }).superfish({
	hoverClass:		'nav-hover',			// the class applied to hovered list items 
    //	pathClass:		'overideThisToUse',		// the class you have applied to list items that lead to the current page 
	pathLevels:		1,						// the number of levels of submenus that remain open or are restored using pathClass 
	delay:			500,					// the delay in milliseconds that the mouse can remain outside a submenu without it closing 
	animation:		{opacity:'show'},		// an object equivalent to first parameter of jQuery’s .animate() method 
	speed:			'normal',				// speed of the animation. Equivalent to second parameter of jQuery’s .animate() method 
	autoArrows:		false,					// if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance 
	disableHI:		true					// set to true to disable hoverIntent detection 
    //	onInit:			function(){},			// callback function fires once Superfish is initialised – 'this' is the containing ul 
    //	onBeforeShow:	function(){},			// callback function fires just before reveal animation begins – 'this' is the ul about to open 
    //	onShow:			function(){},			// callback function fires once reveal animation completed – 'this' is the opened ul 
    //	onHide:			function(){}			// callback function fires after a sub-menu has closed – 'this' is the ul that just closed 
	});
    
    if(jQuery('.profiles_home').size()){
	liWidth = 200;
	VisibleAreaWidth = 200;
	visibleLis = 1;
	init_profiles();
    }
    if(jQuery('.profiles_footer').size()){
	liWidth = 170;
	VisibleAreaWidth = 850;
	visibleLis = 5;
	autoanimate_direction = 'fwd';
	init_profiles();
	setInterval("autoplay()",5000);
    }
    
});



var init_profiles = function() {
    var $ = jQuery;
    var original_count = jQuery(".prof_li").length;
    var pre_copy = $(".prof_li").clone();
    var pos_copy = $(".prof_li").clone();
    $(".profiles_ul").append(pre_copy).prepend(pos_copy).css("width",((original_count * 3)*liWidth) + "px").css("left","-" + (original_count*liWidth) + "px");
    $('.pcontrol_next').click(function() {
	af_next();
	return false;
    });
    $('.pcontrol_prev').click(function() {
	af_prev();
	return false;
    });
}

var af_next = function() {
    if(animate_in_process) { return false; }
    animate_in_process = true;
    autoanimate_direction = 'fwd';
    jQuery(".profiles_ul").animate({
	left: '+='+liWidth
    }, 1000, function() {
	var the_offset = jQuery(this).css("left").replace("px","") * 1;
	if(the_offset==0) {
	    jQuery(this).css("left","-" + ((jQuery(".prof_li").length/3) * liWidth) + "px");
	}
	animate_in_process = false;
    });
}


var af_prev = function() {
    if(animate_in_process) { return false; }
    animate_in_process = true;
    autoanimate_direction = 'rev';
    jQuery(".profiles_ul").animate({
	left: '-='+liWidth
    },1000, function() {
	var the_offset = jQuery(this).css("left").replace("px","") * 1;
	var max_offset = -(jQuery(this).css("width").replace("px","") * 1) + VisibleAreaWidth; // 200 ancho del area visible
	if(the_offset==max_offset) {
	    jQuery(this).css("left","-" + (((jQuery(".prof_li").length/3) * liWidth) + (((jQuery(".prof_li").length/3) - visibleLis) *liWidth )  ) + "px");
	}
	animate_in_process = false;
    });
}

var autoplay = function() {
    switch(autoanimate_direction) {
	case 'fwd':
	    af_next();
	    break;
	case 'rev':
	    af_prev();
	    break;
    }
}
