var navigWidth = [];

$(document).ready(function() {    
    if($("#addfriend").length > 0) {
        $("#sendMSG").click(function() {
            //$("#sendMSG").attr("disabled","disabled");            
            if($("#emailshow:checked").length > 0) {
                $("#addfriendform fieldset").append('<input type="hidden" value="' + $("#emailshow").val() + '" name="emailshow"/>');
            }
            $("#emailshow").attr("disabled","disabled");
            $.extend($.blockUI.defaults.overlayCSS, { backgroundColor: '#cecece' });
            $("#content").css("border","solid 1px #A5ACB2");
            $("#contentOverlay").block();
        });
    }
    
    /** DODAJ DO ULUBIONYCH **/
	
	$("a.faventry").click(function() {
		var th = this;
		$(this).ajaxSubmit({
			dataType: 'json',
			type: 'post',
			url: $(th).attr("href"),
			beforeSubmit: function() {
				$(th).parent().children(".faventrywiatrak").css("display","inline");
			},
			success: function(re,st) {
				$(th).parent().children(".faventrywiatrak").css("display","none");
				if(re.result == "ok") {
					$(th).attr("href",re.value);
					if($(th).children("img").attr("title").indexOf("Dodaj") >= 0) {
						$(th).children("img").attr("src","/images/faventrydel.png");
						$(th).children("img").attr("title","Usuń z ulubionych");
					} else if($(th).children("img").attr("title").indexOf("Usuń") >= 0) {
						$(th).children("img").attr("src","/images/faventryadd.png");
						$(th).children("img").attr("title","Dodaj do ulubionych");
					}
				} else if(re.result == "error") {
					genZdzivBox(300,80,10,"errorbox",false,false);
					$("#errorbox").append('<p class="verif">(!) '+re.value+'</p><input type="image" id="errorok" src="/global/images/btn/ok.gif" alt="ok" />');
					createPoltergeist();
					$("#errorbox").fadeIn(300, function() {
						$("#errorok").click(function() {
							removePoltergeist();
							$("#errorbox").fadeOut(300, function() { $(this).remove(); });
						});
					});
				}
			},
			error: function() {
				$(th).parent().children(".faventrywiatrak").css("display","none");
				genZdzivBox(240,80,10,"errorbox",false,false);
				$("#errorbox").append('<p class="verif">(!) Wystąpiły problemy techniczne</p><input type="image" id="errorok" src="/global/images/btn/ok.gif" alt="ok" />');
				createPoltergeist();
				$("#errorbox").fadeIn(300, function() {
					$("#errorok").click(function() {
						removePoltergeist();
						$("#errorbox").fadeOut(300, function() { $(this).remove(); });
					});
				});
			}
		});
		return false;
	});
    
});

function sendMsgStatus(msg) {
    $("#content").css("background","none");
    $("#contentOverlay").unblock();
    $("#addfriendform").slideUp("slow");
    setTimeout("$(\"#addfriend\").append(\"<p style=\'padding: 10px 0; font-size: 12px; letter-spacing: 1px;\'>" + msg + "</p>\");",1000);
}

function setCursorPos(ctrl) {
    if(document.selection) { // IE
	   globalCursorPos = getCursorPos(ctrl);
	}
	else if (ctrl.selectionStart || ctrl.selectionStart == '0') { // FF
	   globalCursorPos = ctrl.selectionStart;
	}
}


function getCursorPos(textElement) {
    var sOldText = textElement.value;
    var objRange = document.selection.createRange();
    var sOldRange = objRange.text;
    var sWeirdString = '#%~';

    objRange.text = sOldRange + sWeirdString;
    objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

    var sNewText = textElement.value;

    objRange.text = sOldRange;

    for (i=0; i <= sNewText.length; i++) {
        var sTemp = sNewText.substring(i, i + sWeirdString.length);
        if (sTemp == sWeirdString) {
            var cursorPos = (i - sOldRange.length);
            return cursorPos;
        }
    }
}


function insertString(textObject,stringToInsert) {
    var firstPart = textObject.value.substring(0, globalCursorPos);
    var secondPart = textObject.value.substring(globalCursorPos, textObject.value.length);
    textObject.value = firstPart + stringToInsert + secondPart;
}

function getZdzivPosition(zdzivName) {
    var left = parseInt((document.documentElement.clientWidth - parseInt($("#" + zdzivName).css("width")))/2);
	var top = parseInt((document.documentElement.clientHeight - parseInt($("#" + zdzivName).css("height")))/2);
    if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		top += document.documentElement.scrollTop;
	} else {
		top += $(document).scrollTop();
	}
	return [left,top];
}

// [2007.08.09]
// Zmieniono sposob wywolywania zmiennej boxName
// przed nazwa boxName nalezy podac selektor elementu ("." / "#")
function giveMeBG(boxName,posX,posY) {
    if(!posX || posX == null || posX == "")
        var posX = "center";
    if(!posY || posY == null || posY == "")
        var posY = "center";
    $(boxName).css("backgroundImage","url('/global/images/wiatrak.gif')");
    $(boxName).css("backgroundPosition",posX + " " + posY);
    $(boxName).css("backgroundRepeat","no-repeat");
}

function genZdzivBox(myWidth,myHeight,myPadding,boxName,showMe,doBG) {
    $("body").append('<div id="' + boxName + '" class="zdziv"></div>');
    $("#" + boxName).hide();
    $("#" + boxName).css("width",myWidth + "px");
	$("#" + boxName).css("height",myHeight + "px");
    $("#" + boxName).css("padding",myPadding + "px");
    var myPos = getZdzivPosition(boxName);
    $("#" + boxName).css("top",myPos[1]);
    $("#" + boxName).css("left",myPos[0]);
    if(doBG != false)
        giveMeBG("#" + boxName);
    if(showMe != false)
        $("#" + boxName).show();
}


function createPoltergeist() {

	var hg = $("#footer").offset();
	hg = hg["top"];
	hg += $("#footer").outerHeight();	

	$("body").append('<div class="poltergeist"></div>');
	$("div.poltergeist").height(hg);
	$("div.poltergeist").width(document.documentElement.clientWidth);
	$("div.poltergeist").show();

	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		navigWidth = [];
  	$("div.navig select").each(function() {
  		var navigId = $(this).attr("id");
  		navigWidth[navigId] = $(this).width();
  		$(this).width(0);
		});
	}
}

function removePoltergeist() {
	$("div.poltergeist").remove();
	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		for(navigId in navigWidth) {
			$("#"+navigId).width(navigWidth[navigId]);
		}
	}
}

function trim(str) { 
    if (str != null) {
        var i; 
        for (i=0; i<str.length; i++) {
            if (str.charAt(i)!=" ") {
                str=str.substring(i,str.length); 
                break;
            } 
        } 
    
        for (i=str.length-1; i>=0; i--) {
            if (str.charAt(i)!=" ") {
                str=str.substring(0,i+1); 
                break;
            } 
        } 
        
        if (str.charAt(0)==" ") {
            return ""; 
        } else {
            return str; 
        }
    } else return '';
}
