function isNotMax(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;
    var code=e.keyCode?e.keyCode:(e.which?e.which:e.charCode)
    switch (code){
        case 13:
        case 8:
        case 9:
        case 46:
        case 37:
        case 38:
        case 39:
        case 40:
        return true;
    }
    if(target.value.length >= target.getAttribute('maxlength')){
        alert('Максимально количество символов - 160!');
    }
    return target.value.length <= target.getAttribute('maxlength');

}


function showPreview(wrapper,heading,image,title,src,width,height) {
    image.src=src;
    image.width=width;
    image.height=height;
    heading.innerHTML=title;
    previewTimeout = setTimeout(function (){wrapper.display='block'},250);
}

function showFLVPreview(wrapper,heading,title,src,width,height) {
	var flvBlock = '' +
		'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" style="display:block" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="' + width + '" height="' + height + '">' +
		'<param name="allowScriptAccess" value="sameDomain" />' +
		'<param name="allowFullScreen" value="true" />' +
		'<param name="quality" value="high">' +
		'<param name="menu" value="false">' +
		'<param id="nameValueFLV" name="movie" value="/images/popup-player.swf?titleVideo=' + src + '" />' +
		'<param name="quality" value="high" />' +
		'<param name="bgcolor" value="#010101" />' +
		'<embed src="/images/popup-player.swf?titleVideo=' + src + '" quality="high" menu="false" bgcolor="#010101" width="' + width + '" height="' + height + '" name="video" align="middle" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></embed>' +
		'</object>';
	$('#templatePreviewBody').prepend(flvBlock);
	$('#templatePreviewProgressBar').css('display', 'none');
    heading.innerHTML=title;
    previewTimeout = setTimeout(function (){wrapper.display='block'},250);
}

function hidePreview(wrapper,heading,image) {
    clearTimeout(previewTimeout);
    if ('undefined' != typeof image) {
	    image.src="";
	    image.width=0;
	    image.height=0;
    }
    heading.innerHTML="";
    wrapper.display='none'; 
}

function previewMouseFollow(event,wrapper,width,height,winWidth,winHeight,topOffset) {
    pageX=event.pageX;
    pageY=event.pageY;
    previewOffsetTop=(winHeight-height)/2;
    previewOffsetLeft=(winWidth-width)/2;
    correctedTopOffset=previewOffsetTop+topOffset;
    offset=30;
    centered=false;
//    alert("winHeight:"+winHeight+", height:"+height);
    $(".debug-div").text("pageX: "+pageX+", pageY: "+pageY+"\nwinWidth: "+ winWidth+", winHeight: "+winHeight+"\nwidth: "+width+", height: "+height+"\ncorrectedTopOffset: "+correctedTopOffset+", topOffset: "+topOffset);
    if (winHeight > height) {
        if (pageY<correctedTopOffset-offset) {pageY=pageY+offset;}
        else if (pageY>correctedTopOffset+height+offset) {pageY=pageY-offset-height;}
        else {pageY=correctedTopOffset; centered=true;}
    }
    else {
        pageY=topOffset;
        centered=true;
    }
 //    if (centered) {
        if (pageX<winWidth/2) {pageX+=offset;}
        else {pageX=pageX-width-offset;}
/*    }
    else {
        if (pageX<previewOffsetLeft-offset) {pageX=pageX+offset;}
        else if (pageX>previewOffsetLeft+width+offset) {pageX=pageX-width-offset;}
        else {pageX=previewOffsetLeft;}
   }*/
    wrapper.left=pageX+'px';
    wrapper.top=pageY+'px';
}
var indexPage = false;

$(document).ready(function(){
    if(indexPage === true) {
        var block1Height = $('.block1-elastic-bg').height();
        var block2Height = $('.block2-elastic-bg').height();
        if(block1Height > block2Height) $('.block2-elastic-bg').height(block1Height+1);
        else if(block2Height > block1Height) $('.block1-elastic-bg').height(block2Height);
    }

	$("#frm-srch-text").focus(function(){
		if($(this).val() == "Искать"){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val('Искать');
		}
	});
    $("a[rel^='lightbox']").fancybox( {opacity:0.10} );
    $("#flylink").fancybox();
    $('.tooltip-target').each(function(i){
            i++;
            $('body').append('<div class="popupWin1" id="tooltip-content-'+i+'"><div  class="popupContent">'+$('div#tooltip-text-'+i).html()+'</div></div>');
            if($.support.boxModel == true){
                var parWidth = $("#tooltip-content-"+i).width();
                $("#tooltip-content-"+i+" .popupBottom").width(parWidth);
              }
    });
    $('.tooltip-target').ezpz_tooltip({contentPosition: 'aboveRightFollow',
    	stayOnContent: true,
    	offset: 0
    });
    $("#confirmRule").click(function(){
        if($(this).attr('checked')){
            $("#SubmitZayavka").attr('disabled', false).removeClass('disabled');
        }else{
            $("#SubmitZayavka").attr('disabled', true).addClass('disabled');
        }
    });
    
   $(".partners-url").each(function(){
        var encUrl = encodeURIComponent($(this).attr('href'));
        $(this).attr('href', 'redirect.php?url='+encUrl);
   });

});
