$(document).ready(function(){
	var formPhotobookInitiateOptions = {success: photobook_initiate};
	var formPhotobookAddImageOptions = {success: photobook_add_image};
	$('.jcarousel-skin-tango').addClass('gallery_show'); // adds new class name to maintain degradability            
	$('ul.gallery_show').galleria({
                        history   : true, // activates the history object for bookmarking, back-button etc.
                        clickNext : true, // helper for making the image clickable
                        insert    : '#main_image', // the containing selector for our main image
                        onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
                                
                                
                                // add slider
                                photobook_panel_close();                               
                                
                                // fade in the image & caption
                                image.css('display','none').fadeIn(200);
                                image.attr('class','cbb');
                                caption.css('display','none').fadeIn(200);
                                
                                // fetch the thumbnail container
                                var _li = thumb.parents('li');
                                
                                // fade out inactive thumbnail
                                _li.siblings().children('img.selected').fadeTo(100,0.6);
                                
                                // fade in active thumbnail
                                thumb.fadeTo('fast',1).addClass('selected');
                                
                                // add a title for the clickable image
                                image.attr('title',thumb.attr('shortdesc'));
                                
                                // update the current image id in form var
                                $('#current_id_photo').val(thumb.attr('alt'));
                                
                                $('#main_image').trigger('img_change');

                        },
                        onThumb : function(thumb) { // thumbnail effects goes here
                                
                                // fetch the thumbnail container
                                var _li = thumb.parents('li');
                                
                                // if thumbnail is active, fade all the way.
                                var _fadeTo = _li.is('.active') ? '1' : '0.6';
                                
                                // fade in the thumbnail when finished loading
                                thumb.css({display:'none',opacity:_fadeTo}).fadeIn(500);
                                
                                // hover effects
                                thumb.hover(
                                        function() { thumb.fadeTo('fast',1); },
                                        function() { _li.not('.active').children('img').fadeTo('fast',0.6); } // don't fade out if the parent is active
                                )
                        }
  });            
	jQuery('#mycarousel').jcarousel({
                scroll: 9,
                initCallback: mycarousel_initCallback
	});
            
	$.hotkeys.add('left', function(){$.galleria.prev();});
	$.hotkeys.add('right', function(){$.galleria.next();});
	$(function() { 			
			var active = false;
			var gal = jQuery('#gallerytitle');
			gal.find('.start').css("cursor", "pointer").click(function() {
			        if (!active) {
			                active = !active;
			                $.galleria.next();
			                gal.everyTime('5s', 'slideshow', function() {
			                        $.galleria.next();
			                });
			        }
			});
			gal.find('.stop').css("cursor", "pointer").click(function() {
			        if (active) {
			                active = !active;
			                gal.stopTime('slideshow');
			        }
			});
	});


	$('.boxgrid.slidedown').hover(function(){
	  $(".cover", this).stop().animate({top:'-260px'},{queue:false,duration:300});
	}, function() {
	  $(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300});
	});
 
	var pic_real_width;
	var pic_real_height;
});

function photobook_initiate(data){
	var oData=$.evalJSON(data);
	if(oData.success=='false'){
		$('.boxgrid .cover #status').html(oData.message);
	}else{
		$('.boxgrid .cover').html(oData.message);
		photobook_panel_close();
	}
}
function photobook_add_image(data){
    	var oData=$.evalJSON(data);
    	$('.boxgrid .cover').html(oData.message);
    	photobook_panel_close();
} 
function photobook_complete(data){
    	var oData=$.evalJSON(data);
    	photobook_panel_close();
    	delete_cookie('id_photobook');
    	$('.boxgrid .cover').html(oData.message);
}
function submit_photobook(){
	var id_photo=$('#current_id_photo').val();
	var name = $('.boxgrid #name').val();
	var agency=$('.boxgrid #agency').val();
	var address=$('.boxgrid #address').val();
	var phone=$('.boxgrid #phone').val();
	var email=$('.boxgrid #email').val();
	$.post(site_url+'public_side/ajax_save_request',{id_photo:id_photo,name:name,agency:agency,address:address,phone:phone,email:email},photobook_initiate);
}
function submit_another_photo(){
	var id_photo=$('#current_id_photo').val();
	$.post(site_url+'public_side/ajax_save_another_request',{id_photo:id_photo},photobook_add_image);
}
function submit_end_photobook(){
	$.post(site_url+'public_side/ajax_end_photobook',{cond:'end',id_photo:id_photo},photobook_complete);
}
function mycarousel_initCallback(carousel) {
	jQuery('#main_image').bind('img_change',function() {
		var num = parseInt((jQuery('.caption').text()).split(":",1)[0])-1;
		var width;
		var height;
		var id_photobook = get_cookie('id_photobook');
		
		var session=false;
		if(id_photobook != null){
		  session=true;
		  //alert(id_photobook);
		}else{
		  session=false;
		}
		
		carousel.scroll(num);
		cbb.init();
		// get img size as we will set the containing div for slider
		$('.galleria_container').hide();
		$('img.i3').load(function() {
		  pic_width = this.width;
		  pic_height = this.height;
		  $('.boxgrid').css('width',pic_width);
		  $('.boxgrid').css('height',pic_height);
		});
		$('.i3').wrap('<div class="boxgrid captionfull"></div>');
		var form="";
		if(!session){
		  form = $('#request_image').html();
		}else{
		  form = $('#request_image_add').html();
		}
		$('.i3').after(form);				
		
		$('.galleria_container').fadeIn(200);
		if(session){
		  $('.boxgrid.captionfull').hover(function(){
		  	$(".cover", this).stop().animate({bottom:'0px'},{queue:false,duration:500});
		  }, function() {
		  	$(".cover", this).stop().animate({bottom:'-200px'},{queue:false,duration:500});
		  	//$(".cover", this).stop().animate({bottom:'0px'},{queue:false,duration:160});
		  });
		}
		
		  //return false;
	});
}		
function photobook_panel(){
  if ($('.request_images').hasClass('selected')){
  	photobook_panel_close();		  
  }else{
  	photobook_panel_open();
  }
}
function photobook_panel_open(){
  $('.request_images').addClass('selected');
  $(".boxgrid .cover").stop().animate({bottom:'0px'},{queue:false,duration:500});
}
function photobook_panel_close(){
  $('.request_images').removeClass('selected');
	$(".boxgrid .cover").stop().animate({bottom:'-200px'},{queue:false,duration:500});
}

function get_cookie ( cookie_name ){
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
  if ( results )
  	return ( unescape ( results[2] ) );
  else
  	return null;
}
function delete_cookie ( cookie_name ){
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

