$(document).ready(function(){
    $('.pngfix').pngFix();

    //links carousel:
    if($('#links-carousel').length)
        $('#links-carousel').jcarousel({scroll: 1,auto: 3,
        wrap: 'last'});
         
    //maison carousel:
    if($('#maison-carousel').length)
        $('#maison-carousel').jcarousel({
            scroll: 1,
            auto: 4,
            wrap: 'last',
            initCallback: maisonInitCallback,
            itemVisibleInCallback:  {
                onBeforeAnimation: maisonItemCallback,
                onAfterAnimation: null            
            },
            buttonNextHTML: null,
            buttonPrevHTML: null 
        }); 
    
    //switching videos on home page:    
    videoBox();
    
    //scroll bar in videos box:
    $('.videos-box .videos-list').jScrollPane({showArrows: true});
    
    //events carousel:
    if($('#events-carousel').length) 
        $('#events-carousel').jcarousel({
            scroll: 1,
            itemLastInCallback: eventsLastInCallback,
            itemLastOutCallback: eventsLastOutCallback
        });  
    if($('#events-carousel-side').length) {
        $('#events-carousel-side').jcarousel({
            scroll: 1,
			visible: 1,
            itemLastInCallback: eventsLastInCallback,
            itemLastOutCallback: eventsLastOutCallback
        });	
	}
    //tabs:
    tabs();      

});


function maisonInitCallback(carousel) {
    $('#maison-carousel .jcarousel-control a').bind('click', function() {
        carousel.scroll($.jcarousel.intval($(this).attr('href').substr($(this).attr('href').indexOf('-') + 1)));
        return false;
    });
};

function maisonItemCallback(carousel, li, id, state) {
    $('#maison-carousel .jcarousel-control a').removeClass('active').eq(id-1).addClass('active');
};  

function eventsLastInCallback(carousel, li, id, state) {
    $(li).addClass('last-visible');
}

function eventsLastOutCallback(carousel, li, id, state) {
    $(li).removeClass('last-visible');
}
function videoBox() {
    var videoLinks = $('.videos-box .videos-list li a');
    if(videoLinks.length) {
        var flashvars = {
            flv: videoLinks.filter('.active').attr('href'),
            width: "200",
            height: "150",
            margin: "0",
            loadingcolor: "cccccc",
            buttonovercolor: "cccccc",
			startimage: $($('img',videoLinks.filter('.active')).get(0)).attr('alt')
        };
        var params = {
            allowScriptAccess: "always",
            allowFullScreen: "true",
            wmode: "transparent",
			movie: false
        };                    
    	if($(videoLinks.filter('.active').get(0)).hasClass('swf-mode')) { 
			var firstItem=videoLinks.filter('.active').get(0);
			params.movie=$(firstItem).attr('href');
			swfobject.embedSWF($(firstItem).attr('href'), "video-player", "200", "150", "9.0.0", "", flashvars, params);	
		}                                 
		else{
        swfobject.embedSWF(EZ_DESIGN_ROOT+"flash/player_flv.swf", "video-player", "200", "150", "9.0.0", "", flashvars, params);
         }
        videoLinks.click(function() {
            if(flashvars.flv.indexOf($(this).attr('href')) == -1) {
                videoLinks.removeClass('active');
                $(this).addClass('active');
                $('#video-player').get(0).SetVariable("player:jsStop", ""); 
                flashvars.flv = $(this).attr('href'); 
				if($(this).hasClass('swf-mode')) {
					params.movie=$(this).attr('href');
					swfobject.embedSWF($(this).attr('href'), "video-player", "200", "150", "9.0.0", "", flashvars, params);
				}        
				else{
	                swfobject.embedSWF(EZ_DESIGN_ROOT+"flash/player_flv.swf", "video-player", "200", "150", "9.0.0", "", flashvars, params);
			    }
                setTimeout(function() {$('#video-player').get(0).SetVariable("player:jsPlay", "");}, 500);
            }                        
            return false;
        });
    }
}

function tabs() {
    if($('.tabs-nav').length) {
        $('.tabs-nav li').click(function(event){
            $(this).addClass('active').siblings('li').removeClass('active');
            var activeDivHref = $(this).find('a').attr('href');
            $(this).parent().nextAll('.tab-body').removeClass('active').filter(activeDivHref.substr(activeDivHref.indexOf('#'))).addClass('active');
            event.preventDefault();
        });
    }
}

