function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));
	
	if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }
}

//alert(getBaseURL());

var themeUrl = getBaseURL()+'wp-content/themes/Kwekpoel/';

$(document).ready(function(){
		
						   
		change_background();
		
		if($('#home-slides img').size() > 0){
			build_home_slides();	
		}	
		init_image_grid_content_slider();
		$('.hidden').css('display', 'none');
		
		if($('#tabs-wrapper').size() > 0){
			if(init_tabs()){
				$('#tabs-wrapper img').simple_captions();
			}			
		}else{
			$('img').not('#home-slides img').simple_captions();
		}
		
		equalHeight($(".row .col"));
		
});

/**
 *	Swaps the backgournd images randomly
 */
function change_background(){
	var bg_image_url = themeUrl+'images/achtergronden/';
	var num_bgs = 4;
	var randno = Math.floor ( Math.random() * num_bgs+1 );
	$('body').css('background', 'url('+bg_image_url+'kwekpoel-achtergrond-'+randno+'.jpg) no-repeat top center');
}

/*
*	Checks if browser is IE6
*/
function isIe6(){
	if( $.browser.msie && ($.browser.version < 7) ){
		return true;
	}else{
		return false;
	}
}

/**
 *	Home page slide show
---------------------------------------------------------------------------------*/
/**
 *	Build the slides
 */
function build_home_slides(){
	var target_elem = '#home-slides li#first'
	var html = '';
	$.getJSON("wp-content/themes/Kwekpoel/json/home-slides.json",
		function(data){
			//alert(data);
			$.each(data.items, function(i,item){
			html += '<li>';
			html += '<div class="caption-wrapper">';
			html += '<img src="'+themeUrl+'images/home-slides/'+data.items[i]['img_src']+'.jpg" width="507" height="239" alt="'+data.items[i]['alt']+'" />';
			html += '</div>';
			html += '</li>';
			/*$("<img/>").attr("src", item.img_src).appendTo("#images");*/
				//if ( i == 3 ) return false;
			});
			if($(target_elem).after(html)){
				startHomeSlides();
			}
			//alert(html);
	  });
	//alert("Alert: ");
}

/**
 *	Init the home page slide hsow
 */
function startHomeSlides(){
	$('#home-slides ul').cycle({ 
	    fx:     'fade', 
	    speed:   1000, 
	    timeout: 5000, 
	    pause:   1, 
		before:  cycleOnBefore, 
		after:   cycleOnAfter
	});
}
/**
 *	Called before the transition starts
 */
function cycleOnBefore() { 

	if($(this).find('.caption_wrapper').size() > 0){
		var caption = $(this).find('.caption');
		caption.attr('id', 'curr_caption')
	}else{
		var caption = buildCaption(this);
	}
	
	if(isIe6()){
		caption.fadeOut(500).css('display', 'block');
		//$(this).mouseleave(function(){caption.css('display', 'none');})
} else {

    if (jQuery.support.opacity) {
        caption
		.fadeTo(0, 0).css('bottom', '0px').fadeTo(1000, 1);
    }
    else {
        caption
		.animate({ bottom: '0px' }, 1000)
		.animate({ bottom: '0px' }, 2000)
    }

	}
} 
/**
 *	Called when the transition ends
 */
function cycleOnAfter() { 
	var caption = $('#curr_caption');
	
	if(isIe6()){
		//caption.css('display', 'none');
	}else{

	    if (jQuery.support.opacity) {
	        setTimeout(function() { caption.fadeTo(500, 0) }, 1500);
	    }
	    else {
	       caption.animate({ bottom: '-55px' }, 500);
	    }
		caption.attr('id', '');		
	}
}

/**
 *	The credit captions
---------------------------------------------------------------------------------*/
/**
 *	Build the caption and caption wrapper
 */
function buildCaption(elem){	
	if(elem.tagName !== 'IMG'){
		var img = $(elem).find('img');
	}else{
		var img = elem;
	}	
	//var w = $(img).css('width');
	
	if($('#home-slides').size() > 0){
		//console.log($(elem).parents());
		
		if($(elem).parents('#home-slides').size() > 0){
			var w = 507;
			var h = 239;
		}else{
			var w = 235;
			var h = 155;
		}
	}else{
		var w = $(img).width();
		var h = $(img).height();
	}
	

	
	var caption_html = '<div class="caption"><span>'+$(img).attr('alt')+'</span></div>';
	var wrapper = '<div class="caption_wrapper"></div>';
	var wrapper_outer = '<div class="caption_wrapper_outer"></div>';
	
	$(img)
	.wrap(wrapper)
	.parent()
	.css('position', 'relative')
	.wrap(wrapper_outer);
	
	$(img).after(caption_html).siblings('div');
	var caption = $(img).siblings('div.caption');
	var caption_wrapper = $(img).parent('div.caption_wrapper');
	var caption_wrapper_outer = $(caption_wrapper).parent();
	caption_wrapper

	.css('overflow', 'hidden');
	
	if(	h > 0) {
		caption_wrapper
		.css('height',h)
	}
	
	caption_wrapper_outer
	.css('overflow', 'hidden')

	$(caption_wrapper_outer).parents('a').addClass('captioned')
	nw = Number(w) - Number(20);

	
	caption
	.css('bottom', '-60px')
	.css('font-size', '10px')
	.css('left', '0px')
	.css('padding-left', '10px')
	.css('padding-right', '10px')
	.css('position', 'absolute')
	.css('width', nw)
	//.attr('id', 'curr_caption');

	if($(caption_wrapper_outer).parents('a').size() > 0){
		$(caption_wrapper_outer).css('cursor', 'pointer').click(function(){
			var link_ref = $(this).parents('a').attr('href')
			window.location = link_ref;
		})
	}	
	
	// Block the animation from IE6 and FF2
	if(isIe6() || ($.browser.mozilla && (parseFloat($.browser.version) < 1.9))){
		caption.css('bottom', '0px').css('display', 'none').css('width');
	}
	return caption;	
}

/**
 *	Show the captions on image rollover
 */
jQuery.fn.simple_captions = function(options) {	
	// default settings
	var options = jQuery.extend( {
		colour: '#ffffff',
		slide_speed: 500, // speed at which the caption needs to slide
		caption_class_name: 'caption',
		caption_height: '55px',
		caption_background: 'url('+themeUrl+'images/bg-caption.png)'
	},options);

	return this.each(function() {
		var caption = buildCaption(this);
		var caption_wrapper_outer = $(this).parents('div.caption_wrapper_outer');
		var this_caption = $(caption_wrapper_outer).find('div.caption');
		
		// Block the animation from IE6 and FF2
		
		if(isIe6() || ($.browser.mozilla && (parseFloat($.browser.version) < 1.9))){
			$(this).mouseenter(function(){caption.css('display', 'block');})
			$(this).mouseleave(function(){caption.css('display', 'none');})
		}else{
			/*if ($.browser.mozilla && (parseFloat($.browser.version) < 1.9)) {
			   console.log('hello')
			}*/
			
			caption_wrapper_outer.mouseenter(function(){
				var this_wrapper_height = $(this).height();
				var this_caption = $(this).find('.caption');
				this_caption.fadeTo(0, 0).css('bottom', '0px');
				if (jQuery.support.opacity) {
				    this_caption.fadeTo(500, 1)
	            }
	            else {
	                this_caption.fadeTo(500, 0.7)
	            }
			})
			caption_wrapper_outer.mouseleave(function(){
				var this_wrapper_height = $(this).height();
					$(this).find('.caption').fadeTo(500, 0);
            	})
		}
	});
}



function init_image_grid_content_slider(){
	if($("#scroll-pages").size() > 0){
		equalHeight($(".image-grid li").not($('.image-grid li ul.link-list li')));
		var grid_height = $('ul.image-grid').height();

		if(build_page_nav(".pagination", $('ul.image-grid').size(), 'appendTo')){
			$(".slide-controls").jFlow({
				slides: "#scroll-pages",
				controller: ".slide-control", // must be class, use . sign
				slideWrapper : "#jFlowSlide", // must be id, use # sign
				selectedWrapper: "jFlowSelected",  // just pure text, no sign
				width: "645px",
				height: grid_height+'px',
				duration: 400,
				useControlText: 'true',
				prev: ".slide-prev", // must be class, use . sign
				next: ".slide-next" // must be class, use . sign
			});

			var paging_width = (19 * $(".pagination").find('.slide-control').size() / 2);
			$(".slide-controls").css('width', paging_width);
			$(".pagination").addClass('scrollable');
		}else{
			grid_height = grid_height;
			$('ul.image-grid').height(grid_height+'px')
		}
	}
}


/**
 *	Equal height columns
	http://www.cssnewbie.com/example/equal-heights/
---------------------------------------------------------------------------------*/

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}
