// ** //
function adjustSlideText() {
    var first = true;
	var state = "start";
	var animateTopBox = false;
	var animateTopBox = false;

    $(".top_slide_text").each(function() {
		
        var obj = $(this);
        var top_slide_down_position = 496 - obj.outerHeight();
        var top_slide_y = top_slide_down_position;

        var children = obj.find("div");
        if (children.length > 0) {
            top_slide_down_position = 464 - ($(children[0]).height()+$(children[1]).height()+$(children[2]).height());
        }

        obj.css({ 'top': top_slide_y });
        obj.parent().attr('maxtop', top_slide_y + "px");
        obj.parent().attr('mintop', top_slide_down_position + "px");

        //top slide up and down the text on hover
    

		obj.bind("mouseenter",function(evt) {
			//console.log( "*** parent::mouseover ***" );
			obj.stop().animate({'top':top_slide_y}, 500, function(){
			});
		});

		obj.bind("mouseleave",function(evt) {
			//console.log( "*** parent::mouseleave ***" );
			obj.stop().animate({'top':top_slide_down_position}, 500, function(){
			});
		});
		
        // make sure first details box drops after a set amount of time

        if (production_drop_delay > 0&&obj.parent().attr("identifier")=="id_1") {
          	obj.delay(production_drop_delay).animate({ 'top': top_slide_down_position }, 500);
       }

       obj.css({ 'visibility': 'visible' });

        first = false;
    });
}


// ** //
function doCanScroll(o) {
    return ($("#video").css('visibility') == 'hidden');
}

jQuery(document).ready(function() {

    $("#dimmer").css("opacity", 0);

    $(".bottom_carousel").jCarouselLite({
        auto: (carousel_length > 4 ? carousel_autoscroll : 0),
        visible: 4,
        speed: 1000,
        btnNext: "#bottom_carousel_next",
        btnPrev: "#bottom_carousel_prev",
        canScroll: doCanScroll
    });

    if (carousel_length <= 4) {
        $("#bottom_carousel_prev").css("visibility", "hidden");
        $("#bottom_carousel_next").css("visibility", "hidden");
    }

    if (notifications_length == 0) {
        var cch = $(".cast_change").outerHeight();
        $(".cast_change").css('display', 'none');
        var h = $("#dimmer").height();
        $("#dimmer").css("height", (h - cch) + "px");
    }

    $("#top_slide ul li:first").attr("active", "1");

    $("#top_slide").jCarouselLite({
        auto: production_autoscroll,
        speed: 1500,
        visible: 1,
        btnNext: ".top_slide_next",
        btnPrev: ".top_slide_prev",
        canScroll: doCanScroll,
        beforeStart: function(v) {
            var search = $(v);
            $(v).parent().find("li").each(function() {
                var o = $(this);
                var active = parseInt(o.attr("active"));
                var t = $(this).attr("maxtop");
                var b = $(this).attr("mintop");
                if (active == 0 && production_drop_delay > 0) o.find(".top_slide_text").css("top", t);
            })
        },
        afterEnd: function(v) {
            var search = $(v);
            $(v).parent().find("li").each(function() {
                var o = $(this);
                var active = parseInt(o.attr("active"));
                var t = $(this).attr("maxtop");
                var b = $(this).attr("mintop");
                if (active == 1 && production_drop_delay > 0) o.find(".top_slide_text").css("top", t).delay(production_drop_delay).animate({ 'top': b }, 500);
                else o.find(".top_slide_text").css("top", t);
            });
        }

    });

    $('#close').click(function() {
        $('.top_container').slideUp("fast");
        return false; //stop the link on actual html working
    });

    if (notifications_length > 1) {
        $("#cast_change").jCarouselLite({
            auto: notifications_autoscroll,
            speed: 1500,
            vertical: true,
            canScroll: doCanScroll,
            circular: true,
            visible: 1
        });
    }

    doRightStill();
    doVideo();
    adjustSlideText();
    $(document).pngFix();
});

// ** //
function doRightStill() {
    $("#right_still").click(function() {
        if (whatson_action.length>0)window.location.href = whatson_action;
    });

    var obj = $("#right_text");
    var h = obj.outerHeight();

    if (jQuery.browser.version == '6.0') h += 6;
    obj.css("top", $("#right_still").height() - h );
    
}

// ** //
function doDimmer( on ) {
    $("#dimmer").css("visibility","visible").animate({
        opacity: (on ? 0.75 : 0)
    }, 500, function() {
        if (!on) $("#dimmer").css("visibility", "hidden" );
    });
}

// ** //
function doVideo(){
    $("#right_video").click(function(){
        doDimmer(true);
        $("#video").css('visibility', 'visible');
        $('#video').animate({width:"452px",height:"254px",left:"541px",top:"240px"}, 500, 'linear', function(){
            $("#video").html("<object id='fx' type='application/x-shockwave-flash' data='./home/System/swf/videoplayer.swf' width='452' height='254'><param name='flashvars' value='assetPath="+video_src+"' /><param name='movie' value='./home/System/swf/videoplayer.swf' /><param name='quality' value='high' /><param name='allowFullScreen' value='true' /><param name='wmode' value='window' /><param name='allowScriptAccess' value='always' /></object>");
        });
    });
};

// ** //
function closeVideo() {
	$("#video").html("");
    $('#video').animate({        
    	width: "1px", height: "1px", "left": "995px", "top": "480px"
    }, 500, 'linear', function() {      
     
    $("#video").css('visibility', 'hidden');
    doDimmer(false);  
    });
};

function adjustHeight(objectParent) 
{ 

	var max_height;
	
	var cnt;
	
	var current_height;
	
	objectParent.each( function(){
	
		
         max_height = parseInt($(this).height()); 
	
		// cnt = 0;
		
		 current_height=0;

		$(this).children('div').each(function(){

		//cnt++;
		//console.log("child count=" + cnt);
		
		current_height+=parseInt($(this).height());
		
	
		    if ( current_height > max_height) 
				{ 

				$(this).css('visibility', 'hidden');

				} 
		
		});
	});
    
} 
