var menuActivo = 0;

var pages = 6;
var inipage = 0;
var rows = 1;
var columns = 6;
var pageWidth = 880; //px
var pageHeight = 480; //px
var separation = 0; //px
var margin = 0; //px
var visibleNextZone = 0;
var background = "images/fondoBig.jpg";

/* It'll be filled on page load */
var windowsTotalWidth;
var windowsTotalHeight;


function atacthEvents() {
    for (i = 1; i <= pages; i++) {
        $('#menu' + i).unbind('mouseenter');
        $('#menu' + i).unbind('mouseleave');
        if (i != menuActivo) {
            $('#menu' + i).bind('mouseenter', mouseEnterEffect);
            $('#menu' + i).bind('mouseleave', mouseLeaveEffect);
            $(".menu-activo", $('#menu' + i)).fadeOut(100);
            $(".menu-inactivo", $('#menu' + i)).fadeIn(100);
        }
    }
}

$('.logo').bind('click', { page: 0 }, function(event) {
        if (menuActivo != event.data.page) {
            menuActivo = event.data.page;
            atacthEvents();
            $('.contenidoMenuIn').scrollTo($('#page' + event.data.page), 800, { offset: -(visibleNextZone / 2) });
            event.preventDefault();
        }
    });

function atacthEventsSEO() {
	
		if(menuActivo == 0)
		{
			for (i = 1; i <= pages; i++) {
	        $('#menu' + i).unbind('mouseenter');
	        $('#menu' + i).unbind('mouseleave');
	        
          $('#menu' + i).bind('mouseenter', mouseEnterEffect);
          $('#menu' + i).bind('mouseleave', mouseLeaveEffect);
          $(".menu-activo", $('#menu' + i)).fadeOut(100);
          $(".menu-inactivo", $('#menu' + i)).fadeIn(100);
	    }
		}
		else
		{
	    for (i = 1; i <= pages; i++) {
	        $('#menu' + i).unbind('mouseenter');
	        $('#menu' + i).unbind('mouseleave');
	        if (i != menuActivo) {
	            $('#menu' + i).bind('mouseenter', mouseEnterEffect);
	            $('#menu' + i).bind('mouseleave', mouseLeaveEffect);
	           $(".menu-activo", $('#menu' + i)).fadeOut(100);
	            $(".menu-inactivo", $('#menu' + i)).fadeIn(100);
	        }
	        else
	        	{
	        		 $(".menu-activo", $('#menu' + i)).fadeIn(100);
	            $(".menu-inactivo", $('#menu' + i)).fadeOut(100);
	        	}
	    }
	  }
}

function mouseEnterEffect()
{
    $(".menu-inactivo", this).fadeOut(100);
    $(".menu-activo", this).fadeIn(100);
}

function mouseLeaveEffect()
{
    $(".menu-inactivo", this).fadeIn(100);
    $(".menu-activo", this).fadeOut(100);
}


$(document).ready(function() {

    //pageWidth = $('#main').width() -visibleNextZone;
    //pageHeight = $('#main').height() - visibleNextZone;

    /* fix height and width */

    windowsTotalWidth = ((pageWidth * columns) + margin + (separation * columns));
    windowsTotalHeight = ((pageHeight * rows) + margin + (separation * rows));

    $(".contenidoMenuIn").append("<div style='position:absolute;left:0px;top:0px;height:1px;width:" + windowsTotalWidth + 'px' + "'></div>");
    $(".contenidoMenuIn").append("<div style='position:absolute;left:0px;top:0px;width:1px;height:" + windowsTotalHeight + 'px' + "'></div>");

    /* set background */

    $("#contenidoMenuIn").append("<div style='position:absolute;left:0px;top:0px;height:" + windowsTotalHeight + "px" + ";width:" + windowsTotalWidth + 'px' + "; background-image:url(" + background + "); z-index:99;'></div>");
    //$("#main").after("<div style='position:absolute;left:0px;top:0px;height:" + windowsTotalHeight + "px" + ";width:"+ windowsTotalWidth +'px' + "; z-index:99;'><img src='" + background + "' /></div>");

    /*$("p").after("<b>Hello</b>");*/

    /* adapt css */

    $('.contenidoMenu').css('overflow', 'hidden');
    $('.contenidoMenuIn').css('display', 'block');
    $('.contenidoMenuIn').css('margin-left', '0px');
    $('.contenidoMenuIn').css('left', '0px');
    $('.contenidoMenuIn').css('overflow-x', '');
    $('.contenidoMenuIn').css('overflow', 'hidden');

    /* doLayout */

    var currentPage = 0;
    for (i = 0; i < rows; i++) {
        for (j = 0; j < columns; j++) {
            currentTop = (pageHeight * i) + margin + (separation * i);
            currentLeft = (pageWidth * j) + margin + (separation * j);
            $('#page' + currentPage).css('position', 'absolute');
            $('#page' + currentPage).css('left', currentLeft + 'px');
            $('#page' + currentPage).css('top', currentTop + 'px');
            $('#page' + currentPage).css('width', pageWidth + 'px');
            $('#page' + currentPage).css('height', pageHeight + 'px');
            //$('#page' + page).css('','');

            currentPage++;
        }
    }

    /* attach events */

    for (i = 1; i <= pages; i++) {
        $('#page' + i).bind('click', { page: i }, function(event) {
            if (menuActivo != event.data.page) {
                menuActivo = event.data.page;
                $('.contenidoMenuIn').scrollTo($('#page' + event.data.page), 800, { offset: -(visibleNextZone / 2) });
                event.preventDefault();
                atacthEvents();
            }
        });
        $('#menu' + i).bind('click', { page: i }, function(event) {
            if (menuActivo != event.data.page) {
                menuActivo = event.data.page;
                atacthEvents();
                $('.contenidoMenuIn').scrollTo($('#page' + event.data.page), 800, { offset: -(visibleNextZone / 2) });
                event.preventDefault();
            }
        });
        
        $('.menuSEO a[scrollTo*=page' + i + ']').bind('click', { page: i }, function(event) {
	        	if (menuActivo != event.data.page) {
                menuActivo = event.data.page;
                atacthEventsSEO();
				        $('.contenidoMenuIn').scrollTo('#page' + event.data.page, 800, { offset: -(visibleNextZone / 2) });
				        event.preventDefault();
			         }
		    });
        
    }
    
    


    $('.logo').bind('click', { page: 0 }, function(event) {
        if (menuActivo != event.data.page) {
            menuActivo = event.data.page;
            atacthEvents();
            $('.contenidoMenuIn').scrollTo($('#page' + event.data.page), 800, { offset: -(visibleNextZone / 2) });
            event.preventDefault();
        }
    });
    
    $('.menuSEO a[scrollTo*=page0]').bind('click', { page: 0 }, function(event) {
        if (menuActivo != event.data.page) {
            menuActivo = event.data.page;
            atacthEventsSEO();
            $('.contenidoMenuIn').scrollTo($('#page' + event.data.page), 800, { offset: -(visibleNextZone / 2) });
            event.preventDefault();
        }
    });
    


    atacthEvents();
    $(".menu-inactivo", $('#menu' + inipage)).hide();
    $(".menu-activo", $('#menu' + inipage)).fadeIn();
    $('.contenidoMenuIn').scrollTo($('#page' + inipage), 800, { offset: -(visibleNextZone / 2) });

});
