rel_to_array = function(tar) {
   var imgs = $(tar).attr('rel')
   var arr = imgs.split(',')
   return arr
};

function CampDenali() {};
   
CampDenali.prototype = {
    init: function(){
        // All of the cosmetic markup
	       $('#container').before('<div id="header_band"><div id="header_bend"></div></div>')
	       $('#container').before('<div id="gallery_band"></div>')
	       $('#container').append('<div class="push"></div>')
	       //$('#footer').after('<div id="mountain_bg"><div id="mountain"></div></div>')
	       
	       // Navigation
	       $('a.nav_button > span').click(function(){
	           campdenali.toggle_nav($(this).parent('a').next('ul'), $(this).parent('a').parent('li'))
	           return false;
	       });
	       
	       $('a.nav_button > span').mouseover(function(){
	           $(this).css('background-position', '0 0')
	       })
	       
	       $('a.nav_button > span').mouseout(function(){
   	           $(this).css('background-position', '-20px 0')
   	       })
	       
	       $('body').click(function(){
	           if($('.hovering').length == 0){
	               return true;// we don't want to disable rolled over navigation on body click since the navigation doesn't cover up anything
	               $('.showing').toggle().toggleClass('showing')
       	           $('.on_page').toggle().addClass('showing')
   	               $('.active').removeClass('active');
   	               $('.on_page').parent('li').addClass('active');
   	           }
             
	       })
	       
	       $("#search a").click(function(){
               // $("#search_form").toggle('normal')
               if($("#search_form").css("display") == "none") {
                  $("#search_form").css({display:"block"}).animate({width:"240px"}, "fast")
               } else {
                   $("#search_form").animate({width:0}, "fast").hide("fast")
               }
               
	           return false;
	       })
	       
	       $('.active > ul').addClass('showing').addClass('on_page')
           
	       if($('.blog_post').length == 0) { // if not on blog page
	           $('#third_nav > ul').children('li').children('a').append("<span class='third_arrow right'></span>");
	       }
	       
	       campdenali.determine_blog_size();
	       campdenali.check_window();
	       
	       $(window).resize(function(){
	           campdenali.check_window();
	       });
	       
	       $('#search_field').focus(function(){
	           $(this).css('background-image','none')
	       })
	       
	       $('#search_field').blur(function(){
   	           $(this).css('background-image','url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif)')
   	       })
    },
    
    check_window: function() {
           if($('body').innerWidth() < $('#mountain').width()) {
                  var width = $('body').innerWidth()
                  $('#mountain').css('width',width)
               } else if($('body').innerWidth() > $('#mountain').width()) {
                   $('#mountain').css('width','1404px')
               }
            
           if($('body').innerWidth() < $('#header_bend').width()) {
                     var width = $('body').innerWidth()
                     $('#header_bend').css('width',width)
                     $('#header_bend').css('margin-left',-width/2)
                  } else if($('body').innerWidth() > $('#header_bend').width()) {
                      $('#header_bend').css('width','1166px')
                  }
    },
    
    determine_blog_size: function() {
           var image = $('#blog_content').find('img')
           if(image.length == 0) {
               $('#blog_content').addClass('mini')
           } else {
               if (image.width() > 400) {
   	               $('#blog_content').addClass('big')
   	           } else {
   	               $('#blog_content').addClass('small')
   	           }
           }
           
    },
       
    toggle_nav: function(tar, parent_li) {
       if($(tar).hasClass('showing')) {
            $(tar).toggle().toggleClass('showing')
            $('.on_page').toggle().addClass('showing')
            $('.on_page').parent('li').toggleClass('active');
       } else {
            $('.showing').toggle().toggleClass('showing')
            $(tar).toggle().toggleClass('showing')
            $('.active').toggleClass('active')
       }
       $(parent_li).toggleClass('active')
   }
}

function Gallery() {};

Gallery.prototype = {
   bg: "<div id='gallery_bg'></div>",
   
   gallery_wrap: '<div id="jquery-lightbox" style="display:none;"><div id="lightbox-container-image-box"><div id="lightbox-container-image"><img id="lightbox-image"><div id="lightbox-loading"><a href="#" id="lightbox-loading-link"><img src="/images/sites/den/lightbox/lightbox-ico-loading.gif"></a></div></div></div><div id="lightbox-container-image-data-box"><div id="lightbox-container-image-data"><div id="lightbox-image-details"><span id="lightbox-image-details-caption"></span><span id="lightbox-image-details-currentNumber"></span></div><div id="lightbox-secNav"><a href="#" id="lightbox-secNav-btnClose"><img src="/images/sites/den/lightbox/lightbox-btn-close.gif"></a></div></div></div></div>',
   
   gallery_thumbs: "<div id='gallery_thumbs'></div>",
   thumbs_go_here: "<div id='thumbs_wrapper'></div>",
   current_image: "",
   i : 10,
   interval : 70,
   next_thumb: "",
   prev_thumb: "",
   
   init: function() {
       
       $('body').append(gallery.bg)
       $('#gallery_bg').after(gallery.gallery_wrap)
       $('#jquery-lightbox').after(gallery.gallery_thumbs)
       $('#gallery_thumbs').after(gallery.thumbs_go_here)
       $('#thumbs_wrapper').after('<div id="left_mini_control" class="mini_dock_control"></div> <div id="right_mini_control" class="mini_dock_control"></div>');
       $('#gallery_bg').after('<div class="dock_toggle hide_dock"></div><div class="dock_toggle show_dock"></div><div id="close_button"></div>')
       $('#close_button').after('<div id="big_nav_left" class="gallery_big_control"></div><div id="big_nav_right" class="gallery_big_control"></div>')
       
       
      
       $('#jquery-lightbox').css({top: $('body').innerHeight()/50, left:0})
      
      $('#thumbs_wrapper').css("width", 120*($("#photogallery ul").find('img').length))
      
       $("#photogallery ul").find('img').each(function(){
           $('#thumbs_wrapper').append("<div class='thumb'  rel='"+$(this).attr('rel')+"'><div class='gal_thumb_inner' rel='"+$(this).attr('alt')+"'>&nbsp;</div></div>")
       });
       
       $('.thumb').click(function(){
           $('.thumb').removeClass('active_thumb')
           $(this).addClass("active_thumb")
           gallery.current_image = $(this)
           gallery.set_image_to_view($(this).attr('rel'));
           $('#jquery-lightbox').show();
           return false;
       })
       
       $('#gallery_bg, #jquery-lightbox, #close_button').click(function(){
           gallery.close();
       });
       
       $(window).scroll(function(){
           gallery.reposition_gallery();
           });

       $(window).resize(function(){
           gallery.reposition_gallery();
           
           });
       
       $('#photogallery > a').click(function(){
           gallery.show()
           return false;
       });
       
       $('#left_mini_control').mousedown(function(){
           gallery.moving_right = setInterval("gallery.move_right("+gallery.i+")", gallery.interval);
       });
       
       $('#right_mini_control').mousedown(function(){
           gallery.moving_left = setInterval("gallery.move_left("+gallery.i+")", gallery.interval);
       });
       
       $('#left_mini_control').mouseup(function(){
           gallery.moving_right = clearInterval(gallery.moving_right);
           if ( gallery.get_cur_x() > 0) {
                 $('#thumbs_wrapper').animate({"left": 0+"px"});
             }
           gallery.i = 10;
       });
       
       $('#right_mini_control').mouseup(function(){
           gallery.moving_left = clearInterval(gallery.moving_left);
           gallery.i = 10;
       });
       
       $('.hide_dock').click(function(){
           gallery.hide_dock();
           $(this).toggle()
           $('.show_dock').toggle()
           gallery.get_active()
           $('#big_nav_left, #big_nav_right').show()
       })
       
       $('.show_dock').click(function(){
              gallery.show_dock();
              $(this).toggle()
              $('.hide_dock').toggle()
              $('#big_nav_left, #big_nav_right').hide()
       })
       
       $('#big_nav_left').click(function(){
           gallery.prev_thumb.trigger("click");
           gallery.get_active();
       })
       
       $('#big_nav_right').click(function(){
           gallery.next_thumb.trigger("click");
           gallery.get_active();
       })
       
       gallery.current_image = $('.thumb:first')
   },
   
   show: function() {
        
        $('#gallery_bg')
               .css({opacity: 0.8})
               // .css("display","block")
               .fadeIn("slow", function() {
                            gallery.show_dock();
                            gallery.current_image.trigger('click')
                            $('#close_button').show();
                            $('.hide_dock').show();
                        }
                    )
        thumbs = $(".thumb");
       $('.thumb').each(function(index,obj){
           my_child = $(obj).find(".gal_thumb_inner");
           my_background = $(my_child).attr("rel");
           $(obj).css("background-image","url(" + my_background + ")");
           
           //$(obj).css("background-color","red");
       })
   },

   close: function() {
       $('#close_button').hide();
       $('.dock_toggle').hide();
       $('#gallery_bg').fadeOut("slow")
       $('#jquery-lightbox').hide();
       $('#big_nav_left, #big_nav_right').hide()
       gallery.hide_dock();
      
       
       
    },
   
    show_dock: function() {
        // $("#gallery_wrap").show()
        $("#gallery_thumbs").animate({height: "60px", paddingTop: "20px", paddingBottom: "20px"}, "fast", function(){
            $('#thumbs_wrapper').show()
            $('.thumb').fadeIn("slow")
            $('.mini_dock_control').show()
        })
    },

    hide_dock: function() {
        $('.thumb').hide();
        $('.mini_dock_control').hide();
        $("#gallery_thumbs").animate({paddingTop:"0px", paddingBottom: "0px", height: "0px"}, "fast")
    },
   
    get_active: function() {
        gallery.next_thumb = $('.active_thumb').next('.thumb')
        gallery.prev_thumb = $('.active_thumb').prev('.thumb')
    },
   
   reposition_gallery: function() {
       var win_width = $(window).width()
       var win_height = $(window).height()

       $("#jquery-lightbox").css({left: (win_width - $("#jquery-lightbox").width())/2+"px", top: $('body').innerHeight()/50})
       
   },
   
   set_image_to_view: function(image) { // show the loading
       
	// Show the loading
	$('#lightbox-loading').show();
	$('#lightbox-image,#lightbox-container-image-data-box,#lightbox-image-details-currentNumber').hide();
	
	// Image preload process
	var objImagePreloader = new Image();
	objImagePreloader.onload = function() {
		$('#lightbox-image').attr('src',image);
		// Perfomance an effect in the image container resizing it
		gallery.resize_container_image_box(objImagePreloader.width,objImagePreloader.height);
		//	clear onLoad, IE behaves irratically with animated gifs otherwise
		objImagePreloader.onload=function(){};
	};
	    objImagePreloader.src = image;
    },
    
    resize_container_image_box: function(intImageWidth,intImageHeight) {
		// Get current width and height
		var intCurrentWidth = $('#lightbox-container-image-box').width();
		var intCurrentHeight = $('#lightbox-container-image-box').height();
		// Get the width and height of the selected image plus the padding
		var intWidth = (intImageWidth + (10 * 2)); // Plus the image´s width and the left and right padding value
		var intHeight = (intImageHeight + (10 * 2)); // Plus the image´s height and the left and right padding value
		// Diferences
		var intDiffW = intCurrentWidth - intWidth;
		var intDiffH = intCurrentHeight - intHeight;
		// Perfomance the effect
		$('#lightbox-container-image-box').animate({ width: intWidth, height: intHeight },400,function() { gallery.show_image(); });
		if ( ( intDiffW == 0 ) && ( intDiffH == 0 ) ) {
			if ( $.browser.msie ) {
				gallery.pause(250);
			} else {
				gallery.pause(100);	
			}
		} 
		$('#lightbox-container-image-data-box').css({ width: intImageWidth });
		$('#lightbox-nav-btnPrev,#lightbox-nav-btnNext').css({ height: intImageHeight + (10 * 2) });
	},
	
	show_image: function() {
		$('#lightbox-loading').hide();
		$('#lightbox-image').fadeIn(function() {	
		});
	},
	
	show_image_data: function() {
		$('#lightbox-container-image-data-box').slideDown('fast');
		$('#lightbox-image-details-caption').hide();
	},
	
	pause: function(ms) {
		var date = new Date(); 
		curDate = null;
		do { var curDate = new Date(); }
		while ( curDate - date < ms);
	 },
	 
    get_cur_x: function(){
        return $('#thumbs_wrapper').position().left;
    },
       
    get_max_scroll: function(){
        return ($(window).width())-($('#thumbs_wrapper').width())
    },  
	 
	 move_left: function(){

         var cur_x = gallery.get_cur_x();
         var max_scroll = gallery.get_max_scroll();

         if (cur_x >= max_scroll) {
           $('#thumbs_wrapper').css({"left": cur_x- gallery.i+"px"})
           if (gallery.i <= 60){
               gallery.i = (gallery.i+5)
           }
         }
	 },
	 
	 move_right: function(){

	     var cur_x = gallery.get_cur_x();

         if (cur_x <= -1) {
           $('#thumbs_wrapper').css({"left": cur_x + gallery.i+"px"})
           if (gallery.i <= 60) {
               gallery.i = (gallery.i+5)
           }
         }
	 }
   
}

gallery = new Gallery
campdenali = new CampDenali

$(document).ready(function() {
    $('#page_loader').remove()
    
    campdenali.init();
   
    $('a[rel=lightbox]').each(function(){
       $(this).lightBox()
    });

    gallery.init()
});