function CreateOverlay() {
    var overDiv = $('<div/>');
    $(overDiv).attr({
        id: 'id-overDiv'
    }).css({
        width: '100%',
        height: $(document).height(),
        'background-color': '#7A7A7A',
        opacity: '0.75',
        'z-index': '1000',
        position: 'absolute',
        left: 0,
        top: 0
    }).click(function () {
        closePopupDiv();
    });
    return overDiv;
}

function closePopupDiv() {
    if ($('#id-overDiv').is(':visible')) {
        $('div:first').fadeOut(function () {
            $('#id-overDiv').fadeOut('fast').remove();
        });
    }
}

function goToUrl(url){
  window.location.href = url;
}

jQuery.fn.center = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}

jQuery.fn.centerElement = function() {
    this.css("position", "absolute");
//     alert($(this).prev().height())
//     this.css("top", ($(this).parent().height() - this.height()) / 2 + "px");
//     this.css("left", ($(this).parent().width() - this.width()) / 2 + "px");
    this.css("top", ($(window).height() - this.height()) / 2 + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + "px");
    return this;
}

function positionLightboxImage() {
  var top = ($(window).height() - $('#lightbox').height()) / 2;
  var left = ($(window).width() - $('#lightbox').width()) / 2;
  $('#lightbox')
    .css({
      'top': top + $(document).scrollTop(),
      'left': left
    })
    .fadeIn();
}

function removeLightbox() {
  $('#overlay, #lightbox, #closeBTN')
    .fadeOut('slow', function() {
      $(this).remove();
      $('body').css('overflow-y', 'auto'); // show scrollbars!
    });
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function createEmbedBanner(src, width, height, embID) {

    embCode = '<embed type="application/x-shockwave-flash" src="' + src + '"'
    embCode += ' width="' + width + '" height="' + height + '" '
    embCode += ' id="' + embID + '" '
    embCode += ' name="miniMovieSwf" '
    embCode += ' quality="hight" '
    embCode += ' wmode="transparent"'
    embCode += ' allowfullscreen="false" allowscriptaccess="always" '
    embCode += '</embed>'

    return embCode;
}


function ShowHideLoaderBtn (elHide){
  parentElement = $(elHide).parent();
  
	if ($(elHide).is(':visible')){
	  $(elHide).hide();
		$('<img />')
			.attr({
				src: 'images/ajax_anim.gif',
				title: 'Loading...',
				id: 'AjaxAnim'
			})
			.css({
				'width': '21px',
				'height': '5px',
				'border': 'none'
			})
			.appendTo(parentElement);
	}
  else {
		if ($('#AjaxAnim').length > 0){
			 $('#AjaxAnim').remove();
		}
		$(elHide).show();
	}

}

function cleanFromMailer(mode){
     idFrom  = mode
     objForm = document.getElementById(idFrom)
     objForm.reset();
     if (document.getElementById('imgCaptcha')){
        refCaptcha()
      }
    }


function refCaptcha(){

  var currentTime = new Date();
  var curTime = currentTime.getSeconds();
  var rand_no = Math.random();


  $('#imgCaptcha').doTimeout(100,function(){
  
      $('#imgCaptcha').attr('src','captcha.asp?'+rand_no);
    });
 }


function NewWindow(mypage, myname, w, h, scroll) {

  var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll
  win = window.open(mypage, myname, winprops)
  win.window.focus();

}

function submit(idForm){
  idForm = $('#'+idForm);
  $(idForm).submit();
}
		  
      
      function rotateTicker(){
      	pos_quote = Math.floor(Math.random()*(quotes.length));
    		tickerText = quotes[pos_quote];
      	c = 0;
      	typetext();
      	setTimeout( "rotateTicker()", 9000 );

      }
      
      var isInTag = false;
      
      function typetext() {	
      	var thisChar = tickerText.substr(c, 1);
      	if( thisChar == '<' ){ isInTag = true; }
      	if( thisChar == '>' ){ isInTag = false; }
      	$('#ticker-area').html("&nbsp;" + tickerText.substr(0, c++));
      	if(c < tickerText.length+1)
      		if( isInTag ){
      			typetext();
      		}else{
      			setTimeout("typetext()", 50);
      		}
      	else {
      		c = 1;
      		tickerText = "";
      	}	
      }

		  


/*  DOCUMENT READY  */

$(document).ready(function(){

  $('#refCaptcha')
  .hover(
  function(){
    $(this).css({opacity: "1"})
  },
  function(){
    $(this).css({opacity: ".7"})
  }  
  )  
  .click(function(){
    refCaptcha()
  })

	$('#talkForm').submit(function() {
	    $(this).ajaxSubmit(optionsAjaxTalk);
	    return false;
	});

	$('a[href^=http://]').attr('target','_blank');

	mainmenu(); //Запуск меню верхнего

  rotateTicker();
	strSearchFld = $('.inp-f-search').val();

  // Обнуление поиского поля по фокусу
  $('.inp-f-search').focus(function(){
    var curValue = $('.inp-f-search').val();
    var defValue = strSearchFld;
    var def = defValue.toLowerCase()
    var cur = $('.inp-f-search').val().toLowerCase()
    var $thisFld = $(this);
    
    if (def === cur){
        $(this).val('');
    }

    $thisFld.blur(function(){
      if($thisFld.val() === defValue || $thisFld.val() ==='' )
      {
        $thisFld.val(defValue);
        return false;
      }
    });
  });

  $('.btn-subscribe').click(function(e){
      //     if ($('#YMP0').val() == ''){
      //       jAlert(errEMAIL, errTITLEWINDOW)
      //       return false;
      //     }
      e.preventDefault();
      checkSubscribeForm();
  });

	$('.jq-logo').css('cursor','pointer').attr('title',strURLWEBSITE).click(function(){
	    goToUrl(strURLWEBSITE);
	});

});
//******************************************//

var optionsAjaxTalk = {
    target:        '#messageAlert',   // target element(s) to be updated with server response
    beforeSubmit:  checkMailerTalk,  // pre-submit callback
		success:       showResponse
};


function checkSubscribeForm() {
   var formname = "subscribe"
   var errors = '';
   errors += checkEmail(formname, 'YMP0', errEMAIL, 'True');

   if (errors != '') {
       jAlert(errors, errTITLEWINDOW)
       return false;
     } else
    $('#subscribe-form').submit()
}


function showResponse(){
			$('#messageAlert').fadeIn();	 
}

function checkMailerTalk() {
   var formname = "talkForm"
   var errors = '';
   errors += checkText(formname, 'fname', errFNAME);
   errors += checkText(formname, 'address', errADDRESS);
   errors += checkText(formname, 'phone', errPHONE);
   errors += checkText(formname, 'message', errMESSAGE);

   if (errors != '') {
       jAlert(errors, errTITLEWINDOW)
       return false;
     }
}


function mainmenu() {
            var Duration = 150;
            //$(" .drospdown ul ").css({ display: "none" }); // Opera Fix
            $(" .drospdown li").hover(function () {

                var thisli = $(this);
                var wPar = thisli.outerWidth();
                var hPar = thisli.height();
                var yPar = thisli.offset().top;
                var xPar = thisli.offset().left;
								var thisa = $(" .drospdown li a[href=#]")
								var wThisa = thisa.width();
								thisa.click(function(e){
										e.preventDefault();
								});
								thisa.css('text-decoration','none');
                thisli
                .find('ul:first')
                .css({
                    position: "absolute",
                    top: yPar + hPar,
                    left: xPar + wThisa/3,
                    display: "none",
                    'z-index':'500'
                })
                .fadeIn('fast', function(){
                   thisli.css({'border-bottom':'1px solid #fff'});
                 });
            },

            function () {
                $(this)
                .find('ul:first')
                .fadeOut()
                .end()
                .css({'border':'1px solid #fff'});
            });
}

