var galleryImages=new Array();
var currentImage=0;

var timeout = 10000;
var timed = null;
var current = 0;

var fiche_timed = null;
var fiche_timeout = 15000;
var fiche_id = '';

function Change(next)
{
    $('ul#diffusion_carousel li').eq(current).fadeOut(100);       
    $('ul#diffusion_carousel li').eq(next).fadeIn(100);
    current = next;
}

function TimedChange()
{
    if(current < $('ul#diffusion_carousel li').length-1)
    {
        Change(current+1);
    }
    else
    {
        Change(0);
    }
    timed = setTimeout('TimedChange()', timeout);
}

function ficheLoad(){
	

    var next_fiche_id = $(".fiches #fiche_act").next().children('div.txt').attr('id');
    var act_fiche_id = $(".fiches #fiche_act .txt").attr('id');

    if(next_fiche_id) {
//        console.log('a');
        $(".fiches #fiche_act").next().attr('id','fiche_act2');
    } else {
//        console.log('b');
        $(".fiches").children(['0']).attr('id','fiche_act2');
        next_fiche_id = $(".fiches #fiche_act2 .txt").attr('id');
    }
    $(".fiches #fiche_act").attr('id','');
    $(".fiches #fiche_act2").attr('id','fiche_act');

//    console.log('2 '+next_fiche_id);
	
    fiche_timed = setTimeout("ficheLoad('"+next_fiche_id+"')", fiche_timeout);

    if($('#inHere')){
            $('#inHere').append('<div class="shadow" style="width: 100%; height: 100%; z-index: 100; position: absolute; top: 0pt; left: 0pt; opacity: 0.5; background: none repeat scroll 0pt 0pt black;"></div><div style="position: absolute; top: 50%; left: 50%;"><img width="32px" src="fileadmin/templates/img/spinner3-black.gif"></div>');

            var uid = next_fiche_id;
            $.ajax({
                url: 'http://'+document.domain+'/index.php?id=1',
                data: {
                    'in':uid,
                    'ajx': 1,
                    'Lang': $("#language_id").val()
                },
                async: false,
                success:function(content){
                    $('#inHere').html(content);
                    $('#film_list_front').tabs({
                        load: function(){
                            $(".film_list_text_front").jScrollPane({
                                showArrows: true
                            });
                        },
                        show: function(){
                            $(".film_list_text_front").jScrollPane({
                                showArrows: true
                            });
                        },
                        selected: 0
                    });

					Scroller('.scrollIt','#gallery_scroller_front .prev','#gallery_scroller_front .next');

                    $('#movie_photos_img li a').click(function(){
                        $('#gallery_photo_front').children("a:nth-child(2)").children("img").attr("src",$(this).attr("rel"));
                    });
					$('.gallery_more').parent().click(function(){

						id = $(this).children('.gallery_more').attr('id');

						blocks = id.split(":");

						ids = blocks[0].split("_");

						showGalleryImage(ids[1],ids[2],blocks[1]);
					});

                                         $('#gallery_ico_front a').click(function(){
                                            $('#gallery_photo_front').children("a:nth-child(2)").children("img").attr("src",$(this).attr("rel"));
                                            $('.gallery_more').attr("id",$(this).attr("class"));
                                        });
                }
            });
    }
}

$(document).ready(function(){
    //fiche_id = $(".fiches #fiche_act .txt").attr('id');
    //console.log('1 '+fiche_id);

//    $(".fiches #fiche_act").next().attr('id','fiche_act2');
//    $(".fiches #fiche_act").attr('id','');
//    $(".fiches #fiche_act2").attr('id','fiche_act');
	
	

    fiche_timed = setTimeout("ficheLoad()", fiche_timeout);

    timed = setTimeout('TimedChange()', timeout);

//    $(".fiches #fiche_act").attr('id','');
//    $(".fiches #fiche_act").next().attr('id','fiche_act');
//    fiche_timed = setTimeout("ficheLoad('"+fiche_id+"')", fiche_timeout);
    
    
    /*$('.light').click(function(){

        id = $(this).attr('id').substr(1);

        ids = id.split("_");

        showAfficheImage(ids[1],ids[0]);
    });*/
    $('.light_close').click(function(){

        $(".lightboxsmall_content").hide();
        $(".lightbox_content").hide();
        $("#lightbox_bg").hide();
    });

    $('.gallery_more').parent().click(function(){

        id = $(this).children('.gallery_more').attr('id');

        blocks = id.split(":");

        ids = blocks[0].split("_");

        showGalleryImage(ids[1],ids[2],blocks[1]);
    });


    $('.lightbox_prev').click(function(){

        if(currentImage > 0)
            showImage(currentImage - 1);

    });
    $('.lightbox_next').click(function(){

        if(currentImage < galleryImages.length - 1)
            showImage(currentImage + 1);

    });

	$('.carousel_dvd_vod ul').jcarousel({
		scroll: 1,
        vertical: true,
        initCallback: dvdCallBack2,
        buttonNextHTML: null,
        buttonPrevHTML: null
	});

//$('ul#diffusion_carousel li').eq(current).fadeOut();       
//$('ul#diffusion_carousel li').eq(next).fadeIn();

	function dvdCallBack2(carousel) {
        $('#pager_top2').bind('click', function() {
            
            if(current > 0)
            {
                Change(current-1);
            }
            else
            {
                Change(length-1);
            }
//            carousel.prev();
//			console.log('top');
            return false;
        });

        $('#pager_bottom2').bind('click', function() {
            
            if(current < $('ul#diffusion_carousel li').length-1)
            {
                Change(current+1);
            }
            else
            {
                Change(0);
            }
            
            
        //            carousel.next();
                    return false;
                });
            };

//    $(".carousel_dvd_vod .jcarousel-container").attr('style','height:150px;');
//    $(".carousel_dvd_vod .jcarousel-container").attr('style','overflow:hidden;');

    function loadFirstDailymotion(){
        $('#inHere').append('<div class="shadow" style="width: 100%; height: 100%; z-index: 100; position: absolute; top: 0pt; left: 0pt; opacity: 0.5; background: none repeat scroll 0pt 0pt black;"></div><div style="position: absolute; top: 50%; left: 50%;"><img width="32px" src="fileadmin/templates/img/spinner3-black.gif"></div>');

            var uid = $(".fiches #fiche_act .txt").attr('id');

            $.ajax({
//                url: location+'index.php?id=1',
                url: 'http://'+document.domain+'/index.php?id=1',
                data: {
                    'in':uid,
                    'ajx': 1,
                    'Lang': $("#language_id").val()
                },
                async: false,
                success:function(content){
                    $('#inHere').html(content);
                    $('#film_list_front').tabs({
                        load: function(){
                            $(".film_list_text_front").jScrollPane({
                                showArrows: true
                            });
                        },
                        show: function(){
                            $(".film_list_text_front").jScrollPane({
                                showArrows: true
                            });
                        },
                        selected: 0
                    });

					Scroller('.scrollIt','#gallery_scroller_front .prev','#gallery_scroller_front .next');

                    $('#movie_photos_img li a').click(function(){
                        $('#gallery_photo_front').children("a:nth-child(2)").children("img").attr("src",$(this).attr("rel"));
                    });
					$('.gallery_more').parent().click(function(){

						id = $(this).children('.gallery_more').attr('id');

						blocks = id.split(":");

						ids = blocks[0].split("_");

						showGalleryImage(ids[1],ids[2],blocks[1]);
					});

                                         $('#gallery_ico_front a').click(function(){
                                            $('#gallery_photo_front').children("a:nth-child(2)").children("img").attr("src",$(this).attr("rel"));
                                            $('.gallery_more').attr("id",$(this).attr("class"));
                                        });
                }
            });
    }

     if($('#inHere')){
        $('.fiche').mouseover(function(){
			
            $('#inHere').append('<div class="shadow" style="width: 100%; height: 100%; z-index: 100; position: absolute; top: 0pt; left: 0pt; opacity: 0.5; background: none repeat scroll 0pt 0pt black;"></div><div style="position: absolute; top: 50%; left: 50%;"><img width="32px" src="fileadmin/templates/img/spinner3-black.gif"></div>');
			clearTimeout(fiche_timed);
			fiche_timed = setTimeout("ficheLoad()", fiche_timeout);
            var uid = $(this).find('.txt').attr("id");

            $(".fiches #fiche_act").attr('id','');
            $(this).attr('id','fiche_act');

            $.ajax({
                url: 'http://'+document.domain+'/index.php?id=1',
                data: {
                    'in':uid,
                    'ajx': 1,
                    'Lang': $("#language_id").val()
                },
                async: false,
                success:function(content){
                    $('#inHere').html(content);
                    $('#film_list_front').tabs({
                        load: function(){
                            $(".film_list_text_front").jScrollPane({
                                showArrows: true
                            });
                        },
                        show: function(){
                            $(".film_list_text_front").jScrollPane({
                                showArrows: true
                            });
                        },
                        selected: 0
                    });

					Scroller('.scrollIt','#gallery_scroller_front .prev','#gallery_scroller_front .next');

                    $('#movie_photos_img li a').click(function(){
                        $('#gallery_photo_front').children("a:nth-child(2)").children("img").attr("src",$(this).attr("rel"));
                    });
					$('.gallery_more').parent().click(function(){

						id = $(this).children('.gallery_more').attr('id');

						blocks = id.split(":");

						ids = blocks[0].split("_");

						showGalleryImage(ids[1],ids[2],blocks[1]);
					});

                                         $('#gallery_ico_front a').click(function(){
                                            $('#gallery_photo_front').children("a:nth-child(2)").children("img").attr("src",$(this).attr("rel"));
                                            $('.gallery_more').attr("id",$(this).attr("class"));
                                        });
                }
            });
        });
    }

    if($('#inHere')){
            loadFirstDailymotion();
    }

    //Flow box - left side of page
    $('#show_box_main #close').click(function(){
        $(this).parent().animate({
            left: "-"+$(this).parent().css("width")
        }, 300);
        $(this).parent().prev().animate({
            left: "-"+$(this).parent().css("width")
        }, 300, function(){
            $('#left_box').animate({
                left: 0
            }, 300);
        });
    });
    $('#left_box').click(function(){
        $('#left_box').animate({
            left: "-"+$('#left_box').css("width")
        }, 300, function(){
            $("#show_box_main").animate({
                left: 0
            }, 300);
            $("#show_box").animate({
                left: 0
            }, 300);
        });
    });
    $('#film_list_front').tabs({
        load: function(){
            $(".film_list_text_front").jScrollPane({
                showArrows: true
            });
        },
        show: function(){
            $(".film_list_text_front").jScrollPane({
                showArrows: true
            });
        },
        selected: 0
    });
    $(".film_production_content_front").jScrollPane({
        showArrows: true
    });
    $(".film_list_text_front").jScrollPane({
        showArrows: true
    });

    // jCarousel for nos productions
    /*$('#production_carousel').jcarousel({
        scroll: 1,
        initCallback: productionsCallBack,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    function productionsCallBack(carousel) {
        jQuery('.pager_right').bind('click', function() {
            carousel.next();
            return false;
        });

        jQuery('.pager_left').bind('click', function() {
            carousel.prev();
            return false;
        });
    };*/


    // jCarousel- DVD/VOD
	
    $('#dvd_carousel').jcarousel({
        scroll: 1,
        vertical: true,
        initCallback: dvdCallBack,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
    function dvdCallBack(carousel) {
        $('.pager_bottom').bind('click', function() {
            carousel.next();
            return false;
        });

        $('.pager_top').bind('click', function() {
            carousel.prev();
            return false;
        });
    };


    // end jCarousel- DVD/VOD

    /*function carCallBack(carousel) {
        $('#gallery_scroller_front .next').unbind('click').bind('click', function() {
            carousel.next();
            return false;
        });

        $('#gallery_scroller_front .prev').unbind('click').bind('click', function() {
            carousel.prev();
            return false;
        });
    };

	
	
    if($('#gallery_ico_front').find('ul')){
		$('#gallery_ico_front').jcarousel({
			scroll: 2,
			initCallback: carCallBack,
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
	}*/
	function Scroller(idgal, prev, next){
		$(idgal+' ul').children().each(function(){
			$(this).hide();
		});
		
		var amountChildren = $(idgal+' ul').children().length;
		
		var howMany = $(idgal+' input.amount').val();
		var howMany2 = $(idgal+' input.moveBy').val();
		var start = $(idgal+' input.start').val();
		var controls = $(idgal+' input.controls').val();
		var childs = $(idgal+'ul ').children().length;
		
		if(start == undefined){
			start = 0;
		}
		
		if(howMany == undefined){
			howMany = 1;
		}
		if(howMany2 == undefined){
			howMany2 = 1;
		}
		var end = (parseInt(howMany) * (parseInt(start) + 1));
		
		for(var x = (parseInt(start) * parseInt(howMany) + 1); x <= end; x++){
			if(controls != undefined){
			}
			$(idgal+' ul').children( ':nth-child(' + x + ')').each(function(){
				$(this).toggle();
			});
		}
		
		if(controls!=undefined){
			$('#movie_pager').children().each(function(){
				var pg = $(this).attr('id').substr(1);
				$(this).click(function(){
					$(idgal+' ul').children('li:visible').toggle();
					
					$('#movie_pager').children().children('img').attr('src',"fileadmin/templates/img/dot.jpg");
					
					$(this).children('img').attr('src',"fileadmin/templates/img/dot_act.jpg");
					var ct = 0;
					for(var y = ((parseInt(pg) * parseInt(howMany2))+1); y <= (parseInt(pg) * parseInt(howMany2)) + parseInt(howMany2); y++){
						$(idgal+' ul').children(':nth-child(' + y + ')').toggle();
						if($(idgal+' ul').children(':nth-child(' + y + ')').length == 0)
							ct++;
					}
					while(ct > 0){
						$(idgal+' ul').children('li:visible:first').prev().toggle();
						ct--;
					}
				});
			});
		}
		
		$(next).click(function(){
			start++;
			var bool2 = false;
			for(var x = 1; x <= howMany2; x++){
				var bool = true;
				$(idgal+' ul').children('li::visible:last').each(function(){
					if($(this).next().length > 0){
						$(this).next().toggle();
						bool2 = true;
					} else {
						bool = false;
					}
				});
				$(idgal+' ul').children('li:visible:first').each(function(){
					if(bool)
						$(this).toggle();
				});
			}
			if(controls != undefined && bool2){
				var obj = $('#movie_pager').find("[src='fileadmin/templates/img/dot_act.jpg']");
				obj.attr("src","fileadmin/templates/img/dot.jpg");
				obj.parent().next().children().attr("src","fileadmin/templates/img/dot_act.jpg");
			}
		});
		$(prev).click(function(){
			start--;
			var bool2 = false;
			
			//Here we protect from moving backwards without accounting page images amount
			var indx = $(idgal+' ul').children('li:visible:last').index();
			if((indx+1) % howMany != 0){
				for(var x = 1; x <= (indx+1) % howMany; x++){
					var bool = true;
					$(idgal+' ul').children('li::visible:first').each(function(){
						if($(this).prev().length > 0){
							$(this).prev().toggle();
							bool2 = true;
						} else {
							bool = false;
						}
					});
					$(idgal+' ul').children('li:visible:last').each(function(){
						if(bool)
							$(this).toggle();
					});
				}
			} else {	
				//Else we move by default amount
				for(var x = 1; x <= howMany2; x++){
					var bool = true;
					$(idgal+' ul').children('li::visible:first').each(function(){
						if($(this).prev().length > 0){
							$(this).prev().toggle();
							bool2 = true;
						} else {
							bool = false;
						}
					});
					$(idgal+' ul').children('li:visible:last').each(function(){
						if(bool)
							$(this).toggle();
					});
				}
			}
			if(controls != undefined && bool2){
				var obj = $('#movie_pager').find("[src='fileadmin/templates/img/dot_act.jpg']");
				obj.attr("src","fileadmin/templates/img/dot.jpg");
				obj.parent().prev().children().attr("src","fileadmin/templates/img/dot_act.jpg");
			}
		});
	}
	
	Scroller('.scrollIt','#gallery_scroller_front .prev','#gallery_scroller_front .next');
	Scroller('#production_carousel','.pager_left a','.pager_right a');
	Scroller('#movie_similar_gallery_div','#pager_left','#pager_right');
	
    $('#gallery_ico_front a').click(function(){
        $('#gallery_photo_front').children("a:nth-child(2)").children("img").attr("src",$(this).attr("rel"));
        $('.gallery_more').attr("id",$(this).attr("class"));
    });
	
	    // search
 
    $("#search_form input").click(function(){
        var radio_val = $(".category:checked").val();
        //alert('jest '+radio_val);
        if(radio_val == "actors") {
            $('#actors_all').removeClass('hidden');
            $('#movies_all').addClass('hidden');
            $('#articles_all').addClass('hidden');
        } else if (radio_val == "movies") {
            $('#actors_all').addClass('hidden');
            $('#movies_all').removeClass('hidden');
            $('#articles_all').addClass('hidden');
        } else if (radio_val == "articles") {
            $('#actors_all').addClass('hidden');
            $('#movies_all').addClass('hidden');
            $('#articles_all').removeClass('hidden');
        }
    });

    $( "#search-text" ).autocomplete({
			source: "index.php?id=26&autocomplete=1",
			minLength: 3
		});




    });



$("#search_form input").ready(function(){
    var radio_val = $(".category:checked").val();
    //alert('jest '+radio_val);
    if(radio_val == "actors") {
        $('#actors_all').removeClass('hidden');
        $('#movies_all').addClass('hidden');
        $('#articles_all').addClass('hidden');
    } else if (radio_val == "movies") {
        $('#actors_all').addClass('hidden');
        $('#movies_all').removeClass('hidden');
        $('#articles_all').addClass('hidden');
    } else if (radio_val == "articles") {
        $('#actors_all').addClass('hidden');
        $('#movies_all').addClass('hidden');
        $('#articles_all').removeClass('hidden');
    }
});


function loadProductionData(element, id, lang){

    $.ajax({
        url: location+'index.php?id=1&our_production=' + id + '&lang=' + lang,
        success: function(data) {
            $('#movie_short_info').html(data);
            $(element).parent().siblings().removeClass('act');

            $(element).parent().addClass('act');
        }
    });
}
function showAfficheImage(id, lang){

$("#lightbox_bg").show();

    $.ajax({
        url: location+'index.php?id=1&affiche=' + id + '&lang=' + lang,
        dataType: 'json',
        success: function(data) {

            $(".lightbox_content_img").html(data['image']);
            $(".lightbox_content_title").html(data['title']);
            $(".lightbox_content_text").html(data['desc']);
            $("#lightbox_bg").show();
            $(".lightbox_content").show('fast',function(){
				var h_w = $(window).height();
				var h_l = $('.lightbox_content').height();
				
				var w_w = $(window).width();
				var w_l = $('.lightbox_content').width();

				var top = (h_w - h_l)/2;
				var left = (w_w - w_l)/2;
				if(top > 0){
					$('.lightbox_content').css('top',top+'px');
				} else {
					$('.lightbox_content').css('top','1px');
				}
				if(left > 0){
					$('.lightbox_content').css('left',left+'px');
				} else {
					$('.lightbox_content').css('left','1px');
				}
			});
        },
        error: function (request, status, error){
            $("#lightbox_bg").hide();
        }
    });
}

function showGalleryImage(id, lang, image){

    $("#lightbox_bg").show();

    $.ajax({
        url: 'http://'+location.hostname+'/index.php?id=1&gallery=' + id + '&lang=' + lang + '&image=' + image,
        dataType: 'json',
        success: function(data) {

            itemsCount = data['count'];
            galleryImages=new Array();
            for(i=0 ; i < itemsCount ; i++){
                galleryImages[i] = data[i];
            }
            currentImage = data['current'];

            $(".lightboxsmall_content_number").html(data['current'] + 1 + '/' + itemsCount);
            $(".lightboxsmall_content_download a").attr('href',data[data['current']]['image_src']);
            $(".lightboxsmall_content_img").html(data[data['current']]['image']);
            $(".lightboxsmall_content_title").html(data[data['current']]['title']);
            $(".lightboxsmall_content_text").html(data[data['current']]['desc']);
            $("#lightbox_bg").show();
            $(".lightboxsmall_content").show('fast',function(){
				var h_w = $(window).height();
				var h_l = $('.lightboxsmall_content').height();
				
				var w_w = $(window).width();
				var w_l = $('.lightboxsmall_content').width();

				var top = (h_w - h_l)/2;
				var left = (w_w - w_l)/2;
				if(top > 0){
					$('.lightboxsmall_content').css('top',top+'px');
				} else {
					$('.lightboxsmall_content').css('top','1px');
				}
				if(left > 0){
					$('.lightboxsmall_content').css('left',left+'px');
				} else {
					$('.lightboxsmall_content').css('left','1px');
				}
			});
        },
        error: function (request, status, error){
            $("#lightbox_bg").hide();
        }
    });
}

function showImage(id){
    itemsCount = galleryImages.length;
    
    $(".lightboxsmall_content_number").html(id + 1 + '/' + itemsCount);
    $(".lightboxsmall_content_download a").attr('href',galleryImages[id]['image_src']);
    $(".lightboxsmall_content_img").html(galleryImages[id]['image']);
    $(".lightboxsmall_content_title").html(galleryImages[id]['title']);
    $(".lightboxsmall_content_text").html(galleryImages[id]['desc']);
	
    currentImage = id;
}


function alertComment(id){
	if(confirm('Etes-vous sur?')){
		document.alert.alertUid.value=id;
		document.alert.submit();
	}
}

function onDailymotionPlayerReady(playerId)
{
  dmplayer = document.getElementById("mydmplayer");
  dmplayer.addEventListener("onStateChange", "ondmplayerStateChange");
}

function ondmplayerStateChange(newState)
{
   if(newState=="1" || newState=="3"){
		clearTimeout(fiche_timed);
   }
}


