var OGLandingCounter = 1;
var OGLandingTimeout;

function handleHPTabs(isVoucherOrdered) {
	if (isVoucherOrdered) {
		setHPVoucherTab2();
	} else {
		setHPVoucherTab1();
	}
	
	jQuery("#hpvchtab1").click(function(){setHPVoucherTab1();setTabHeight();});
	jQuery("#hpvchtab2").click(function(){setHPVoucherTab2();setTabHeight();});

	setHPProjectTab1();
	jQuery("#hpprjtab1").click(function(){setHPProjectTab1();setTabHeight();});
	jQuery("#hpprjtab2").click(function(){setHPProjectTab2();setTabHeight();});
	jQuery("#hpprjtab3").click(function(){setHPProjectTab3();setTabHeight();});
	jQuery(".hplogin1 input").focus(function(){
		jQuery(this).addClass("hplogintxt");
		if (this.value.indexOf(" ") != -1) this.value = "";
	});
	
	setTabHeight();
	
	jQuery(".tabbutr").click(function(){
		var url = jQuery(".tabbutc a").attr("href");
		location.href = url;
	});
}

function setTabHeight() {
	var minHeight = 185;
	if (qcodo.isBrowser(qcodo.IE)) minHeight = minHeight + 5;
	
	var leftHeight = jQuery(".hpvch2").height();
	var rightHeight = jQuery(".hpprj2").height();
	
	var h = Math.max(Math.max(minHeight,leftHeight),rightHeight);
	jQuery(".hpvch2").height(h);
	jQuery(".hpprj2").height(h);
	if (jQuery(".hpvch2d").css("display") != "none") jQuery(".hpvch2d").hide().css({"vertical-align":"bottom"}).show();
	jQuery(".hpprj2d").hide().css({"vertical-align":"bottom"}).show();
}

function setHPVoucherTab1() {
	jQuery("#hpvchtab1").removeClass('hpvchtab1Off').addClass('hpvchtab1On');
	jQuery("#hpvchtab2").removeClass('hpvchtab2On').addClass('hpvchtab2Off');	
	jQuery("#hpvchtbl1").removeClass('tbl1a').addClass('tbl1');
	jQuery("#hpvchtab1c").show();
	jQuery("#hpvchtab2c").hide();
	jQuery("#hpvch2lnk").show();
}

function setHPVoucherTab2() {
	jQuery("#hpvchtab1").removeClass('hpvchtab1On').addClass('hpvchtab1Off');
	jQuery("#hpvchtab2").removeClass('hpvchtab2Off').addClass('hpvchtab2On');	
	jQuery("#hpvchtbl1").removeClass('tbl1').addClass('tbl1a');
	jQuery("#hpvchtab2c").show();
	jQuery("#hpvchtab1c").hide();
	jQuery("#hpvch2lnk").hide();
}

function setHPProjectTab1() {
	jQuery("#hpprjtab1").removeClass('hpprjtab1Off').addClass('hpprjtab1On');
	jQuery("#hpprjtab2").removeClass('hpprjtab2On').addClass('hpprjtab2Off');	
	jQuery("#hpprjtab3").removeClass('hpprjtab3On').addClass('hpprjtab3Off');	
	jQuery("#hpprjtab1c").show();
	jQuery("#hpprjtab2c").hide();
	jQuery("#hpprjtab3c").hide();
}

function setHPProjectTab2() {
	jQuery("#hpprjtab1").removeClass('hpprjtab1On').addClass('hpprjtab1Off');
	jQuery("#hpprjtab2").removeClass('hpprjtab2Off').addClass('hpprjtab2On');	
	jQuery("#hpprjtab3").removeClass('hpprjtab3On').addClass('hpprjtab3Off');	
	jQuery("#hpprjtab1c").hide();
	jQuery("#hpprjtab2c").show();
	jQuery("#hpprjtab3c").hide();
}

function setHPProjectTab3() {
	jQuery("#hpprjtab1").removeClass('hpprjtab1On').addClass('hpprjtab1Off');
	jQuery("#hpprjtab2").removeClass('hpprjtab2On').addClass('hpprjtab2Off');	
	jQuery("#hpprjtab3").removeClass('hpprjtab3Off').addClass('hpprjtab3On');	
	jQuery("#hpprjtab1c").hide();
	jQuery("#hpprjtab2c").hide();
	jQuery("#hpprjtab3c").show();
}

/**
 * ustawia wysokość środkowego panelu z pytaniami
 */
function setInquiryHeight() {
	var tabh = jQuery(".inq").height();
	var imgh = jQuery("#imgh").html();
	jQuery(".inqright").height(Math.max(tabh,imgh)-61);
}

jQuery(document).ready(function(){
	var page = jQuery("#GAPageName").html();
	if (page && page != "") {
		jQuery("#lnkTopInquiry_ctl").click(function(){
			saveGA(page + '/menu-gorne/biore-udzial-w-badaniu');
		});
		jQuery("#lnkFootInquiry_ctl").click(function(){
			saveGA(page + '/menu-dolne/biore-udzial-w-badaniu');
		});
		jQuery("#lnkVoucherInquiry_ctl").click(function(){
			saveGA(page + '/voucher-za-opinie/biore-udzial-w-badaniu');
		});
		jQuery("#lnkProjectInquiry_ctl").click(function(){
			saveGA(page + '/aktualny-projekt/biore-udzial-w-badaniu');
		});
		jQuery("#lnkInquiryCount_ctl").click(function(){
			saveGA(page + '/modul-liczba-przebadanych/biore-udzial-w-badaniu');
		});
		jQuery("#lnkProjectsInquiry_ctl").click(function(){
			saveGA(page + '/w-tekscie/biore-udzial-w-badaniu');
		});
		jQuery("#lnkAboutInquiry_ctl").click(function(){
			saveGA(page + '/w-tekscie/biore-udzial-w-badaniu');
		});
	}
	
	// pokazanie regulaminu w stronie zamówienia
	showTerms();
	
	// zamknięcie regulaminu
	jQuery("#ordTermsClose1").click(function(){
		qc.getWrapper("ordDlgTerms").hideDialogBox();
	});
	jQuery("#ordTermsClose2").click(function(){
		qc.getWrapper("ordDlgTerms").hideDialogBox();
	});
	
	// newsletter - usunięcie napisu
	jQuery("#hpnewsinp").focus(function(){
		var off = jQuery("#hpnewsoff").html();
		if (this.value == off) {
			this.value = '';
		}
	});
	
	// landing 4 - button
	jQuery(".lnd4g1").click(function(){
		document.location.href = jQuery(this).find("a").attr("href");
	});
});

function showTerms() {
	jQuery(".ordTerms a").click(function(){
		qc.getWrapper("ordDlgTerms").showDialogBox();
		saveGA('order/warstwa-regulamin');
	});
}

/**
 * obsługa dialogu dla landing w wersji 1
 */
function handleLanding() {
	saveGA('landing/1/show');
	OGLandingTimeout = setTimeout(showLanding,1000);
}

/**
 * handler dla intervalu
 */
function showLanding() {
	if (OGLandingCounter == 5) {
		var lnk = jQuery("#OGLInq_ctl").attr("href");
		location.href = lnk;
	} else {
		OGLandingCounter++;
		jQuery(".dlgb"+OGLandingCounter).show();
		OGLandingTimeout = setTimeout(showLanding,1000);
	}
}

/**
 * zamknięcie dialogu
 * @param intVersion
 */
function cancelLanding(intVersion) {
	clearTimeout(OGLandingTimeout);
	saveGA('landing/' + intVersion + '/close');
}

/**
 * przekierowanie do kroku 3
 * @param strPage
 * @param strControlId
 */
function redirectToStep3(strPage, strControlId) {
	OGLandingTimeout = setTimeout(function(){
		qc.pA(strPage, strControlId, 'QClickEvent', '', '');
	}, 3000);
}

/**
 * przekierowanie na ankietę
 */
function redirectToInquiry() {
	OGLandingTimeout = setTimeout(function(){
		var lnk = jQuery("#OGLInq_ctl").attr("href");
		location.href = lnk;
	}, 4000);
}

/**
 * zmiana stylu i usunięcie tekstu po wejściu do pola
 */
function inquiryEmailFocus() {
	jQuery("#inqEmail").focus(function(){
		var txt = jQuery("#inqEmailOff").html();
		if (this.value == txt) {
			jQuery(this).addClass("inqe5b");
			this.value = '';
		}
	});
}

/**
 * przepisanie wartości na kopertę
 * @param strFieldId
 */
function changeAddressField(strFieldId) {
	var obj = document.getElementById(strFieldId);
	var val = "";
	if (obj) {
		if (obj.tagName.toLowerCase() == 'select') {
			val = jQuery(obj).find(":selected").html();
		} else {
			val = jQuery(obj).val();
		}
	}
	jQuery("#"+strFieldId.substr(7)).html(val);
}

function trackAdwords() {
	var params = {
			id: 1067693797,
			language: "pl",
			format: "1",
			color:"ffffff",
			label:"UTKJCJu0zQEQ5e2O_QM",
			value: 1
	};
	
	// wyjście z drugiego kroku bez dodatkowych ekranów i walidacji
	jQuery(".ordpmtTb").find(":radio").each(function(){
		if (this.checked) {
			var pid = this.getAttribute("paymentId");
			switch (pid) {
				case "1"  : // COD
				case "11" : // PPL CC
				case "12" : // SaferPay
				case "10" : // PayPal
				case "16" : // PayPal transfer
				case "17" : // PayPal CC
				case "18" : // OTP
				case "21" : // Epay
				case "22" : // EBG
					googleConversion(params);
			}
		} 
	});
	
	// platnosci.pl
	jQuery(".osPPLFrame").find(":radio").each(function(){
		if (this.checked) googleConversion(params);
	});
}

function postInquiry() {
	googleConversion({id:1067693797,language:"en",format:"3",color:"ffffff",label:"EBpfCJvrhAIQ5e2O_QM",value:0});
	location.href = '/order.html';
}

function googleConversion(params) {
	var script = new Array();
	script.push("var google_conversion_id = ",params.id,";");
	script.push("var google_conversion_language = ",'"',params.language,'"',";");
	script.push("var google_conversion_format = ",'"',params.format,'"',";");
	script.push("var google_conversion_color = ",'"',params.color,'"',";");
	script.push("var google_conversion_label = ",'"',params.label,'"',";");
	script.push("var google_conversion_value = ",params.value,";");
	
	var objScript1 = document.createElement("script");
	objScript1.setAttribute("type", "text/javascript");
	objScript1.text = script.join("");
	document.body.appendChild(objScript1);

	var objScript2 = document.createElement("script");
	objScript2.setAttribute("type", "text/javascript");
	objScript2.setAttribute("src", "http://www.googleadservices.com/pagead/conversion.js");
	document.body.appendChild(objScript2);
}

function startAnim() {
	jQuery("#repShow").cycle({
		speedIn: 1500,
		speedOut: 1500,
		timeout: 3000,
		speed: 3000,
		requeueTimeout: 0,
		autostop: 1,
		autostopCount: 18
	});
}

function showHotelTerms() {
	jQuery(".htltxtTT span").click(function(){
		qc.getWrapper("ordDlgTerms").showDialogBox();
		var top = document.getElementById("cheque").offsetTop;
		var frame = jQuery("div.ordTermsDlg5");
		frame[0].scrollTop = top;
		saveGA('hotel/warstwa-regulamin');
	});
	jQuery("#ordTermsClose1").click(function(){
		qc.getWrapper("ordDlgTerms").hideDialogBox();
	});	
}

function welcomeEmail() {
	var html = jQuery("#email_html").html();
	jQuery(".textbox").focus(function(){
		if (jQuery(this).val() == html) {
			jQuery(this).val("");
		}
	}).blur(function(){
		if (jQuery(this).val() == "") {
			jQuery(this).val(html);
		}
	});
}

