/*
Theme Name: Jarla Partilager Theme
Theme URI: http://wordpress.org/
Description: Theme for Jarla Partilager
Author: Perfect Fools
Version: 1.0
License: GNU General Public License
License URI: license.txt
Tags: Jarla Partilager
*/

/*-----------------------------------------------------
=LOG
-----------------------------------------------------*/
function logit(msg) {
	try {
		if (typeof console != 'undefined' && typeof console.log != 'undefined') {
			console.log(msg);
		}
	} catch (e) {}
}

/*-----------------------------------------------------
=LONGER PAGES
-----------------------------------------------------*/
function longerPages(){
	//alert("longerpages");
	
	var version = jQuery.browser.version;
	var documentHeight =  $('#wrapper').height();
	//documentHeight = document.documentElement.clientHeight;
	
	var windowHeight =  $(window).height();	

	//alert(document.location.toString().indexOf("nggpage"));
	
	var headerHeight = $('#header').outerHeight(true);
	var mainHeight = $('#main').outerHeight(true);
	var menuHeight = $('#menu').outerHeight(true);
	var footerHeight = $('#footer').outerHeight(true);
	
	var containerHeight = $('#container').outerHeight(true);
	var bottomInfoHeight = $('#bottomInfo').outerHeight(true);
	var exhibitionInfoHeight = $('.exhibition_info_title').outerHeight(true); 
	var exhibitionListHeight = $('#exhibitions_list').outerHeight(true);
	
	logit('mainHeight: '+mainHeight);
	/*logit('menuHeight: '+menuHeight);
	logit('footerHeight: '+footerHeight);
	*/logit('documentHeight: '+documentHeight);/*
	logit('windowHeight: '+windowHeight);
	logit('headerHeight: ' + headerHeight);
	logit('containerHeight: '+windowHeight);
	logit('bottomInfoHeight: ' + bottomInfoHeight);
	logit('exhibitionInfoHeight: ' + exhibitionInfoHeight);
	logit('exhibitionListHeight: ' + exhibitionListHeight);
	 */
	
	var contentLimit = 500;
	//if (mainHeight <= documentHeight){
	if (1 == 1){
		
		// Page is "shorter" than window
		$('#main').css({
			'height': documentHeight - headerHeight - menuHeight - footerHeight
		});
		
		$('#container').css({
			'height': documentHeight - headerHeight - menuHeight - footerHeight - bottomInfoHeight
		});
		
		/*var h = documentHeight - headerHeight - menuHeight - footerHeight - bottomInfoHeight;
		if(h > contentLimit) {*/
		$('#content').css({
			'height': documentHeight - headerHeight - menuHeight - footerHeight - bottomInfoHeight
		});
		//}
		
		
		// If we are on the start page, change height of content
		if(document.location.toString().indexOf("nggpage") > 0) {
			var contentHeight = $('#content').outerHeight(true);
			
			$('#content').css({
				'height': documentHeight - headerHeight - menuHeight - footerHeight - exhibitionInfoHeight - bottomInfoHeight,
			});
			
			if ($.browser.webkit || $.browser.msie) {
				$('#footer').css({
					'margin-top': 30,
				});
				
				$('#content').css({
					'height': documentHeight - headerHeight - menuHeight - footerHeight - exhibitionInfoHeight - bottomInfoHeight - 10
				});	
			}
			
			
			//logit(exhibitionInfoHeight);
			
			if(exhibitionInfoHeight > 39) {
				$('#footer').css({
					'margin-top': 36
				});
						
				$('#content').css({
					//'height': documentHeight - headerHeight - menuHeight - footerHeight - exhibitionInfoHeight - bottomInfoHeight - 11
				});	
				
								
				$('#main').css({
					//'height': documentHeight - headerHeight - menuHeight - footerHeight -11
				});
			}
			
			
		}
		
		if(document.location.toString().indexOf("category") > 0) {
			$('#main').css({
				'height': exhibitionListHeight
			});
			
			if ($.browser.msie) {
				$('#main').css({
					'height': exhibitionListHeight + 90
				});
			}
			
			
		}
		
	} else {
		// Page is "longer" than window
		alert("page is longer than window");
	}
	
	// Internet Explorer
	if ($.browser.msie) {
	}
		
	//alert($.browser.webkit);
	
	
	$('#wrapper').css({
		'visibility': 'visible'
	});	  
	//}	
		
return false;	
} // end longerPages()

/*-----------------------------------------------------
 RESIZE
-----------------------------------------------------*/

//if window is resized, run longerPages() again to get right size
window.onresize = function(){
	doresize();
};

function doresize() {
	//alert("now");
	
	longerPages();
	/*var currheight;
	if(currheight > 500) {
		
	}
	
	currheight = document.documentElement.clientHeight;
	logit('currheight on doresize: ' + currheight);*/
}

var i = 0;//initialize
var int=0;//Internet Explorer Fix
var intervalid;

$(document).ready(function() {
	longerPages();
	
	$(function () {
	    //$('img').hide(); //hide all the images on the page
		$('img').css('visibility', 'hidden');
	});

	
	// Fetch href of start page image and give the start page header "Private exhibition space" the same href. Do this after 500ms
	intervalid = setInterval("onInterval()", 500);
	
	
	$(window).bind("load", function() { //The load event will only fire if the entire page or document is fully loaded
		//alert("loaded?");
	    var int = setInterval("doThis(i)", 10);//500 is the fade in speed in milliseconds
	});
	
	/* 
		Bit of an ugly fix:
		Based on the current url, set menu items css class
	*/
	
	
	
	
	var isHome = '<?php echo is_home();?>';	
	var currentURL = window.location.toString();
	
	if(currentURL.indexOf('contact') != -1) {
		$("#item_contact").addClass('current_page_item');
	}  
	else if(isHome == 1) {
		$("#item_exhibitions").removeClass('current_page_item');
		$("#item_contact").removeClass('current_page_item');
		
	}
	else {
		$("#item_exhibitions").addClass('current_page_item');
	} 


	// -------------------------------- 
	// Setup rollover for thumbnails 
	// --------------------------------
	// Get width and height from image and set width and height (minus 4 for border thickness) on white-border  
	$('.ngg-gallery-thumbnail-box').mouseover(function(){
		// The commented solution below is old, new solution in gallery.php 
		/*var w = $(this).find('img').attr('width') - 4; 
		var h = $(this).find('img').attr('height') - 4;

		$(this).find('.white-border').css('width', w);
		$(this).find('.white-border').css('height', h);*/
		
		var loaded = ($(this).find('img').css('visibility') == 'visible') ? true : false;		
		if(loaded) {
			$(this).find('.white-border').css('visibility','visible');			
		}
		
	});

	$('.ngg-gallery-thumbnail-box').mouseout(function(){
		$(this).find('.white-border').css('visibility','hidden');
	});

	// -------------------------------- 	
});

function onInterval() {
	//alert("h");
	intervalid = clearInterval(intervalid);
	var start_image_link = $('#start_page_image').find('a').attr('href');
	$('#start_page_header_link').attr('href', start_image_link);
	
}

function doThis() {
    var images = $('img').length;//count the number of images on the page
    if (i >= images) {// Loop the images
            clearInterval(int);//When it reaches the last image the loop ends
    }
    //$('img:hidden').eq(0).fadeIn(250);//fades in the hidden images one by one
    $('img').css('visibility', 'visible');
    
    
    i++;//add 1 to the count
}

function showGallery() {
	isFromList = false;
	
	$('#exhibition_startpage_table').hide();
	$('#container').show(); 
}
