// Replace 1300 numbers in the text of the website
var phoneNumber = "1300 275 449";
var newPhoneNumber = "1300 914 418";

$(document).ready(function() {
  queryString = "?" + window.location.search.substring(1); // We add the "?" so the pos of gclid is always > 0
	if (queryString.search(/gclid/i) > 0 || getCookie('gizmo_adwords') == '1') {
		// It's an Adwords user
		setCookie('gizmo_adwords','1',7);
		replaceText(phoneNumber, newPhoneNumber, $('#ContentLarge'));
		replaceText(phoneNumber, newPhoneNumber, $('#ContentSmall'));
		replaceImageBySelector("#Logo1 img", 'http://www.gizmo.com.au/images/gizmo_id1-new.jpg');
		replaceImageBySelector("#TitleRight img", 'http://www.gizmo.com.au/images/home_phone-new.gif');
	}
});


function replaceText(search, replace, subject) {
	if (subject.html() != null) {
		var match = new RegExp(search, "ig");     
	
		var replaced = "";
		
		if (replace.length > 0) {
			replaced = subject.html().replace(match, replace);
		}

		subject.html(replaced);

	}
}

function replaceImageBySrc(oldSrc, newSrc, subject) {
	$('img').each(function () {
		if ($(this).attr('src') == oldSrc) $(this).attr('src', newSrc);
	});
}

function replaceImageBySelector(selector, newSrc, subject) {
	$(selector).attr('src', newSrc);
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + '; path=/';
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
