function showOverlay (){
    $('body').prepend('<div id="modal-overlay"></div>');
    $('body').css('overflow-x', 'hidden');
    $('#modal-overlay').css({
      'background-color':'#000000',
      'bottom':'0',
      'height':'100%',
      'left':'0',
      'margin':'0',
      'opacity':'0.94',
      'padding':'0',
      'position':'fixed',
      'right':'0',
      'top':'0',
      'width':'100%',
      'z-index':'10000'
    });
}

function removeOverlay(){
  if ( $('#modal-overlay').length > 0 ){
    $('#modal-overlay').fadeOut().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;
}


$(document).ready(function(){

$('#projectGallery div:has(span)').hover(function(){
  //alert($(this).children('span').length)
  $(this).children('span').css({
  'position':'absolute',
  'top':parseFloat($(this).position().top) + parseFloat($(this).height()) + 18,
  'left':$(this).position().left + 5,
  'max-width':$(this).parent().width() - 40,
  'min-width': '240px',
  'min-height': '20px'
  }).show();
}, function(){
  $(this).children('span').hide('fast');
  
});

$('#projectGallery div').click(function(e) {
    //$('body').css('overflow-y', 'hidden'); // hide scrollbars!
    
    $('<div id="overlay"></div>')
      .css({
			//'top': $(document).scrollTop()),
      'bottom':'0',
      'height':'100%',
      'left':'0',
      'margin':'0',
      'opacity':'0.94',
      'padding':'0',
      'position':'fixed',
      'right':'0',
      'top':'0',
      'width':'100%',
 			'background': 'black url(../images/spinner.gif) no-repeat scroll center center'
			})
      .css('opacity', '0')
      .animate({'opacity': '0.9'}, 'slow')
      .appendTo('body');
      
    $('<div id="lightbox"></div>').css({'direction':'ltr'})
      .hide()
      .appendTo('body');
    
		File = $(this).find('img').attr('bFile').replace('../','');
		extFile = File.indexOf('.swf') > -1;

		if (!extFile) {

        $('#lightbox').html('<div style="position:relative;"></div>')

        $('<img />')
          .attr('src', File)
          .load(function() {
            positionLightboxImage();
    				$('#overlay').css('background-image','none');

            $('<img />')
            .attr({
            'src':'../images/fileclose.png',
            'id':'closeBTN',
            'title':'Close'
            })
            .appendTo('#lightbox')
            .css({
            'position':'absolute',
            'cursor': 'pointer',
            'z-index':'10000',
            'top': "-25px",
            'left': ($('#lightbox').width() - 5) + "px"
            })
            .click(function() {
              removeLightbox();
            })
            .show();
            
          })
          .click(function() {
            removeLightbox();
          })
          .appendTo('#lightbox div');


    }	else {


        var fWidth = $(this).find('img').attr('fWidth');
        var fHeight = $(this).find('img').attr('fHeight');

        var banner = createEmbedBanner(File, fWidth, fHeight, 'swfBanner')

        $('#lightbox')
        .css({'width':(parseInt(fWidth)+50),'height':(parseInt(fHeight)+50),'z-index':'10500','background-color':'#000'})
        .html('<div></div>');

        $('#lightbox div').css({'width':(parseInt(fWidth)+50),'height':(parseInt(fHeight)+50),'position':'relative'});
          $('<img />')
          .attr({
          'src':'../images/fileclose.png',
          'id':'closeBTN',
          'title':'Close'
          })
          .appendTo('#lightbox')
          .css({
          'position':'absolute',
          'cursor': 'pointer',
          'z-index':'10000',
          'top': "-25px",
          'left': ($('#lightbox').width() - 5) + "px"
          })
          .click(function() {
            removeLightbox();
          })

        $('#lightbox').center().fadeIn(function(){

          $(banner)
          .appendTo('#lightbox div')
          .css({
          'position':'relative',
          'z-index':'100',
          'top': ($('#lightbox div').height() - fHeight) / 2 + "px",
          'left': ($('#lightbox div').width() - fWidth) / 2 + "px"
          })
          .fadeIn();

          });
       }
		    return false;
  });
});

function ShowHideLoaderBtn (elHide){
	if ($(elHide).is(':visible')){
	  $(elHide).hide();
		$('<img />')
			.attr({
				src: 'images/ajax_anim.gif',
				title: 'Loading...',
				id: 'AjaxAnim'
			})
			.css({
				'width': '21px',
				'height': '5px'
			})
			.appendTo('li.buttons');
	}
  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();

}


