
$(document).ready(function(){
   if($.browser.msie) {
    try {
      document.execCommand('BackgroundImageCache', false, true);
    } catch(e) {}
   }
   $('#suggestions li').click(function(){
     
     if ($(this).hasClass('b-closed')) {
        $(this).find('.b-descr').slideDown('normal');
        $(this).removeClass('b-closed').addClass('b-opened');
     }
     /*else {
        $(this).parent().find('.b-descr').slideUp('normal');
         $(this).parent().addClass('b-closed');
     } */
   }).hover(function(){
     if ($(this).hasClass('b-closed')) {
        $(this).addClass('b-hover');
     }
   },function(){
      if ($(this).hasClass('b-closed')) {
        $(this).removeClass('b-hover');
      }
   });
   $('#suggestions .b-toggle').click(function(e){
       if (!$(this).parent().hasClass('b-closed')) {
           e.stopPropagation();
           $(this).parent().find('.b-descr').slideUp('normal');
           $(this).parent().addClass('b-closed').removeClass('b-opened');
       }
   });
    //select in tab
   $('.b-current .b-selection li').live('click',function(){

     	
     	var section = $(this).parent().parent().parent().attr('id');
         if (!$(this).hasClass('b-selected')) {
         	
         	var sel=$('.b-current .b-selection li').index($(this));
         	
         	var me=$(this);
            $('.b-current .b-selection .b-selected').removeClass('b-selected');
            $(this).addClass('b-selected');
             
//			$.get(siteURL+'/test.php', { sectionClick: section, tabClick: sel  }, function(sData) { 
//				alert("Data Loaded: " + sData);
//				var json = eval('(' + sData + ')');
			var langId = $('#langId').html();
//			alert(langId);
			$.getJSON(siteURL+'/test.php', { sectionClick : section, tabClick : sel, langId : langId }, function(json) { 
				
				//alert(sel);
				//alert(json.cnt);
				//alert(json.elem[1].name);
				//alert(json.cnt);

				var selector = 	'#'+section+'-list li';			

				$(selector).each(function(i) {
//					alert(i);
					if (i<json.cnt) {
						//alert(json.elem[i].name);
						var aElement=$(this).find('.b-point a'),currentHref=aElement.attr('href');
						//alert(i);

						$(this).find('.b-title').html(json.elem[i].name);
						//alert($(this).find('.b-title').html());
						$(this).find('.b-date').html(json.elem[i].public_date);
						$(this).find('.b-annotation').html(json.elem[i].annotation);
						$(this).find('.b-code').html(json.elem[i].profile_id);
						var icon=$(this).find('.b-l-icon');
						if (icon.hasClass('b-rus')) {
							icon.removeClass('b-rus');
						}
						else {
							icon.removeClass('b-fr');
						}
						//alert(json.elem[i].icon_class);
						icon.addClass(json.elem[i].icon_class);
						
						aElement.attr('href',json.elem[i].id);
						$(this).show();
					}
					else {
						$(this).hide();
					}
					
				});
			});

         }
         return false;
     });
   //tabs click
   $('.b-tabs dt').click(
        function() {
          if  (!$(this).hasClass('b-selected')) {
            var i=$('.b-tabs dt').index($(this));
            $('.b-tabs dd').removeClass('b-current');
            $('.b-tabs dd').eq(i).addClass('b-current');
            $('.b-tabs dt.b-selected').removeClass('b-selected');
            $(this).addClass('b-selected');
            //ajax here
          }
        }
   );
//   $('.b-search .b-button').click(function(){
//       return false;
//   });
var editFlag=false,inputValue;
inputValue=$('.b-search .b-field').attr('value');
    $('.b-search .b-field').focus(function(){
        if ((inputValue=='Поиск по предложениям') || (inputValue=='Поиск по запросам')||(inputValue=='Поиск')) {
            $(this).attr('value','');
        }

    }).blur(function(){
       if ($(this).attr('value')=='') {
    	   if ($(this).attr('id') == 'search_offers')
    		   $(this).attr('value','Поиск по предложениям');
    	   if ($(this).attr('id')=='search-inner')
    	   		$(this).attr('value','Поиск');
    	   else
    		   $(this).attr('value','Поиск по запросам');
        }
    }).keyup(function(e){
    	
        if(e.keyCode!='32') {
	    	var button=$(this).parents('.b-search').find('.b-button');
	    	editFlag=true;
	    	if ($(this).attr('value')!='') {
	    		button.removeClass('b-disabled').removeAttr('disabled');
	    	}
	    	else {
	    		button.addClass('b-disabled').attr('disabled','disabled');
	    	}
        }
    });
    $('.b-search').submit(function(){
    	if (!editFlag) {
    		return false;
    	}
    	
    });
    $('.b-special .b-clickable span').toggle(function(){
        $(this).parent().removeClass('b-clickable');
        $(this).parent().parent().find('.b-answer').slideDown('normal');
    },function(){
        $(this).parent().addClass('b-clickable');
        $(this).parent().parent().find('.b-answer').slideUp('normal');
    });
    /*all about popup*/
    $('.b-popup .b-close').click(function(){
        $('.b-popup').hide();
        $(".b-overlay").hide();
    });
    $('.b-popup').click(function(e){
		 e.stopPropagation();
	});
    $('.b-interest').click(function(e){
        e.stopPropagation();
         var arrayPageSize = getPageSize();
         var arrayPageScroll = getPageScroll();
		 var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		 var lightboxLeft = arrayPageScroll[0];
         $('.b-popup').show().css('visibility','hidden');
         $('.b-popup .b-small-title2').html($('#suggestion_title').html());
         var addHeight=Math.abs($('.b-popup').height()+200-arrayPageSize[3]);
      
		$(".b-overlay").css({width: parseInt($('body').css('padding-left'))*2+$('body').width(), height: arrayPageSize[1]+addHeight+'px'}).show();
        $('.b-popup').css({top:lightboxTop+'px',visibility:'visible'}).show();

    });
    $('body')
	    .keypress(function(e) {
	      if(e.keyCode==27) {
	        $('.b-popup:visible').each(function() {
	          $(this).hide();
              $(".b-overlay").hide();
	        });
	      }
	    })
	     .click(function(e) {
	      $('.b-popup:visible').each(function() {
	        $(this).hide();
            $(".b-overlay").hide();
	      });
	});
    function getPageScroll(){
            var xScroll, yScroll;

            if (self.pageYOffset) {
                yScroll = self.pageYOffset;
                xScroll = self.pageXOffset;
            } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
                yScroll = document.documentElement.scrollTop;
                xScroll = document.documentElement.scrollLeft;
            } else if (document.body) {// all other Explorers
                yScroll = document.body.scrollTop;
                xScroll = document.body.scrollLeft;
            }

            var arrayPageScroll = new Array(xScroll,yScroll);
            return arrayPageScroll;
    };
    function getPageSize(){
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;

		if (self.innerHeight) { // all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}


		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}

		var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	};
    var col=0,counter=0,num=$('.b-req').length,errorArray='';
    function checkFields(){
        col=0;
        errorArray='';
        $('.b-req').each(function(){
                    var i=$('.b-req').index($(this));
                    if ($(this).attr('value')!='') {
                        col++;
                        
                        if(($(this).attr('id')=='email')&&!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test($(this).attr('value'))) {
                            errorArray+=$(this).parent().find('label').text()+', ';
                            col--;
                        }

                    }
                    else {
						if ($(this).attr('id')=='code') {
                        	errorArray+='Кодовое число, ';
                        }
                        else {
                        	errorArray+=$(this).parent().find('label').text()+', ';
                        }
                    
                    }

                 });
    };
    checkFields();
    $('.b-form-layout').submit(function(e){
            checkFields();
            if (col==num) {
            }
            else {
                e.preventDefault();
                $('.b-form-layout .b-error').empty();
                errorArray=errorArray.substr(0,errorArray.length-2)+'.';
                $('.b-form-layout .b-error').html('<span>Поля пустые или заполнены неправильно:</span> '+errorArray);
            }
    });
});