/**
 * jQuery.Preload - Multifunctional preloader
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com
 * Dual licensed under MIT and GPL.
 * Date: 3/25/2009
 * @author Ariel Flesler
 * @version 1.0.8
 */
;(function($){var h=$.preload=function(c,d){if(c.split)c=$(c);d=$.extend({},h.defaults,d);var f=$.map(c,function(a){if(!a)return;if(a.split)return d.base+a+d.ext;var b=a.src||a.href;if(typeof d.placeholder=='string'&&a.src)a.src=d.placeholder;if(b&&d.find)b=b.replace(d.find,d.replace);return b||null}),data={loaded:0,failed:0,next:0,done:0,total:f.length};if(!data.total)return finish();var g=$(Array(d.threshold+1).join('<img/>')).load(handler).error(handler).bind('abort',handler).each(fetch);function handler(e){data.element=this;data.found=e.type=='load';data.image=this.src;data.index=this.index;var a=data.original=c[this.index];data[data.found?'loaded':'failed']++;data.done++;if(d.enforceCache)h.cache.push($('<img/>').attr('src',data.image)[0]);if(d.placeholder&&a.src)a.src=data.found?data.image:d.notFound||a.src;if(d.onComplete)d.onComplete(data);if(data.done<data.total)fetch(0,this);else{if(g&&g.unbind)g.unbind('load').unbind('error').unbind('abort');g=null;finish()}};function fetch(i,a,b){if(a.attachEvent&&data.next&&data.next%h.gap==0&&!b){setTimeout(function(){fetch(i,a,1)},0);return!1}if(data.next==data.total)return!1;a.index=data.next;a.src=f[data.next++];if(d.onRequest){data.index=a.index;data.element=a;data.image=a.src;data.original=c[data.next-1];d.onRequest(data)}};function finish(){if(d.onFinish)d.onFinish(data)}};h.gap=14;h.cache=[];h.defaults={threshold:2,base:'',ext:'',replace:''};$.fn.preload=function(a){h(this,a);return this}})(jQuery);


// Preload Images
jQuery.preloadImages = function()
{
  for(var i = 0,x; i<arguments.length; i++)
  {
    x = (new Image()).src = arguments[i];
  }
};

$(document).ready(function() {
	
	$('.tabControls a').click( function(e){
		$('.tabs .content, .tabControls a').removeClass('selected');
		$(this).addClass('selected');
		
		$($(this).attr('href')).addClass('selected');
		e.preventDefault();
	});

    $('body#contact').each(function() {


		
        var map = new GMap2(document.getElementById('contactNY'));
        map.setCenter(new GLatLng(40.739706, -73.992147), 17);
		var point1=new GLatLng(40.739706, -73.992147);
        map.setUIToDefault();
        map.disableScrollWheelZoom();

        var map2 = new GMap2(document.getElementById('contactATL'));
        map2.setCenter(new GLatLng(33.80792872493839, -84.4143533706665), 17);
		var point2=new GLatLng(33.80792872493839, -84.4143533706665);
        map2.setUIToDefault();
        map2.disableScrollWheelZoom();

        
        var lbiIcon = new GIcon(G_DEFAULT_ICON);
        lbiIcon.image = "http://www.lbi.com/us/wp-content/themes/lbi/assets/img/map-marker.png";
        lbiIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        lbiIcon.iconSize = new GSize(33, 41);
        lbiIcon.shadowSize = new GSize(22, 20);
        
		var lbiIcon2 = new GIcon(G_DEFAULT_ICON);
        lbiIcon2.image = "http://www.lbi.com/us/wp-content/themes/lbi/assets/img/map-marker.png";
        lbiIcon2.shadow = "http://www.google.com/mapfiles/shadow50.png";
        lbiIcon2.iconSize = new GSize(33, 41);
        lbiIcon2.shadowSize = new GSize(22, 20);

        markerOptions = { icon: lbiIcon };
        markerOptions2 = { icon: lbiIcon2 };
        map.addOverlay(new GMarker(point1, markerOptions));
        map2.addOverlay(new GMarker(point2, markerOptions2));

    });
	
	$('#showcase').jcarousel({
        vertical: true,
        scroll: 1
    });
	
	$('#showcase a').each(function() {
		var filename = $(this).contents().attr('src').replace(/.*?([^\/]+)$/, '$1');
		var preloadImg = "/assets/img/featured/" + filename;
		$.preloadImages(preloadImg);
	});
	
	$('#showcase a').click(function() {				
		var filename = $(this).contents().attr('src').replace(/.*?([^\/]+)$/, '$1');
		if (filename != '') {
			var targetImg = "/assets/img/featured/" + filename;
		} else {  
			var targetImg = "/assets/img/featured/no-image.png";
		}
		var targetUrl = $(this).attr('href');
		$('#img-feature > img').css('display', 'none').attr('src',targetImg).fadeIn('slow');
		targetTitle = $(this).contents().attr('title');
		var markup = '<a href="' + targetUrl + '"></a>';
		$('#headline h2').text(targetTitle).wrapInner(markup);
		//pageTracker._trackPageview(targetTitle);
		return false;
	});
	
	$(".winnew").click(
        function () {
            var w = window.open($(this).attr("href"),"pop");
            w.focus();
            return false;
        }  
    );

	$("#services > li:not(.active) h3 a").live("click",function(e){
	    $("#services > li").removeClass('active');
	    $(this).parents("li").addClass('active');
	    $("#tab1 > li").removeClass('active');
	    $("#tab2 > li").removeClass('active');
		e.preventDefault();
	});
	
	$("#services > li.active h3 a").live("click", function(e){	
		$(this).parents('li').removeClass('active');
		e.preventDefault();
	});
	
	$("#tab1 > li:not(.active) h3 a").live("click",function(e){
	    $("#tab1 > li").removeClass('active');
	    $(this).parents("li").addClass('active');
	    $("#services > li").removeClass('active');
		e.preventDefault();
	});
	
	$("#tab1 > li.active h3 a").live("click", function(e){	
		$(this).parents('li').removeClass('active');
		e.preventDefault();
	});
	$("#tab2 > li:not(.active) h3 a").live("click",function(e){
		$("#tab2 > li").removeClass('active');
		$(this).parents("li").addClass('active');
		$("#services > li").removeClass('active');
		e.preventDefault();
	});
	
	$("#tab2 > li.active h3 a").live("click", function(e){	
		$(this).parents('li').removeClass('active');
		e.preventDefault();
	});
	
	
});

/* twitter feed */
$(document).ready(function() {
	if (document.getElementById("tweet")) {
		jQuery.get("http://www.lbi.co.uk/proxy/proxy.php", function(data) {
			if (typeof data === "string") {
				document.getElementById("tweet").innerHTML = data;
			}
		}, "html");
	}
});

$(document).ready(function() {
    if (document.getElementById("tabs")) {
        $(function() {
            $("#tabs").tabs();
        });
    }
});

//THIS TEST'S CONTENT OF THE SIDEBAR DIV
//IF THE SIDE BAR IS EMPTY ALL PADDING IS REMOVED
$(document).ready(function() {
   var ts = $("#content-secondary").html();
   ts = ts.replace(/^\s*|\s*$/g,'');
   if( ts.length > 0 )
   {
   		$("#content-secondary").css("display","block");
   }
});


$(document).ready(function(){
	var lastentrydiv = $('.entry').get($('div.entry').length-1);
		lastentrydiv.style.paddingBottom = '40px';
		//console.log(lastentrydiv);
	});



/* inserting spans to mark the first letter of each word in the LBI Header in red */
$(function() {
	// get the randomised string (this class is not used on the homepage)
	var $tagline = $('.random_tagline');
	if ($tagline.length !== 0) {
		var string = $tagline.text();
		var new_string = "";
		var words = string.split(' ');
		$.each(words, function(key, value) {
		    var letters = value.split('');
		    //alert('letters:' + letters);
		    $.each(letters, function(key, value) {
		        if (key == 0) {
		            if (value == 'L' || value == 'B' || value == 'i' || value == 'I') {
		                new_string += '<span>' + value + '</span>';
		            } else new_string += value;
		        } else {
		            new_string += value;
		        }
		    });
		    new_string += ' ';
		});
	}
	$tagline.html(new_string);
});

