function launchSurvey(){
	createModal();
	$("#wufoo-survey").dialog({
		resizable: false,
		height:130,
		width:465,
		modal: true,
		closeOnEscape: true,
		buttons: {
			"Give Feedback": function() {
				// store the width x height of window
				var windowHeight = $(window).height();
				var windowWidth = $(window).width();
				
				// webtrends
				dcsMultiTrack('DCS.dcsuri','/survey/','WT.ti','Foresee Survey');
				
				// remove the margin on the iframe area before loading it in 
				$(".ui-dialog-content").css('margin', '0');
				
				// load the iframe
				$(this).html('<iframe id="wufoo" width="100%" height="100%" src="https://sherwin.wufoo.com/forms/product-finishes-satisfaction-survey/" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" />');
				$(this).dialog({width:670, 
								height:windowHeight - 100, 
								position:[(windowWidth - 666) / 2, 20],
								buttons: {}
								});
			},
			"no thanks": function() {
				$( this ).dialog("close");
			}
		}
	})
	.dialog("widget")
	.find(".ui-dialog-buttonpane button")
	.eq(0).addClass("btn_Feedback").end()
	.eq(1).addClass("btn_NoThanks").end();	
}

function createModal(){
	if($("#wufoo-survey").length == 0){
		var surveyModal = '<div id="wufoo-survey">';
		surveyModal += '<img src="/images/wufoo/intro_text.png" alt="" />';
		surveyModal += '</div>'
		$("body").append(surveyModal);	
	}
}

function saveCookie(){
	var cookie = $.cookie("clickCount");
	var path = "/";
	var domain = "sherwin-williams.com";
	var expires = 30;
	
	if (cookie == null){
		$.cookie("clickCount", "1", {expires: expires, path: path, domain: domain });
	} else {
		$.cookie("clickCount", parseInt(cookie) + 1, {expires: expires, path: path, domain: domain });
		if ($.cookie("clickCount") > 4){
			$.cookie("clickCount", "expired", {expires: expires, path: path, domain: domain });
		}
	}
}

function checkCookie(){
	var cookie = $.cookie("clickCount");
	if(cookie == 4)
		launchSurvey();
}

function detectClicks(){
	$("a").click(function(){
		saveCookie();
	});
}

$(document).ready(function(){
	detectClicks();
});

$(window).load(function(){
	checkCookie();
});
