$(document).ready(function() {
     $.easing.expoout = function (x, t, b, c, d) {
        return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
    }
    $(".favphoto").mouseover(function(){
        $(this).fadeTo("fast",0.8)
    })
    $(".favphoto").mouseout(function(){
        $(this).fadeTo("fast",1)
    })
    $(".favphoto").click(function(){
       var id = $(this).attr('id')
       id = id.substr(1, id.length)

       $.post("/api/restaurant/photo/fav/"+id, { ske:"nh2%hn458sE$F_a37rfTyh" }, function(data){
           if(data==id){
               $("#h"+id).css('display','block').animate({
                    easing:"expoout",
                    fontSize:"48px",
                    paddingLeft:"65px",
                    opacity: 0
                }, 760 , function(){
                    $("#l"+id).text(Number($("#l"+id).text())+1);
                    $("#h"+id).attr("style", "font-size:28px;line-height:48px;font-weight:bold;color:#fff;z-index:234;position:absolute;display:none;padding-top:50px;padding-left:82px;")
                });
           }
        }, "json");
    })
    $("li img").click(function(){
       var id = $(this).attr('id')
       id = id.substr(1, id.length)
       $("#a"+id).click()
    })

    $("li img").mouseover(function(){
       var img = $(this).attr('id')
       $(this).stop().fadeTo(400,1)
       $("li img[id!='"+img+"']").stop().fadeTo(400, 0.45)
    })

    $("li img").mouseout(function(){
       var img = $(this).attr('id')
       $("li img[id!='"+img+"']").show(500).fadeTo(600, 1)
    })

    $("#allphoto a").overlay({
        target: '#gallery',
        expose: '#000',
        onLoad: startTimer,
        onClose: stopTimer
    }).gallery({
        speed: 800,
        template:'<strong>${title}</strong> <span>Image ${index} of ${total}</span>'
    });

    url = window.location.href.toString().split('/#');

    if(url==window.location.href.toString()){
        url = window.location.href.toString().split('#');
    }

    if(url.length>1){
        var id = Number(url[1])
        $("#a"+id).click()
    }

})
var url
var imgurl='';

function startTimer(){
    setInterval('addChng()', 500 );
}
function stopTimer(){
    clearInterval('addChng');
}
function addChng(){
    if(imgurl!=$('#img').attr('src')){
        imgurl = $('#img').attr('src');
        var imgId = $("a[href="+imgurl+"]").attr('id').toString().substr(1)
        document.location.href = '#' + imgId;
        addview(imgId, $('#vs'+imgId).text().replace(' views',''))
    }
}

function addview(id,view){
   $.post("/api/restaurant/photo/view/"+id+"/"+view, { ske:"nh2%hn458sE$F_a37rfTyh" });
}
