var subPageRecommendTimeoutFunc,mainMenuTimeoutFunc;
var recommendFieldInputActive=false; 
var individualSifrInitialized=false;

/** drukuj strone */
function pagePrint() {
	var pageUrl = document.location.href;
	var tPrintLink = '' + pageUrl + ( ( pageUrl.indexOf('?') > 0 ) ? '&' : '?' ) + '_view=print';
	var tWin = window.open( tPrintLink, 'print', 'scrollbars=yes,status=no,width=640,height=600' );
}

function removePageOfferMenuHoverAction() {
	$("#pageOfferMenu").unbind("mouseenter mouseleave");
	$("#selector").show();
}

/** polec znajomemu */
function showRecommendToContainer(animate) {
	clearTimeout(subPageRecommendTimeoutFunc);
	if(animate) {
		$("#polec_en").animate({opacity: "show", top: "-75"}, "fast");
		if($("#polec_podziekowanie").is(":visible")) {
			$("#polec_podziekowanie").hide();
			$("#polec_formularz").show();
		}
	}
}
function hideRecommendToContainer(timeout) {
	if( recommendFieldInputActive == false ) {
		subPageRecommendTimeoutFunc = setTimeout(function(){
			$("#polec_en input.newsletter").val("");
			$("#polec_en").animate({opacity: "hide", top: "-77"}, "fast");
		}, ((timeout!=undefined) ? timeout : 1000 ));
	}
}

/** clear default */
(function($){
    $.fn.clearDefault = function(){
        return this.each(function(){
            var default_value = $(this).val();
            $(this).focus(function(){
                if ($(this).val() == default_value) 
                    $(this).val("");
            });
            $(this).blur(function(){
                if ($(this).val() == "") 
                    $(this).val(default_value);
            });
        });
    };
})(jQuery);


 
function appendObservers()
{
    /** kotwice i linki */
    $('a[href^=#]').each(function(i, obj){
        var baseLocation = document.location.href;
        if (document.location.hash.length) {
            baseLocation = baseLocation.substr(0, baseLocation.indexOf('#'));
        }
        $(this).attr('href', baseLocation + $(this).attr('href'));
    });

	/** clearDefault event */
	$("input.newsletter").clearDefault();
	$("#pageSearchForm input.input").clearDefault();
	$("#polec_en input.newsletter").clearDefault();
	
    /** narzedziownik */
    $('#TextTools').find('li[class^=fs1]').each(function(i, obj){
        $(obj).click(function() {
            $('.txt').removeClass().addClass("txt " +$(this).attr('class'));
            return false;
        });
    });
	
    /** drukowanie */
    $('a.drukuj').click(function(){
        pagePrint();
        return false;
    });
	
	/** polec znajomemu */
	$("#btn_polec").hover(function() {
		showRecommendToContainer(true);
	}, function() {
		hideRecommendToContainer();
	}).click(function(){
		showRecommendToContainer(true);
	});
	
	$("#polec_en").hover(function(){
		showRecommendToContainer(false);
	}, function(){
		hideRecommendToContainer();
	});

	$("#polec_en input.newsletter").focus(function(){
		recommendFieldInputActive=true;
		showRecommendToContainer(false);
	});
	$("#polec_en input.newsletter").blur(function(){
		recommendFieldInputActive=false;
		hideRecommendToContainer();
	});
	$("#polec_en a.searchGo").click(function(){
		recommendFieldInputActive=true;
		showRecommendToContainer(false);

		$("#polec_formularz").hide();
		$("#polec_wysylanie").show();
		
        $.post("ajaxindex.php", {
            start: 'sendRecommendFriendForm',
            email: $("#recommend_friend_content").val(),
			pageUrl: window.location.href
        }, function(data){
			$("#polec_wysylanie").hide();
			if(data=='OK') {
				$("#polec_podziekowanie").show();
				recommendFieldInputActive=false;
				hideRecommendToContainer(3500);
			} else {
				/** cos poszlo nie tak */
				$("#polec_formularz").show();
				$("#recommend_friend_content")
					.css('border','1px solid #ff8040')
					.focus(function(){
						$(this)
							.css('border','')
							.unbind('focus');
					});
				
			}
        });
		return false;
	});
	
	/** newsletter */
	$("#newsletterForm a.searchGo").click(function(){
		$("#newsletterForm").submit();
		return false;
	});
	
	/** placowki i bankomaty */
	$("#placowkiBankomatyForm a.searchGo").click(function(){
		return false;
	});
	
	/** wyszukiwarka */
	$("#pageSearchForm a.btnSearch").click(function(){
		$("#pageSearchForm").submit();
		return false;
	});	

	/** ankieta */
	$("form[name='ankieta'] #glosuj").click(function(){
		$("form[name='ankieta']").submit();
		return false;
	});
	
}

$(document).ready( function() {
	appendObservers();
});

