this.gallery = function(){		
	var i = 0;
	jQuery.each($("#gallery ul li"), function() {	
		i++;
		this.i = i;		
		this.className = "gallery"+i;
    });					
	
	$("#gallery ul li").mouseover(function(){		
		show(this);
    });
	
	this.show = function(obj){
		jQuery.each($("#gallery ul li"), function() {
			if (this != obj) $("img:first-child",this).fadeOut("fast");
			if (this != obj) $("p",this).fadeOut("fast");
		});		
		$("img:first-child",obj).fadeIn("fast");
		$("p",obj).fadeIn("fast");		
		/*
		$("#gallery p img").attr("src",$("a",obj).rel);
		$("#gallery p span").html($("a",obj).title);*/
	};
	show($("#gallery ul li:first-child"));
};

function getElementLeft(obj_id) {
	var obj = document.getElementById(obj_id);
	var objParent = obj.offsetParent;
	var objLeft = 0;
	if(obj != null) {
		objLeft = obj.offsetLeft;
		while(objParent != null) {
			objLeft += objParent.offsetLeft;
			objParent = objParent.offsetParent;
		}
	}
	return objLeft;
}

function getElementWidth(obj_id) {
	var elem = document.getElementById(obj_id);
	if (elem.offsetWidth) {
		xPos = elem.offsetWidth;
	} else {
		xPos = elem.style.pixelWidth;
	}
	return xPos;
}

this.kampyle = function(){	
	kampylePos();	
	kampyleLink($("#kampyle a"));	
	$("a[href$='#kampyle']").each(function(){	
		kampyleLink($(this));	
    });	
};
this.kampylePos = function(){	
	var w = getElementLeft("container") + getElementWidth("container");
	$("#kampyle").css("left", w);
	$("#kampyle").css("display", "block");
};
this.kampyleLink = function(obj){
	$(obj).attr("href","javascript:void(0);");	
	$(obj).attr("target","");	
	$(obj).click(function(){						   	
		window.open('http://www.kampyle.com/feedback_form/ff-feedback-form.php?&site_code=7043240&form_id=1784&lang=en&url='+encodeURIComponent(window.location.href), 'kampyle_ff','left='+((window.screenX||window.screenLeft)+10)+',top='+((window.screenY||window.screenTop)+10)+',height=502px,width=440px,resizable=false');
	//return false;		  										   
    });	
};

this.searchField = function(){	
	var txt = "Search This Site";
	$("#searchField").val(txt);
	$("#searchField").focus(function(){	
		this.value = (this.value != txt) ? this.value : "";	   
    });	
	$("#searchField").blur(function(){	
		this.value = (this.value != txt && this.value != "") ? this.value : txt;
    });		
};

this.newsletterFields = function(){	
	var txt1 = "Your Name";
	$(".newsletterName").val(txt1);
	$(".newsletterName").focus(function(){	
		this.value = (this.value != txt1) ? this.value : "";	   
    });	
	$(".newsletterName").blur(function(){	
		this.value = (this.value != txt1 && this.value != "") ? this.value : txt1;
    });		
	var txt2 = "Your Email Address";
	$(".newsletterEmail").val(txt2);
	$(".newsletterEmail").focus(function(){	
		this.value = (this.value != txt2) ? this.value : "";	   
    });	
	$(".newsletterEmail").blur(function(){	
		this.value = (this.value != txt2 && this.value != "") ? this.value : txt2;
    });			
};

this.cta = function(){
	$("#cta li").css("cursor","pointer");	
	$("#cta li").click(function(){	
		var url = (this.className != "two") ? $("a",this).attr("href") : $("a:nth-child(2)",this).attr("href");
		window.location.href = url;
    });	
};

this.buzzPage = function(){
	$("#davidnaylor .inner div").append('<span><iframe scroll="no" src="http://PlayAudioMessage.com/play.asp?m=511507&amp;f=TQTDKW&amp;ps=13&amp;c=FFFFFF&amp;pm=2&amp;h=25" frameborder="0" height="25" scrolling="no" width="76"></iframe>Listen to it here</span>');
	$("#billglazer .inner div").append('<span><iframe scroll=no width=76 height=25 frameborder=0 scrolling=no src="http://PlayAudioMessage.com/play.asp?m=511309&f=YSJXWJ&ps=13&c=FFFFFF&pm=2&h=25"></iframe>Listen to it here</span>');
	$("#tomleung .inner div").append('<span><iframe scroll="no" src="http://PlayAudioMessage.com/play.asp?m=482664&amp;f=OCXNQD&amp;ps=13&amp;c=FFFFFF&amp;pm=2&amp;h=25" scrolling="no" width="76" frameborder="0" height="25"></iframe>Listen to it here</span>');
};

this.links = function(){
	$("a").each(function(){	
		blankwin(this);		
		// temp
		var o = "#TB_inline?height=425&width=240&inlineId=newsletterBox&modal=true";
		var n = "#TB_inline?height=240&width=240&inlineId=newsletterBox&modal=true";
		var h = $(this).attr("href");
		if(h==o){
			$(this).attr("href",n);		
		};		
    });		
};

this.blankwin = function(a){
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	this.check = function(obj){
		var href = obj.href.toLowerCase();
		return ((href.indexOf("http://")!=-1 || href.indexOf("https://")!=-1)&& href.indexOf(hostname)==-1) ? true : false;				
	};
	this.set = function(obj){
		obj.target = "_blank";
		obj.className = "external";
	};	
	if(check(a)) set(a);	
};

this.newsletterLinks = function(){
	$("a.newsletterLink").each(function(){	
		newsletterLinkSet($(this));	
    });		
	$("a[href$='#newsletter']").each(function(){	
		newsletterLinkSet($(this));	
    });			
};
this.newsletterLinkSet = function(obj){	
	$(obj).attr("href","#TB_inline?height=250&amp;width=240&amp;inlineId=newsletterBox&amp;modal=true");	
	$(obj).addClass("thickbox");	
};


this.dropcap = function(){	
	$(".blogpost").each(function(){	
		node = $(this).text();
		html = $(this).html();
		node = node.replace(/\t/g,"");	
		node = node.replace(/\n/g,"");	
		node = node.split(" ");
		i=0;
		while(node[i] == ""){		
			i++;
		};	
		word = node[i];
		letter = word.substr(0,1);
		remain = word.slice(1);
		dropcap = "<span class=\"dropcap\">"+ letter +"</span>"+remain;
		html = html.replace(word,dropcap);
		$(this).html(html);		
    });			
};

this.misc = function(){	
	$(".sociable-hovers").each(function(){		
		$(this).css("opacity",0.4);		
		$(this).hover(
		  function () {
			$(this).css("opacity",1);		
		  },
		  function () {
			$(this).css("opacity",0.4);		
		  }
		);		
	});				
};
 

this.init = function(){	
	dropcap();
	gallery();	
	searchField();
	newsletterFields();
	newsletterLinks();
	cta();		
	kampyle();
	links();
	buzzPage();		
	misc();
};

$(document).ready(function(){	
	init();	
});
$(window).bind('resize', function() {
	kampylePos();
});