$(document).ready(function() {
	 
	 initpage();
 
});

function initpage(){

	//hide anything with class=hidden
	$('.hidden').hide();
	
	$('.alert').hide();
	
	$('.success').hide();
	
	$('.alert').fadeIn();
	
	$('.success').fadeIn();
	
	//mark all facebox links as such
	$('a[rel*=facebox]').facebox();
	
	if($('div#image-pool').length > 0) initdrags();


	if($('div#gallery').length > 0){

		Galleria.loadTheme('/js/galleria/themes/classic/galleria.classic.js');
		
		$('#gallery').galleria(
		{
			extend: function(options) {


				// listen to when an image is shown
				this.bind(Galleria.IMAGE, function(e) {


					// lets make galleria open a lightbox when clicking the main image:
					$(e.imageTarget).click(this.proxy(function() {
					   this.openLightbox();
					}));
				});
			}
		});
	
	}

	if($('textarea#copy').length > 0){
	
		$( 'textarea#copy' ).ckeditor( function()
		{
			CKFinder.setupCKEditor('/js/ckfinder/') ;
		});
	
	}


	
	if($('#navigation').length > 0){
		sfHover('navigation');
	}
	if($('#sub-navigation').length > 0){
		sfHover('sub-navigation');
	}
	

	
	$('ul#navigation').superfish({ 
            delay:       600,                            // one second delay on mouseout 
            animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
            speed:       'fast',                          // faster animation speed 
            autoArrows:  true,
			pathClass:  'current' 
     });
	 
	 $('ul#sub-navigation').superfish({ 
            delay:       800,                            // one second delay on mouseout 
            animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
            speed:       'fast',                          // faster animation speed 
            autoArrows:  false                           // disable generation of arrow mark-up 
     });
	
	var adminTab = {
                
        speed:300,
        containerWidth:$('.admin-nav').outerWidth(),
        containerHeight:$('.admin-nav').outerHeight(),
        tabWidth:$('.admin-tab').outerWidth(),
                
        init:function(){
            
			$('.admin-nav').css('height',adminTab.containerHeight + 'px');
                    
            $('a.admin-tab').click(function(event){

                if ($('.admin-nav').hasClass('open')) {
					
					$('.admin-nav').animate({left:'-' + adminTab.containerWidth}, adminTab.speed)
                            .removeClass('open');
                        } else {
                            $('.admin-nav').animate({left:'0'},  adminTab.speed)
                            .addClass('open');
                        }
                        event.preventDefault();
                    });
                }
            };
     
    adminTab.init();
	
	if($('#slide-list').length > 0){
		$("#slide-list").sortable({
		  handle : '.handle',
		  update : function () {
			var order = $('#slide-list').sortable('serialize');
			$("#update-info").load("process/slides-sortable.php?"+order);
		  }
		});
	}
	if($('#image-list').length > 0){
		$("#image-list").sortable({
		  handle : '.handle',
		  update : function () {
			var order = $('#image-list').sortable('serialize');
			$("#update-info").load("process/images-sortable.php?gallery_id=" + gallery_id + "&"+order);
		  }
		});
	}

	

}

initdrags = function() {

	 $(".drag").draggable({ helper: 'clone' });
	 
	 $(".drag-out").draggable({ helper: 'original' });
	 
	 $("#admin-gallery").droppable({
            accept: ".drag",
            drop: function (ev, ui) {
				var droppedItem = $(ui.draggable).clone();
				var img_id = droppedItem.attr("id");
				$('#admin-gallery').load("process/add-image-to-gallery.php?gallery_id=" + gallery_id + "&img_id=" + img_id,initdrags);
            }
       });
	   
	 $("#image-pool").droppable({
            accept: ".drag-out",
            drop: function (ev, ui) {
				var droppedItem = $(ui.draggable);
				var img_id = droppedItem.attr("id");
				$(ui.draggable).remove();
				$('#admin-gallery').load("process/delete-image-from-gallery.php?gallery_id=" + gallery_id + "&img_id=" + img_id,initdrags);
                }
      });
}

sfHover = function(el) {
	
		var sfEls = document.getElementById(el).getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	
}
