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");		
	};
	show($("#gallery ul li:first-child"));
};

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=250&width=240&inlineId=newsletterBox&modal=true";
		var h = $(this).attr("href");
		if(h==o){
			$(this).attr("href",n);		
		};		
    });	
	// side button
	sideButtonPos();	
	// live chat
	if (sideButtonId == "livechat"){		
		sideButton("livechat");	
	}		
	// kampyle		
	$("a[href$='#kampyle']").each(function(){	
		kampyleLink($(this));	
    });		
	// newsletter
	$("a.newsletterLink").each(function(){	
		newsletterLinkSet($(this));	
    });		
	$("a[href$='#newsletter']").each(function(){	
		newsletterLinkSet($(this));	
    });	
};

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);	
};

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;
}
var sideButtonId = "";
this.sideButtonPos = function(){	
	var w = getElementLeft("container") + getElementWidth("container");
	$("#sideButton").css("left", w);
	$("#sideButton").css("display", "block");
};
this.sideButton = function(tag){
	$("#sideButton a").attr("href", "#" + tag);	
	$("#sideButton a").attr("class", tag);
	if (tag == "livechat") {
		liveChatLink($("#sideButton a"));			
	}	
};

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.liveChatLink = function(obj){
	$(obj).html("Chat with our representative");	
	$(obj).attr("href","javascript:void(0);");	
	$(obj).attr("target","");	
	$(obj).click(function(){						   	
		window.open('http://server.iad.liveperson.net/hc/32971532/?cmd=file&file=visitorWantsToChat&site=32971532&imageUrl=http://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/General/1a&referrer='+escape(document.location),'chat32971532','width=475,height=400,resizable=yes');
		return false;	  										   
    });	
};

this.newsletterLinkSet = function(obj){	
	$(obj).attr("href","#TB_inline?height=250&amp;width=240&amp;inlineId=newsletterBox&amp;modal=true");	
	$(obj).addClass("thickbox");	
};

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.application = function(){	
	
	this.validate = function(name, email, phone, web){
		$("span.error").remove();
		var valid = true;
		//name
		if(name == "") {
			error($("#app_name"),"Please tell us your name.")
			valid = false;
		};	
		//email
		if(!checkEmail(email)) {
			error($("#app_email"),"We need a valid email address.")
			valid = false;
		};			
		//phone
		if(!checkPhone(phone)) {
			error($("#app_phone"),"We need your phone number.")
			valid = false;
		};	
		//web
		if(!checkWeb(web)) {
			error($("#app_web"),"What is your website's URL?")
			valid = false;
		};					
		return valid;
	};
	
	this.checkEmail = function(str){
	  var regEx = /^[^@]+@[^@]+.[a-z]{2,}$/;
	  return (str.search(regEx) != -1);
	};	
	this.checkPhone = function(str){
	  var regEx = /[\d\s_]{6,}/;
	  return (str.search(regEx) != -1);
	};			
	this.checkWeb = function(str){
	  var regEx = /[^.].[^.]{2,}$/;
	  return (str.search(regEx) != -1);
	};		
	
	this.error = function(obj,text){
		var parent = $(obj).parent();
		parent.append("<span class=\"error\">"+ text +"</span>");
		$("span.error",parent).hide().show("slow");
	};	
		
	this.send = function(name, email, phone, web, urgent, point, comment){	
	$("#applicationForm button").remove();					
	$("#applicationForm").append("<p class=\"success\">Sending...</p>");
	$("#applicationForm .success").hide().fadeIn(800);
	var dataString = "name="+ name +"&email="+ email +"&phone=" + phone +"&web=" + web +"&urgent=" + urgent +"&point="+ point +"&comment=" + comment;
	$.ajax({
			type: "POST",
			url: "/wp-content/themes/cre/application.php",
			data: dataString,
			success: function(data) {
				$("#applicationForm .success").remove();
				if(data == "ok"){
					$("#applicationForm").append("<p class=\"success\"><strong>Thank you for your time!</strong> We've received your email and we'll reply as soon as possible.</p>");				
				} else {
					$("#applicationForm").append("<p class=\"success\"><strong>There has been an error with sending your application.</strong> Please contact us at <a href='mailto:info@conversion-rate-experts.com'>info@conversion-rate-experts.com</a>. Sorry for the inconvenience.</p>");
				};
				$("#applicationForm .success").hide().fadeIn(800);
			}	
		});
	};	

	$("#applicationForm button").click(function(){																
		var name = $("#app_name").val();
		var email = $("#app_email").val();	 		
		var phone = $("#app_phone").val();	 				
		var web = $("#app_web").val();	 	
		var urgent = $("#app_urgent").val();	 	
		var point = $("#app_point").val();	 	
		var comment = $("#app_comment").val();	 			
		if(validate(name, email, phone, web)) send(name, email, phone, web, urgent, point, comment);
		return false;
	});	
};


this.swf = function(){	
	var s1 = new SWFObject("http://www.conversion-rate-experts.com/wp-content/themes/cre/swf/cronimation.swf","ply","693","226","9","#FFFFFF");
	s1.addParam("allowfullscreen","true");
	s1.addParam("allowscriptaccess","always");
	s1.addParam("wmode","transparent");
	if (document.getElementById("cronimation")) s1.write("cronimation");
	if (document.getElementById("cronimation2")) s1.write("cronimation2");
	var s2 = new SWFObject("http://www.conversion-rate-experts.com/wp-content/themes/cre/swf/cycleplayer.swf","ply","694","393","9","#f0f0f0");
	s2.addParam("allowfullscreen","true");
	s2.addParam("allowscriptaccess","always");
	if (document.getElementById("cycle")) s2.write("cycle");
};

this.init = function(){	
	gallery();	
	searchField();
	newsletterFields();
	cta();		
	links();
	buzzPage();		
	misc();
	application();
	swf();
};

$(document).ready(function(){	
	init();	
});
$(window).bind('resize', function() {
	sideButtonPos();
});