<?php
if(isset($_POST['media_ajax'])) {
    ob_end_clean();
    ob_start();
    //require_once("../../adbcon.php");

    $media = array();
    $req = $_POST['req'];
    $mc = $_POST['mc'];
    $mc_folder = "$include_www/imCinemaAdmin/movies/$mc/";

    $q = "select * from im_movies where movie_code='$mc';";
    $r = @mysql_query($q);
    if($r && mysql_num_rows($r)>0) {
        $md = mysql_fetch_assoc($r);

        // Get Posters and Trailers
        if($req == "posters") {
            $pos = 0;
            $trails = explode("|", $md['trailers']);
            if(is_array($trails) && count($trails) > 0) {
                $cnt = 1;
                foreach ($trails as $i=>$trailer) {
                    if(trim($trailer) != "") {
                        $media [] = array(
                            "title" => "Trailer $cnt",
                            "url"	=> trim($trailer),
                            "description" => "",
                            "type"	=> "mv"
                        );
                        $cnt++;
                    }
                }
            }

            $posters = array();
            if(trim($md['poster']) != "") {
                $url = $mc_folder . $md['poster'];
                $media [] = array(
                    "title" => "Poster 1",
                    "url"	=> $url,
                    "description" => "",
                    "type"	=> "im"
                );
            }

            if(trim($md['poster_large']) != "") {
                $url = $mc_folder . $md['poster_large'];
                $media [] = array(
                    "title" => "Poster 2",
                    "url"	=> $url,
                    "description" => "",
                    "type"	=> "im"
                );
            }

            if(trim($md['poster_full']) != "") {
                $url = $mc_folder . $md['poster_full'];
                $media [] = array(
                    "title" => "Poster 3",
                    "url"	=> $url,
                    "description" => "",
                    "type"	=> "im"
                );
            }

        }

        // Get Trailers Only
        if($req == "trailers") {
            $pos = 0;
            $trails = explode("|", $md['trailers']);
            if(is_array($trails) && count($trails) > 0) {
                $cnt = 1;
                foreach ($trails as $i=>$trailer) {
                    if(trim($trailer) != "") {
                        $media [] = array(
                            "title" => "Trailer $cnt",
                            "url"	=> trim($trailer),
                            "description" => "",
                            "type"	=> "mv"
                        );
                        $cnt++;
                    }
                }
            }
        }

        // Get Media Images
        if($req == "images") {
            $pos = 0;
            $def_im = (isset($_POST['im'])) ? $_POST['im'] : "";
            $imgs = explode("|", $md['media_images']);
            if(is_array($imgs) && count($imgs) > 0) {
                $cnt = 1;
                foreach ($imgs as $i=>$img) {

                    if(trim($img) != "") {
                        if($def_im == $img) $pos = $cnt - 1;
                        $media [] = array(
                            "title" => "Media Image $cnt",
                            "url"	=> $mc_folder.$img,
                            "description" => "Media Image $cnt",
                            "type"	=> "im"
                        );
                        $cnt++;
                    }
                }
            }
        }

    }

    if(count($media ) > 0) {
        $media_all = array(
            "group"	=> "ppax_" . $mc,
            "pos"	=> $pos,
            "media"	=> $media
        );
    }

    ob_end_clean();
    while(ob_get_contents()) ob_end_clean();
    echo json_encode($media_all);
    die();
}