//$(function () {
$(document).ready(function(){
    //prepareHoverImages();
    $("#logo").jrumble({ rumbleSpeed: 100, rumbleEvent: 'hover' });
    $("#botones_servicios img").jrumble({ rumbleSpeed: 50, rumbleEvent: 'hover' });

    $("#planes").click(function () { ShowAlbumPhotos('179798418740047'); });
    $("#chucho_pachanga").click(function () { ShowAlbumPhotos('189574854429070'); });
    $("#inmuebles").click(function () { ShowAlbumPhotos( '181153651937857'); });
    $("#lista_hall").click(function () { ShowAlbumPhotos('181153158604573'); });
    $("#camelle").click(function () { ShowAlbumPhotos('181150068604882'); });
    $("#agencia_de_viajes").click(function () { ShowAlbumPhotos('189724284414127'); });

    $("#contactoLink, #contactoLinkHeader, #ProductosHeader, #QuienesSomos").fancybox({
		'scrolling' : 'no',
		'titleShow'	: false,
        'onStart' : function(){
            $("#Videos, #MusicaChivas").dialog("close");
        }
	});

    $("#MusicaChivas").dialog({ autoOpen: false, show: "blind", hide: "explode", title : "Reproductor Chivas", position:  ['right','top']  });
    $("#Videos").dialog({ autoOpen: false, show: "blind", hide: "explode", width: 450, title : "Videos Chivas", position:  ['right','top']  });

    $("#videosBox").click(function() {
        $("#Videos").dialog("open");
        return false;
    });

    $("#musicaChivasLink").click(function() {
        $("#MusicaChivas").dialog("open");
        return false;
    });

    /*
    $('#slider').galleryView({
        panel_animation: "slide", easing: "easeInOutBack",
        transition_speed: 800, transition_interval: 4000,
        panel_width: 938, panel_height: 350,
        frame_width: 0, frame_height: 0,
        pause_on_hover: true, show_filmstrip: false
    }); */

    getPhotosHall();
    getPhotosSlide();
});

function enviarComentario () {
    $.post("comentario_correo.php", $("#fancybox-content form").serialize());
    alert('Su solicitud ha sido exitosa, en momentos un agente se comunicara con usted.');
    $.fancybox.close();
}

$(window).load(function(){

});

/*
function prepareHoverImages() {
    //add hover effects    
    $(".hovers").hover(function () {
        //$(this).attr("src", $(this).attr("src").replace(".", "-over."));
        $(this).animate({ opacity: 0.5 }, 1000);
    }, function () {
        //$(this).attr("src", $(this).attr("src").replace("-over.", "."));
        $(this).animate({ opacity: 1 }, 1000);
    });

    //preload Images
    //$(".hovers").each(function () {
    //    (new Image()).src = $(this).attr("src").replace(".", "-over.");
    //});
}*/

function getPhotosHall(){
    $.ajax({
        url: 'http://graph.facebook.com/181153158604573/photos?callback=?',
        dataType: 'json',
        crossDomain: true,
        success: function (response) {
            $("#lista_hall").empty();

            var reverseResponse = response.data.reverse();
            for(var i=0; i < 6; i++ ){
                var val = reverseResponse[i];
                var item = $("<li>");
                item.append("<img src='"+ val.images[1].source +"' />");
                $("#lista_hall").append(item);
            }

            addAnimationHall();
        }
    });
}

function getPhotosSlide(){
    $.ajax({
        url: 'http://graph.facebook.com/224220750964480/photos?callback=?',
        dataType: 'json',
        crossDomain: true,
        success: function (response) {
            $("#slider").empty();

            //var reverseResponse = response.data.reverse();
            for(var i=0; i < response.data.length; i++ ){
                var val = response.data[i];
                var item = $("<li>");
                item.append("<img src='"+ val.images[0].source +"' />");
                $("#slider").append(item);
            }

             $('#slider').galleryView({
                panel_animation: "slide", easing: "easeInOutBack",
                transition_speed: 800, transition_interval: 4000,
                panel_width: 938, panel_height: 350,
                frame_width: 0, frame_height: 0,
                pause_on_hover: true, show_filmstrip: false
            });
        }
    });
}

function  addAnimationHall(){
    $("#lista_hall li").hover(function() {
        $(this).css({'z-index' : '10'}); /*Add a higher z-index value so this image stays on top*/
        $(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
        .animate({
            marginTop: '-100px', marginLeft: '-100px',
            top: '50%', left: '50%',
            width: '200%', height: '200%',
            padding: '10px'
        }, 200);

    } , function() {
        $(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
        $(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
        .animate({
            marginTop: '0', marginLeft: '0',
            top: '0', left: '0',
            width: '100%',  height: '100%',
            padding: '0px'
        }, 400);
    });
}

function ShowAlbumPhotos(albumId) {
    var facnyboxOptions = { 'type': 'image', 'changeFade': 0, 'padding': 0,
        'transitionIn': 'elastic', 'transitionOut': 'elastic',
        'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
		},
        'onStart' : function(){
            $("#Videos, #MusicaChivas").dialog("close");
        }
    };

    $.ajax({
        url: 'http://graph.facebook.com/' + albumId + '/photos?callback=?',
        dataType: 'json',
        crossDomain: true,
        success: function (response) {
            if (!response || response.error) { return; }

            var photos = [];
            $.each(response.data, function (key, val) {
                photos.push(val.source);
            });

            var reversePhotos = photos.reverse();
            $.fancybox(reversePhotos, facnyboxOptions);
        }
    });
}
