var num_items_shown = 2;
var num_video_items_shown = 2;
var g_counter = 0;
var g_video_counter = 0;
var sets = 0;
var sets_video = 0;
var sets_current_item = 0;
var container_height = 0;
var container_video_height = 0;
var thumbnail_height = 75;
var thumbnail_paddings = 12;
var speed = 1000;
var total_items = 0;
var current_selected_item = 1;


function show_g_list(path, title){
	
    var str_parse = "";
    var active = "active";
    jQuery.ajax({
      url: siteurl+"/ajax_g_list.php?p="+path,
      success: function(data){
          var data_ary = data.split("|");
          jQuery("#g_viewer .g_list .g_container .g_spanner").html("");
          if(data != ""){
                        jQuery(".title_photos").show();
                        jQuery(".g_container").show();
                        jQuery(".g_navigation").show();

              for(var i in data_ary)
                {
                    str_parse = "<a href=\"javascript:show_item2('item"+i+"');\" class=\"item "+active+" item"+i+"\">";
                    str_parse = str_parse + '<img src="'+siteurl+'/images/gallery/'+path+'/thumbnail/'+data_ary[i]+'" alt=\"\" />';
                    str_parse = str_parse + "</a>";

                    if(active == "active"){
                        active = "";
                    }

                    jQuery("#g_viewer .g_list .g_container .g_spanner").append(str_parse);

	                }
					
          }
		  else{
                jQuery(".title_photos").hide();
                jQuery(".g_container").hide();
                jQuery(".g_navigation").hide();

          }

          jQuery.ajax({
              url: siteurl+"/ajax_g_video_path_youtube.php?p="+path,
              success: function(data2){
                  var first_video = "";
                  var first_video_preview = "";
                    if(data2 != ""){
                        jQuery(".g_video_navigation").show();
                        jQuery(".title_video").show();
                        jQuery(".g_container_video").show();
                        var data_ary = data2.split("|");
                        var preview = "";
                        var video = "";
                        jQuery("#g_viewer .g_list .g_container_video .g_spanner_video").html("");
                        for(var i in data_ary)
                        {

                            preview = data_ary[i];
                            preview = preview.split("~");
                            video = preview[0];
                            preview= preview[1];
                            str_parse = "<a href=\"javascript:show_video('"+video+"','"+preview+"');\" class=\"item "+active+" item"+i+"\">";
                            str_parse = str_parse + '<img src="'+siteurl+'/'+preview+'" alt=\"\" style="width:100px;height: 75px;" />';
                            str_parse = str_parse + "</a>";
                            if(active == "active"){
                                active = "";
                            }
                            jQuery("#g_viewer .g_list .g_container_video .g_spanner_video").append(str_parse);
                            if(i == 0){
                                first_video = video;
                                first_video_preview = preview;
                            }
                        }
                    }
                    else{
                        jQuery(".g_video_navigation").hide();
                        jQuery(".title_video").hide();
                        jQuery(".g_container_video").hide();
                    }

                    var images_data = data;
                    var video_data = data2;

                  //  alert("has img = [" + images_data + "]");
                  //  alert("has video = [" + video_data + "]");
                    
                    if(video_data != "" && images_data == ""){
                        g_init(true, false);
                       // alert("has video and has no img");
                        show_video(first_video , first_video_preview);
                    }
                    if(video_data == "" && images_data != ""){
                        g_init(false, true);
                      //  alert("has no video and has img");
                    }
                    if(video_data != "" && images_data != ""){
                        g_init(true, true);
                      //  alert("has video and has img");
                        show_video(first_video , first_video_preview);
                    }
                   // alert(jQuery("#g_viewer .g_preview_container .g_preview_sub_title").html());

                    jQuery("#g_viewer .g_preview_container .g_preview_sub_title").html(title);

                    jQuery.colorbox({inline:true, href:"#g_viewer", width: 620, height: 500, scrolling: true})
					jQuery(".g_spanner").animate({top: "0"});	
              }
          });

      }
    });
}
var path = "";
var path_video = "";
var preview_path = "";
var preview_path_video = "";
var kids = "";
var kids_video = "";
function g_init(has_video, has_img){
    jQuery(".g_preview").html("");
    if(has_img == true){
        path = jQuery(".g_list .g_container .g_spanner .item.active img").attr("src");
        preview_path = path.replace("thumbnail/", "");
        jQuery(".g_preview").html("<div><img src=\""+preview_path+"\" alt=\"\" ><div class=\"g_video\"></div></div>");
        kids = jQuery(".g_list .g_container .g_spanner").children();
        container_height = num_items_shown * (thumbnail_height + thumbnail_paddings);
        jQuery(".g_list .g_container").height(container_height);
        sets = Math.ceil( kids.length / num_items_shown );
        jQuery(".g_list .g_container .g_spanner").height(kids.length * (thumbnail_height + thumbnail_paddings) );
        jQuery(".g_list .g_navigation").height(container_height - thumbnail_paddings);
    }
    else{
        jQuery(".g_preview").html("<div><img src=\"\" alt=\"\" ><div class=\"g_video\"></div></div>");
    }
    if(has_video == true){
        kids_video = jQuery(".g_list .g_container_video .g_spanner_video").children();
        container_video_height = num_video_items_shown * (thumbnail_height + thumbnail_paddings);
        sets_video = Math.ceil( kids_video.length / num_video_items_shown );
        if(kids_video.length > num_video_items_shown){
            jQuery(".g_video_navigation").show();
        }
        else{
            container_video_height = kids_video.length * (thumbnail_height + thumbnail_paddings);
            jQuery(".g_video_navigation").hide();
        }
        jQuery(".g_list .g_container_video").height(container_video_height - 7);
        jQuery(".g_list .g_container_video .g_spanner_video").height(kids_video.length * (thumbnail_height + thumbnail_paddings) );
        jQuery(".g_list .g_video_navigation").height(container_video_height - thumbnail_paddings);
    }
    if(has_video == true && has_img == false){
        jQuery(".g_preview .g_video").show();
        jQuery(".g_preview img").hide();
    }
    if(has_video == false && has_img == true){
        jQuery(".g_preview .g_video").hide();
        jQuery(".g_preview img").show();
    }
    if(has_video == true && has_img == true){
        jQuery(".g_preview .g_video").show();
        jQuery(".g_preview img").hide();
    }

}
function show_item2(item_str){
    var item = jQuery(".g_list .g_container .g_spanner .item."+item_str);
    current_selected_item = item.index() + 1;

    jQuery(".g_list .g_container .g_spanner .item").removeClass("active");

    item.addClass("active");

    var path = item.find("img").attr("src");

    var preview_path = path.replace("thumbnail/", "");
    jQuery(".g_preview .g_video").fadeOut(function(){
        jQuery(".g_preview img").fadeOut(function(){
            jQuery(".g_preview img").load(function () {
                jQuery(this).fadeIn();
            }).error(function () {

            }).attr('src', preview_path);
        });
    });

}


function show_video(video,preview){
    jQuery(".g_preview img").fadeOut(function(){
        jQuery(".g_preview .g_video").html('<iframe scrolling="no" frameborder="0" width="360" height="280" src="'+siteurl+'/video_player_youtube.php?path='+video+'&preview='+preview+'&width=320&height=240"></iframe>')
        jQuery(".g_preview .g_video").fadeIn();
    });
}

function show_item(item){
    current_selected_item = item.index() + 1;
    jQuery(".g_list .g_container .g_spanner .item").removeClass("active");
    item.addClass("active");
    var path = item.find("img").attr("src");
    var preview_path = path.replace("thumbnail/", "");
    jQuery(".g_preview img").fadeOut(function(){
        jQuery(".g_preview img").load(function () {
            jQuery(this).fadeIn();
        }).error(function () {
        }).attr('src', preview_path);
    });
}


function load_video(path,preview,type,width,height){
    var video = '';
    video = video + '<div class="video-js-box">';
    video = video + '<video id="example_video_1" class="video-js" width="'+width+'" height="'+height+'" controls="controls" preload="auto" poster="'+preview+'">';
    video = video + '<source src="'+path+'" type=\''+type+'\' />';
    video = video + '<object id="flash_fallback_1" class="vjs-flash-fallback" width="'+width+'" height="'+height+'" type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf">';
    video = video + '<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />';
    video = video + '<param name="allowfullscreen" value="true" />';
    video = video + '<param name="flashvars" value=' + '\'config={"playlist":["'+preview+'", {"url": "'+path+'","autoPlay":false,"autoBuffering":true}]}\' />';
    video = video + '<img src="'+preview+'" width="'+width+'" height="'+height+'" alt="Poster Image" title="No video playback capabilities." />';
    video = video + '</object>';
    video = video + '</video>';
    video = video + '</div>';
    return video;
}


jQuery(document).ready(function() {
    jQuery(".g_prev").click(function(){
            g_move_list("prev");
        }
    );
    jQuery(".g_next").click(function(){
            g_move_list("next");
        }
    );
    jQuery(".g_video_prev").click(function(){
            g_video_move_list("prev");
        }
    );
    jQuery(".g_video_next").click(function(){
           g_video_move_list("next");
        }
    );



    function g_video_move_list(mode){
        if(mode == "prev"){
            if(g_video_counter > 0){
                g_video_counter--;

                var counter_pixels = g_video_counter * container_video_height;

                jQuery(".g_list .g_container_video .g_spanner_video").animate({
                    top: '-'+counter_pixels+'px'
                }, {
                    duration: speed
                }
                )
            }
        }
        else{
            if(g_video_counter < (sets_video - 1)){
                g_video_counter++;

                var counter_pixels = g_video_counter * container_video_height;

                jQuery(".g_list .g_container_video .g_spanner_video").animate({
                    top: '-'+counter_pixels+'px'
                }, {
                    duration: speed
                }
                )
            }
        }

    }
    function g_move_list(mode){
        if(mode == "prev"){
            if(g_counter > 0){

                g_counter--;

                var counter_pixels = g_counter * container_height;

                jQuery(".g_list .g_container .g_spanner").animate({
                    top: '-'+counter_pixels+'px'
                }, {
                    duration: speed
                }
                )
            }
        }
        else{
            if(g_counter < (sets - 1)){

                g_counter++;

                var counter_pixels = g_counter * container_height;

                jQuery(".g_list .g_container .g_spanner").animate({
                    top: '-'+counter_pixels+'px'
                }, {
                    duration: speed
                }
                )

            }
        }

    }

/*
    jQuery("#g_viewer .g_list .g_container .g_spanner .item").click(function(){

        alert("test");
      //  show_item(jQuery(this));

    });
*/

    jQuery(".g_preview_next a").click(function(){
        g_navigate("next");
    })


    jQuery(".g_preview_prev a").click(function(){
        g_navigate("prev");
    })

    function g_navigate(mode){

        var item = jQuery(".g_list .g_container .g_spanner .item.active");

        if(mode == "prev"){
            var active_item = item.prev();
        }
        else{
            var active_item = item.next();
        }

        if(active_item.index() != -1){

            var raw_current_set = parseInt((active_item.index()/num_items_shown)+1);

            if(g_counter+1 != raw_current_set){
                g_move_list(mode);
            }

            item.removeClass("active");
            active_item.addClass("active");

            show_item(active_item);

        }
    }



});
jQuery(".g_container").animate({top: "0"},{duration:0});

