/**
 * 
 */

var gallery_url = '/media/files/photos/';

function preloadImg(src){
	var cimg = document.createElement('img');
	cimg.src = el.attr('href');
	return cimg;
}

function mainMenuRollover(){
	$("ul.header-menu li img:not([src$=hl.png])").hover(function() {
			var re = /(\w+).png/g;
			$(this).attr("src", 
				$(this).attr("src").replace(re, "$1_hl.png")
			);
		} , function() {
			var re = /([a-z]+)_hl.png/g;
			$(this).attr("src", 
				$(this).attr("src").replace(re, "$1.png")
			);
	});
	
	$('#main-logo').click(function(){ window.location.href = '/'; });
}

function loadSlide(el){
	//show 'loading' overlay
	$('#gallery-slide-loading').fadeTo('slow', 0.5,function(){});
	
	//preload slide
	var cimg = document.createElement('img');
	cimg.src = gallery_url+'middle-'+el.attr('href');
	$('#preloader').append(cimg);
	
	//on slide loaded
	$(cimg).bind('load', function(){
		//view slide
		$('#gallery-slide').css('background-image', 'url('+gallery_url+'middle-'+el.attr('href')+')');
		
		//bind colorbox to slide
		$('#gallery-slide-zoom').unbind();
		$('#gallery-slide-zoom').click(function(){
			$.colorbox({
				href: gallery_url+'big-'+el.attr('href'),
				title: el.attr('title')
			});
		});
		
		//do we have comment overlay on that?
		if(el.attr('comment') != ""){
			$("#gallery-slide-overlay-content").html(el.attr('comment'));
			$("#gallery-slide-overlay").show('slow');
		} else {
			$("#gallery-slide-overlay").hide('slow');
		}
		
		//fade out 'loading' overlay
		$('#gallery-slide-loading').fadeOut('slow', function(){});
	});
	
}

function bindSlideHandlers(){
	$("#carousel a").unbind();
	
	$("#carousel a").click(function(event){
		event.preventDefault();
		loadSlide($(this));
    });
}

function afterItemLoadCallback(carousel, idx, current_pic_id){
	//if(idx == 0){
    	loadSlide($("#carousel a:first"));
   // } else {
    	//carousel.scroll(parseInt(idx), false);
    	//loadSlide($('#carousel a[id="'+parseInt(current_pic_id)+'"]'));
    	
   // }

	bindSlideHandlers();
}

function galleryCarouselInit(mycarousel_itemList){
	
	
	
	
	function mycarousel_itemLoadCallback(carousel, state){
		var current_pic_id = 0;
		var idx = 0;
		
		//set current album id from the menu
		var current_album_id = parseInt($("#gallery-footer-categories a.selected").attr('href'));
		
		//specific picture is requested, override album_id
		for(var j=0, k=0; j < mycarousel_itemList.length; j++){
			if(mycarousel_itemList[j].selected == "1"){
				current_album_id = mycarousel_itemList[j].album_id;
				current_pic_id = mycarousel_itemList[j].picture_id;
				$("#gallery-footer-categories a").removeClass('selected');
				$("#gallery-footer-categories a[href="+current_album_id+"]").addClass("selected");
			}
		}
		
		var c_list = Array();
		
		for(var j=0, k=0; j < mycarousel_itemList.length; j++){
			//build array with pics from specifed album
			if(mycarousel_itemList[j].album_id == current_album_id){
				c_list[k++] = mycarousel_itemList[j];
				
			}
			
			//current pic? remember carousel index (idx) and reset flag
			if(mycarousel_itemList[j].selected == "1"){
				idx = k-1;
				mycarousel_itemList[j].selected = 0;
			}
		}
		
		
		
		
		carousel.size( c_list.length );
		

		
	    for (var i = carousel.first; i <= carousel.last; i++) {
	        if (carousel.has(i)) {
	            continue;
	        }

	        if (i > c_list.length) {
	            break;
	        }
	        
	        
	        carousel.add(i, mycarousel_getItemHTML(c_list[carousel.index(i)-1]));
	    }


	    
	    $("#idx").attr('value', idx);
	    $("#current_pic_id").attr('value', current_pic_id);
	    
	};

	
	
	function mycarousel_getItemHTML(item)
	{
		//alert(item.url);
		return '<a comment="' + item.comment + '" id="' + item.picture_id + '" href="' + item.url + '" title="' + item.title + '"><div style="background-image: url('+gallery_url+'small-' + item.url + '); "></div></a>';
	};
	                       
	function mycarousel_initCallback(carousel) {
	    $('#gallery-carousel-right').bind('click', function() {
	        carousel.next();
	        bindSlideHandlers();
	        return false;
	    });

	    $('#gallery-carousel-left').bind('click', function() {
	        carousel.prev();
	        bindSlideHandlers();
	        return false;
	    });
	    
	    $("#carousel").bind('mousewheel', function(event, delta){
	    	if(delta > 0){
	    		carousel.prev();
		        bindSlideHandlers();
	    	} else {
	    		carousel.next();
		        bindSlideHandlers();
	    	}
	    });
	    
	    
	};
	
	
	
	$("#carousel").jcarousel({
		wrap: 'circular',
        
        initCallback: mycarousel_initCallback,
    
        itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback},
        
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
	//load current pic with given carousel position index (idx) and picture ID
	afterItemLoadCallback($("#carousel").data('jcarousel'),  
							$("#idx").attr('value'), 
							$("#current_pic_id").attr('value')
						);
	
	bindSlideHandlers();
	
	$('#gallery-slide-loading').fadeOut('slow', function(){});
}

function footerZoom(){
	$("ul.thumb li").hover(function() {
		$(this).css({'z-index' : '10'}); /*Add a higher z-index value so this image stays on top*/ 
		$(this).find('div').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
			.animate({
				marginTop: '-89px', /* The next 4 lines will vertically align this image */ 
				marginLeft: '-32px',
				top: '50%',
				left: '50%',
				width: '110px', /* Set new width */
				height: '110px', /* Set new height */
				padding: '0px'
			}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */
	
		} , function() {
		$(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
		$(this).find('div').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
			.animate({
				marginTop: '0', /* Set alignment back to default */
				marginLeft: '0',
				top: '0',
				left: '0',
				width: '52px', /* Set width back to default */
				height: '52px', /* Set height back to default */
				padding: '0px'
			}, 400);
	});
}
