$(document).ready(function(){
    $("a[rel^='prettyPhoto']").prettyPhoto({
        social_tools : false,
        theme: 'pp_default'
    });

    $('img.poster_small').each(function(){
        var $poster = $(this);
        $poster.click(function(ev){
            var axmc = $poster.attr('rel');
            var axurl = "index.php";
            $.ajax({
                url: axurl,
                data: {media_ajax:'1',mc:axmc,req:'posters', module_page:'im_cinemas'},
                type: 'POST',
                cache: false,
                dataType: 'json',
                success: function(data){
                    var group = data['group'];

                    var images = [];
                    var titles = [];
                    var descs = [];
                    for(var i=0; i<data['media'].length; i++) {
                        var media = data['media'][i];

                        images.push(media.url + "?iframe=true&width=100%&height=100%");
                        titles.push(media.title);
                        descs.push(media.description);
                    }

                    $.prettyPhoto.open(images,titles,descs);
                }
            });
        });
    });

    $('a.trailers').each(function(){
        var $trailer = $(this);
        $trailer.on('click', function(ev){
            ev.preventDefault();
            var axmc = $trailer.attr('rel');
            var axurl = baseurl + '/';
            $.ajax({
                url: axurl,
                data: {media_ajax:'1',mc:axmc,req:'trailers', module_page:'im_cinemas'},
                type: 'POST',
                cache: false,
                dataType: 'json',
                success: function(data){
                    var group = data['group'];

                    var images = [];
                    var titles = [];
                    var descs = [];
                    for(var i=0; i<data['media'].length; i++) {
                        var media = data['media'][i];

                        images.push(media.url + "?iframe=true&width=100%&height=100%");
                        titles.push(media.title);
                        descs.push(media.description);
                    }

                    $.prettyPhoto.open(images,titles,descs);
                }
            });

            return false;
        });
    });


    $('a.media_image').each(function(){
        var $mi = $(this);
        $mi.click(function(ev){
            ev.preventDefault();
            var ax = $mi.attr('rel').split("|");
            var axmc = ax[0];
            var axim = ax[1];
            var axurl = "index.php";
            $.ajax({
                url: axurl,
                data: {media_ajax:'1',mc:axmc,im:axim, req:'images', module_page:'im_cinemas'},
                type: 'POST',
                cache: false,
                dataType: 'json',
                success: function(data){
                    var group = data['group'];
                    var pos = data['pos'];
                    var images = [];
                    var titles = [];
                    var descs = [];
                    for(var i=0; i<data['media'].length; i++) {
                        var media = data['media'][i];

                        images.push(media.url);
                        titles.push(media.title);
                        descs.push(media.description);
                    }

                    $.prettyPhoto.open(images,titles,descs, pos);
                }
            });
            return false;
        });
    });

    $('a.movie_popup').each(function(){
        var $mp = $(this);
        var hr = $mp.attr('href') + "?page=1&iframe=true&width=100%&height=100%";
        $mp.attr('href', hr);

        //alert($mp.attr('href'));

    });


});