﻿$(document).ready(function () {
	$("#partnerId").keyup( function () {
	  makePartnerLinkText($(this).attr("value"), 1);
	} );
	
	makePartnerLinkText(0, 2);
	
	$("#simpleTextLink .linkText").keyup( function () {
	  $("#simpleTextLink .codeLink").val(wrapPartnerContent(0, 2, $(this).attr("value"), 1));
	} );
	
	$(".qAnswer").css("display", "none");
	$(".qTitle a").click(function () {
		if ($(this).parent().next(".qAnswer").css("display") == "none")
			$(this).parent().next(".qAnswer").show("slow");
		else 
			$(this).parent().next(".qAnswer").hide("slow");
		return false;
	});
	
	$.showPopup();
});

var siteTitle = "Бесплатные вебинары, онлайн-курсы, онлайн-школы: ФИНАНСЫ, ЗДОРОВЬЕ, ОТНОШЕНИЯ, ДУХОВНЫЙ И ЛИЧНОСТНЫЙ РОСТ";

function makePartnerLinkText(partnerId, type)
{
	switch (type) {
		case 1:
			$("#partnerBanners li .partnerCode").each (function () {
		    $(this).attr("value", wrapPartnerContent(partnerId, type, '<img src="http://happyandrich.ru' + $(this).parent().children("img").attr("src") + '" alt="' + siteTitle + '"/>'));
			});
			break;
		
		case 2:
			$("#partnerBanners li .simpleCode").each (function () {
		    $(this).attr("value", wrapPartnerContent(partnerId, type, '<img src="http://happyandrich.ru' + $(this).parent().children("img").attr("src") + '" alt="' + siteTitle + '"/>'));
			});
	}
}
function wrapPartnerContent(partnerId, type, content)
{
	var href;
	if (type == 1)
	{
		href = 'http://www.ecommtools.com/cgi-bin/aff_click.cgi?uid=happyandrich&id=' + partnerId;
	}
	else if (type == 2)
	{
		href = "http://www.happyandrich.ru/";
	}
	if (partnerId.length > 0 || type == 2) {
  	return '<a title="' + siteTitle + '" href="' + href + '">' + content + '</a>';
  }
	else {
		return "";
	}
}

$.showPopup = function(){
	var timeout = 5000;
	
	if ($.getCookie("popupShowed") != "true" && !doNotShowOnFp)
		setTimeout(showPopup, timeout);
		
	function showPopup() {
		$.ajax({
			url:"/load_node.php",
			data:{
				p_id:4781
			},
			success:function(data){
				showForm(data);
			}
		});
	}
	
	function showForm(data) {
		var blackBack;
		$(document.body).append('<div id="blackBack"></div>');
		
		blackBack = $("#blackBack");
		blackBack.css("width", document.body.offsetWidth + "px").css("height", document.body.offsetHeight + "px").
			css("position", "absolute").css("left", "0").css("top", 0).
			css("background", "#000").css("opacity", "0").css("zIndex", "5000");
		
		$(document.body).append('<div id="alertForm"><div class="formContent">' + 
			data + '</div><a href="#" class="close">Не показывать больше</a></div>');
		var alertForm = $("#alertForm");
		
		alertForm.css("position", "absolute").css("width", "30%").css("z-index", "6000").
			css("background", "#fff").css("opacity", "0").css("padding", "10px 10px 50px").
			css("border", "#666 10px solid");
		
		alertForm.find(".close").css("position", "absolute").css("right", "10px").css("bottom", "10px").
			click(function(){
				alertForm.remove();
				blackBack.remove();
				$.setCookie("popupShowed", "true");
				return false;
			});
		
		centrizeForm();
		
		blackBack.animate({
			opacity:0.5
		}, 300, "", function(){
			alertForm.animate({
				opacity:0.9
			}, 800);
		});
		
		$(window).resize(function(){
			blackBack.css("width", document.body.offsetWidth + "px").css("height", document.body.offsetHeight + "px");
		}).scroll(function(){
			centrizeForm();
		});
		
		function centrizeForm() {
			alertForm.css("left", document.body.offsetWidth / 2 - alertForm.width() / 2 + "px").
				css("top", $(window).scrollTop() + 
					(typeof(window.innerHeight) != 'undefined' ? window.innerHeight : document.documentElement.clientHeight) / 2 - alertForm.height() / 2 - 40 + "px");
		}
	}
}

$.setCookie = function(name, value) {
    var expiration = new Date();

    expiration.setTime(expiration.getTime() + 1000*60*60*24*3650);

    document.cookie = name + "=" + value + "; expires=" + expiration.toGMTString() + "; path=/";
}
    
$.getCookie = function(name) {
    var docCookie = " " + document.cookie;
    var search = name + "=";
    var start = docCookie.indexOf(search);
    var end;

    if (start >= 0)
    {
        start += search.length;
        end = docCookie.indexOf(";", start - 1);
    
        if (end == -1)
        {
            end = docCookie.length;
        }
    
        return docCookie.substring(start, end);
    }
    return false;
}

