var ajaxMessages = {wait:"..."} 
function addFavourites(id, title)
{
	//var value = document.getElementById(input).value;
	//alert(id);
	function __handlerCitiesList(data)
	{
		$("#ajax_container").empty().append(data);
		PCloseWaitMessage('wait_container', true);
	}
	PShowWaitMessage('wait_container', true);
	var TID = CPHttpRequest.InitThread();
	CPHttpRequest.SetAction(TID,__handlerCitiesList);
	CPHttpRequest.Post(TID, "/ajax.php?id="+id+"&title="+title);
}


$(document).ready(function() {
	
	$('.options .pseudo-link').click(function() {
		addFavourites($(this).attr('rel'), $(this).attr('title'));
		return false;
	});
  
	$('.submit button').click(function() {
		if ($.browser.msie) {
      return $(this).parents('.service-order').find('form').submit();
    } else {
      return $(this).parents('.service-order').find('form').attr('onsubmit');
    };
  });
  
  $('#subscribe button span').click(function() {
      $('#subscribe').submit();
  });
  
  
  $('.header .nav li').hover(function() {
    if ($(this).children('div').length) {
      $(this).addClass('hover').children('div:first').show();
    };
  }, function() {
    $(this).removeClass('hover').children('div:first').hide();
  });
  
  if ($('#site-search').length) {
    var label = 'Поиск по сайту';
    $('#site-search').focus(function() {
      $(this).prev().css('visibility', 'hidden');
    }).blur(function() {
      if ($(this).val() == '' || $(this).val() == label) {
        $(this).prev().css('visibility', 'visible');
      };
    });
  };
  if ($('#news-subscribe').length && $('#news-subscribe').val().length<=0) {
    var label = 'Электронная почта';
    $('#news-subscribe').focus(function() {
      $(this).prev().css('visibility', 'hidden');
    }).blur(function() {
      if ($(this).val() == '' || $(this).val() == label) {
        $(this).prev().css('visibility', 'visible');
      };
    });
  };
  
  Popup.init();
  
  if ($('.faq').length) {
    $('.faq dt span').click(function() {
      if ($(this).parent().next().is('dd')) {
        $(this).parent().next().slideToggle('fast');
      };
    });
  };
  
  if ($('.certificates').length) {
    $('.certificates li a').fancybox({
      'overlayOpacity': '0.8',
      'overlayColor': '#000',
      'titlePosition': 'inside',
      'margin': 10,
      'title': $(this).find('.title').html(),
      'speedIn': 400,
      'speedOut': 400
    });
  };
  if ($('.testimonials').length) {
    $('.testimonials a').fancybox({
      'overlayOpacity': '0.8',
      'overlayColor': '#000',
      'titlePosition': 'inside',
      'margin': 10,
      'title': $(this).find('.title').html(),
      'speedIn': 400,
      'speedOut': 400
    });
  };
  
  if ($.browser.mozilla) {
    $('.image.rc4, .image.rc15').append('<b/>');
  };
  
  if ($('.products-list-basket').length) {
    $('.products-list-basket .basket-remove').hover(function() {
      $(this).parent().addClass('remove');
    }, function() {
      $(this).parent().removeClass('remove');
    });
  };
  
});

var Popup = {
  
  init: function() {
    $('.header .phone span, .aside .help a.pop, .content a[rel=popup]').click(function() {
      Popup.show($(this).attr('href'));
      return false;
    });
    
     $('.product-item .order a').click(function() {
     	$($(this).attr('href')).find("textarea").empty().append(element.NAME);
      Popup.show($(this).attr('href'));
      return false;
    });
    
    $('.basket-controls .order a').click(function() {
//    	var text = "";
//    	for(var key in data.favourites) 
//    		text = text+"\n"+key+". "+data.favourites[key]['NAME'];	
//    		$($(this).attr('href')).find("textarea").empty().append(text);	
//    	
//    	
     	
      Popup.show($(this).attr('href'));
      return false;
    });
    
    $('.popup .close').click(function() {
      Popup.hide('#' + $('.popup:visible').attr('id'));
    });
    $(document).keyup(function(e) {
      if (e.keyCode == 27) {
        Popup.hide('#' + $('.popup:visible').attr('id'));
      }
    });
  },
  
  show: function(popupId) {
    if (popupId == undefined) popupId = '#contacts';
    
    $('#overlay').css({
      opacity: '0.8',
      height: $('html').attr('scrollHeight') + 'px'
    }).click(function() {
      Popup.hide(popupId);
    });
    $('#overlay').fadeIn('fast', function() {
      $(popupId).css('top', Popup.adjustPosition(popupId));
      if ($.browser.msie) {
        $(popupId).show();
      } else {
        $(popupId).fadeIn('fast');
      };
    });
  },
  
  hide: function(popupId) {
    if ($.browser.msie) {
      $(popupId).hide();
      $('#overlay').fadeOut('fast');
    } else {
      $(popupId).fadeOut('fast', function() {
        $('#overlay').fadeOut('fast');
      });
    };
  },
  
  adjustPosition: function(popupId) {
    var popupHeight = $(popupId).height();
    var windowHeight = $(window).height();
    var scrollTop = $(window).scrollTop();
    var scrollHeight = $('html').attr('scrollHeight');
    if (scrollHeight - scrollTop - 52 < popupHeight && windowHeight < popupHeight) {
      return scrollHeight - (popupHeight + 26);
    } else if (windowHeight < popupHeight + 52) {
      return scrollTop + 26;
    } else {
      return ((windowHeight - popupHeight) / 2) + scrollTop;
    }
  }
  
};
