/**
 * @author florent global variables
 */
$(document).ready(function(){

    // we declare the javascript activated!! party on! :-)
    $("body").addClass("jsOn");
    
    // sort list of items (now on shop) : when the select changes, the list is updated
    $('.sort input[type="submit"]').hide();
    $('.sort select').change(function(){
        $('.sort form').submit();
    });
    
    // init search
    $('#search input.text').focus(function(){
        if ($(this).attr('value') == searchFieldDefault) {
            $(this).attr('value', '');
        }
    });

    /*
     *  whole link process
     *  subpages, blog items, gallery template, gallery page etc
     */
    $('.items .item').css({
        'cursor': 'pointer'
    }).click(function(){
    	if ($(this).find('a').length>0) {
    		window.location = $(this).find('a:first').attr('href');
    	}
    }).hover(function(){
        $(this).addClass('selected');
    }, function(){
        $(this).removeClass('selected');
    });
    
    /* video teaser */
    $('.video.splash a').append('<span class="marker" />');
    $('.video .warning').hide();

    // INIT LIGHTBOX : fancybox.net
    // we must change the href of the images of the gallery (mode display gallery) because they lead to the pages for non javascript users. 
    for (var i =0; i<$('.gallery2Plugin .images .lightbox').length; i++) {
    	var lightboxHref = '/files/_galleries'+$(".gallery2Plugin .images .lightbox").eq(i).attr('href');
    	$(".gallery2Plugin .images .lightbox").eq(i).attr('href', lightboxHref);
    }
    initLightbox();
});

function initLightbox() {
    // default case : images 
    $(".lightbox:not(.video)").attr('rel', 'main').fancybox({
    	"titlePosition"		: "inside", 
    	"overlayColor"		: "black", 
    	"overlayOpacity"	: 0.2, 
     	"showNavArrows"		: false, 
     	"titleFormat"		: lightBoxTitle
    });
    // case : videos 
    for (var i =0; i<$('.lightbox.video').length; i++) {
        // first we change the original link (to the flv video) to the player (flowplayer)
    	if ($('.lightbox.video').eq(i).attr('href').indexOf('flowplayer/flowplayer-3.1.5.swf')<0) $('.lightbox.video').eq(i).attr('href',pathTheme+"flowplayer/flowplayer-3.1.5.swf?&config={'clip':'"+webroot.substring(0,webroot.length-1)+$('.lightbox.video').eq(i).attr('href')+"','plugins':{'controls':{'url':'flowplayer.controls-3.1.5.swf'}}}");
        // then we call lightbox on these
    	$('.lightbox.video').eq(i).attr('rel', 'main').fancybox({
        	"titlePosition"		: "inside",
        	"overlayColor"		: "black", 
        	"overlayOpacity"	: 0.2, 
         	"showNavArrows"		: false, 
         	"titleFormat"		: lightBoxTitle, 
         	"type"				: "swf",
    		"swf"				: {
    		   	"wmode"				: "window", // pour ie :-( 
    			"allowfullscreen"	: true
    		}
        });
    }	
    // external links : images 
    $(".lightbox.external").attr('rel', 'main').fancybox({
    	"titlePosition"		: "inside", 
    	"overlayColor"		: "black", 
    	"overlayOpacity"	: 0.2, 
     	"showNavArrows"		: false, 
     	"titleFormat"		: lightBoxTitle,     	
     	'width' 			: '75%',
     	'height' 			: '75%',
     	'autoScale' 		: false,
     	'type' 				: 'iframe' 
    });
}

function lightBoxTitle(title, currentArray, currentIndex, currentOpts) {
	var titleStr = '';
	if (currentIndex > 0) titleStr += '<a class="previous" href="javascript:;" onclick="$.fancybox.prev();"><img src="'+pathTheme+'fancybox/fancy_nav_left.png" /></a>';
	if (title && title.length) titleStr += '<span class="title">'+title+'</span>';		
	if (currentIndex < currentArray.length-1) titleStr += '<a class="next" href="javascript:;" onclick="$.fancybox.next();"><img src="'+pathTheme+'fancybox/fancy_nav_right.png" /></a>';
    return titleStr; 
}

