if(typeof console == "undefined"){
	console = {
		log: function(msg){
			// alert(msg);
		}
	}
}

var JQ = jQuery.noConflict(true); 

// FORM VALIDATION
JQ(function(){
	if(JQ('#primary form').length == 0) return false;
	
	JQ('input[name="nn"]').addClass("required name");
	JQ('input[name="ne"]').addClass("required email");
	
	JQ('#primary form').validate();
});

// GALLERY FRONT END FUNCTIONALITY
JQ(function(){

	var galleryContainer = JQ('.ngg-galleryoverview');
	JQ(galleryContainer).hide();
	
	var galleryItems = JQ('.ss_image');	

	galleryContainer.html("<div id='g_container'><div id='galleryThumbnails'><div id='galleryItems'></div></div></div>");
	JQ('#galleryItems').append(galleryItems);		

	
	// Setup the thumbnail click events
	//JQ('#galleryItems img').attr({'height':55,'width':90});
	JQ('#galleryItems img').attr({'height':70,'width':100});
	galleryItems.click(galleryItemClicked);
	

	if(JQ('#galleryItems img').length > 8){
		// if there are more than 6 gallery images add the previos and next buttons.
		JQ('#g_container').append('<div class="gallery_controls"><a href="#" class="gallery_previous">Previous</a><a href="#" class="gallery_next">Next</a></div>');
	}
	
	// Setup Main image area and set the first image in the gallery in the main area
	galleryContainer.append('<a href="#" id="galleryFeature" class="lightbox"></a><div class="clear">&nbsp;</div>');
	
	var baseImage = JQ(galleryItems[0]).attr('href'); 
	JQ('#galleryFeature').css('background','url('+baseImage+') center center no-repeat').attr('href',baseImage);
	
	//JQ('#galleryFeature').attr('href',baseImage);
	//JQ('#galleryFeature').html('<img src="'+JQ(galleryItems[0]).attr('href')+'" />');
	
	if(JQ(galleryItems[0]).attr('title') != " "){
		var caption = JQ('.ss_caption');
		if(caption.length <= 0){
			caption = JQ('<div class="clear">&nbsp;</div><p class="ss_caption">'+JQ(galleryItems[0]).attr('title')+'</p>');			
			JQ(caption).insertAfter(JQ('#galleryFeature'));
		}
		else{
			caption.text(JQ(galleryItems[0]).attr('title'));
		}			
		
	}
	
	
	// Fade in the entire gallery
	
	JQ(".post").prepend(JQ(galleryContainer));
	JQ(galleryContainer).fadeIn(500);
	
	JQ('#galleryThumbnails').scrollable({
		clickable : true,
		easing : "swing",
		nextPage : ".gallery_next",
		prevPage : ".gallery_previous",
		disabledClass : "disabled",
		vertical : true
	});
});

function galleryItemClicked(event){
	event.preventDefault();
	var target = event.currentTarget;
	
	if(JQ(target).attr('href') == JQ('#galleryFeature img').attr('src')) return false;
	
	var newImage = JQ(target).attr('href');
	
	JQ('#galleryFeature').fadeOut(500, function(){	
			
		JQ('#galleryFeature').css('background','url('+newImage+') center center no-repeat');
		JQ('#galleryFeature').attr('href',newImage);
		
		if(JQ(target).attr('title') != " "){
			JQ(".ss_caption").text(JQ(target).attr('title'));
		}
		
		JQ('#galleryFeature').fadeIn(500,function(){
			JQ("#galleryFeature .ss_caption").fadeIn(500);
		});
		
	});	
	return false;
}

// lightbox setup
function lightbox_setup () {
	if(jQuery('a[rel*=lightbox]').length > 0){
		jQuery('a[rel*=lightbox]').lightBox(); 
	}
	
	if(jQuery('a.lightbox').length > 0){
		jQuery('a.lightbox').lightBox(); // Select all links in the page		
	}
}
JQ(lightbox_setup);

function initActiveNavigation(){
	
	var current = JQ("#nav_"+current_page);
	var marker = JQ("#marker");

	if(current.length <= 0){
		marker.hide();
		return false;
	}
	
	
	var c_coords = current.offset()
	var c_width = current.width();

	marker.css({
		'top':c_coords.top + 32,
		'left':c_coords.left - 29,
		'width':c_width - 2
	});
}
JQ(document).ready(initActiveNavigation);

// COLUMN HEIGHT FIX
JQ(function(){
	var content = JQ('#content');
	var primary = JQ('#primary');
	var sidebar = JQ('#sidebar');
	
	if(sidebar.height() > content.height()){
		primary.height(sidebar.height() + 50);
		console.log(primary.height());
	}
	else{
		console.log("NO HEIGHT SHOULD GET SET HERE");		
	}
	
});



