$(document).ready(function()
{	

	// Init pdBox
  	$('.thickbox').pdBox({minWidth: 400});
  
	// SEARCH BOX TEXT
	$('input#q').each(function(){
      var btntext = $(this).attr('value');
      $(this).focus(function(){
            text = $(this).attr('value');
            if( text == btntext ){
                $(this).attr('value', '');
            }
      }).blur(function(){
            var text2 = $(this).attr('value');
            if( text2 == '' || text2 == undefined ){
                $(this).attr('value', btntext);
            }
      });
  });
  
  // CHECKBOX
  $('.get-all :checkbox').bind('click', function(){
    if($(this).is('#all')){
      $(':checkbox:not(#all)').attr('checked', false);
      $(this).attr('checked', true);
    }
    else{
      var check = $('.get-all input:not(#prod1):checked').size();
      if ( check > 0){$(':checkbox#all').attr('checked', false);}
      else{$(':checkbox#all').attr('checked', true);}
    }
  });
  
  // QUICK MENU
  $('#quick-links .reset li').each(function(){  	
  	var left = $(this).offset().left;
  	if (left>830) {$(this).addClass('show-r');}
	});
  
  // NEWS LIST
  $('.news-box .in').each(function(){
	    var element = $(this);
        var scrollEl = $('ul.reset', this);
        var scrollSize = $('ul.reset li', this).size();
        var counter = 0;
        var scrollVal = 1;
        var scrollScreenVal = 4;
        var animateTime = 150;
        var prev = $('a.prev', this);
        var next = $('a.next', this);
        if (scrollSize > scrollScreenVal){
            prev.show();
            next.show();
        }
        
        scrollEl.addClass('js');
              
        if (counter == 0){
            prev.addClass('disable');
            scrollEl[0].scrollTop = 0;
        }
    
        next.click(function(){
            counter = scrollSize - scrollScreenVal > counter ? counter + scrollVal : scrollSize - scrollScreenVal;
            scrollEl.animate({
                scrollTop: ($('li:eq('+ counter +')', scrollEl)[0].clientHeight +1) * counter
            }, animateTime);
            prev.removeClass('disable');
            if (counter == scrollSize - scrollScreenVal){
                $(this).addClass('disable');
            }
            return false;
        });
        
        prev.click(function(){
            counter = counter - scrollVal < 0 ? 0 : counter - scrollVal;
            scrollEl.animate({
                scrollTop: ($('li:eq('+ counter +')', scrollEl)[0].clientHeight +1) * counter
            }, animateTime);
            next.removeClass('disable');
            if (counter == 0){
                $(this).addClass('disable');
            }
            return false;
        });
    
    });
      
  // ADJUST HEIGHTS  
  function init(){
        $('.tree-news').adjustHeights({
            elements : ['.in']
        });
        $('.col').adjustHeights({
            elements : ['.tree-news']
        });
        $('.adjust-height').adjustHeights({
            elements : ['.sort']
        });
        $('.att-images').adjustHeights({
            elements : ['li .spc'],
            vAlign : 'center',
			adjust: false           
        });
        $('.galery-list ul.reset ul.reset').adjustHeights({
            elements : ['.img'],
            vAlign : 'center',
			adjust: false         
        });	
		$('.news-box .in').each(function(){
			var height = ($('ul.reset li', this)[0].clientHeight + 1) * 4;
			$('ul.reset', this).css('height', height);
		});
				
    }    
    $(document).fontSizeListener({
        element: '#header',
        callFunction : function(){init()}
    });  
    
 	// Tabs call  
	$('.pages-box .in').tabs({ fxAutoHeight: true });
	 
  	// ACCORDION	
	$('.tree-news').each(function(){
      var element = $(this);
      $(element).addClass('pdAccordion');
			
			$('.in', element).css({
				'overflow' : 'hidden',
				'display' : 'none'
			});
			
			$('.reset li.selected .in', element).show();
			
			$('.reset', element).each(function(){
				var el = $(this);
				$('li', el).each(function(){
					var parent = $(this);						
					$('.heading', parent).click(function(){
						$('li', el).removeClass('selected');
						$(parent).addClass('selected');			
						$('li:not(".selected") div', el).animate({height: "hide"}, 500);			
						$('.in', parent).animate({height: "show"}, 500);			
						
						return false;
					});
				});
			});
  	});
  	
		$('.galeries-detail').each(function(){
			var imgMPath = $('.galery-detail img').attr('src');
			var imgMID = imgMPath.split('/data/imgs/gallery/')[1];
			var imgMID = imgMID.split('m.jpg')[0];
			$('.att-images ul li').each(function(){
				var imgSPath = $('img', this).attr('src');
				var imgSID = imgSPath.split('/data/imgs/gallery/')[1];
				var imgSID = imgSID.split('s.jpg')[0];
				
				if (imgMID == imgSID ) {$(this).addClass('active');}
			});
		});	
		
	// SCROLL 
    $('.att-images .in').each(function(){
        var $this = $(this);
        var scroll = {
            el: $('ul', this)
            ,elWidth: function(){return scroll.el.width();}
            ,elScroll: function(){return scroll.el.attr('scrollWidth');}
            ,elScrollL: function(){return scroll.el.scrollLeft();}
            ,itemWidth: function(){return $('li', scroll.el).width();}
            
            ,activeLi: function(){return $('li', scroll.el).index($('li.active', scroll.el));}
            
            ,coef: 1
            ,hover: function(e){
                e = e || window.event;
                if(e.type == 'mouseenter'){
                    $(e.target).addClass('hover');
                }
                else if(e.type == 'mouseleave'){
                    $(e.target).removeClass('hover');
                }   
            }
            ,controlN: function(){
                  if(left + scroll.elWidth() >= scroll.elScroll()){
                      $('.next', $this).fadeTo("fast", 0.23).addClass('disable').removeClass('hover').unbind();
                  }
                  if($('.prev', $this).is('.disable') && left > 0){
                      $('.prev', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
                  }
            }
            ,controlP: function(){
                if(left <= 0){
                    $('.prev', $this).fadeTo("fast", 0.23).addClass('disable').removeClass('hover').unbind();
                }
                if($('.next', $this).is('.disable') && left + scroll.elWidth() < scroll.elScroll()){
                    $('.next', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
                }
            }
            ,next: function(){
                scroll.el.stop();
                var length = scroll.itemWidth() * scroll.coef;
                left = left + length;
                scroll.controlN();
                scroll.el.animate({
                    scrollLeft: left +'px'
                }, 200*scroll.coef);        
            }
            ,prev: function(){
                scroll.el.stop();
                var length = scroll.itemWidth() * scroll.coef;
                left = left - length;
                scroll.controlP();
                scroll.el.animate({
                    scrollLeft: left +'px'
                }, 200*scroll.coef);        
            }
        };
        
        
        
        if(scroll.elWidth()){
            $this.append('<span class="prev"></span><span class="next"></span>');
            var left = scroll.elScrollL();
            scroll.el.css('overflow-x', 'hidden');
            scroll.controlN();
            scroll.controlP();
            $('.next:not(.disable)', $this).click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
            $('.prev:not(.disable)', $this).click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
        }
        
        if(scroll.activeLi()>=0){
			var left =  scroll.itemWidth() * (scroll.activeLi()-2);
        	scroll.next();
		};
        
        
    });
    
    
    
  // COMMENTS
  $('.comments').each(function(){
  	var ele = $(this);
  	$('.buttons .show', this).click(function(){
		$('.reset li', ele).each(function(){
			$('.head', this).data('status', true);
	  		$('.desc', this).show();
		});	
		return false;
	});	
	$('.buttons .hide', this).click(function(){
		$('.reset li', ele).each(function(){
			$('.head', this).data('status', false);
	  		$('.desc', this).hide();
		});	
		return false;
	});
	
  	$('.reset li', this).each(function(){
	  	var el = $(this);
	  	$('.head', el).data('status', false);
	  	$('.desc', el).hide();
	  	
	  	var comhash = window.location.hash;
		var comhash = comhash.split('#')[1];
		var eleID = el.attr('id');
		status = true;
		if (comhash == eleID)
		{
			$('.head', el).data('status', true);
	  		$('.desc', el).show();
		}
	  	
		$('.head', el).click(function(){
			if( $(this).data('status'))
	    	{
	            $('.desc', el).hide();
	            $(this).data('status', false);
	        }
	        else
	        {
				$('.desc', el).show();
				$(this).data('status', true);
	        }
	        return false;
		});
		
	  });
  });
  
  // FILTER
   $('.next-params').each(function(){
        var $this = $(this);
        
        var hideDiv = function(c){

            if($('div.'+c, $this).filter(':visible').size() > 0){
                $('div.'+c, $this).hide();
                $(':radio:checked', $('div.'+c, $this)).each(function(){
                    var id = $(this).attr('id');
                    hideDiv(id);
                });  
            }        
        };
        
        var showDiv = function(c, $par){
            $($('div.'+c, $this).get()).insertAfter($par).show();
            $(':radio:checked', $('div.'+c, $this)).each(function(){
                var $par2 = $(this).parents('div:first');
                var id = $(this).attr('id');
                showDiv(id, $par2);
            });          
        };

        var $chck = $(':radio', $this);
        $('>div', $this).not('.id0').hide();
        $chck.each(function(){
        	$(this).data('change', $(this).attr('checked'));
            if($(this).is(':checked')){
                var $par = $(this).parents('div:first');
                var c = $(this).attr('id');
                showDiv(c, $par);
            }
        });
        
        $chck.click(function(){
            var $par = $(this).parents('div:first');
            sum = 0;
            if($(this).data('change') != $(this).attr('checked'))
            {
            	$(':radio', $par).each(function(){
            		$(this).data('change', $(this).attr('checked'));
	                var c = $(this).attr('id');
	                if($(this).is(':checked') == true){
	                    showDiv(c, $par);
	                }
	                else{                  
	                    hideDiv(c); 
	                }
	            });	
            }
        });
    });
    
    // LOGIN HOVER
	var timeM = null;
	$('#user-menu .not-logged .login').bind('mouseenter', function (){
		var elem = $(this);
    	timeM = setTimeout(
			function (e) {
				$(elem).addClass('hover');
				$('.sub-t', elem).show();
				
				$(document).bind('click', function(e){
					e = e || window.event;
					if ($(e.target).parents('#user-menu .reset .login').size() <= 0) {
						$('#user-menu .reset .login .sub-t').hide();
						$('#user-menu .reset .login').removeClass('hover');
						$(document).unbind('click');
					}
				});
		    }		
		, 600); 
    	
  	});
	$('#user-menu .not-logged .login').bind('mouseleave', function(){
		clearTimeout(timeM);
    });
	
	// ADMIN ATT IMAGES	
	$('.admin-imgs').each(function(){
		var $this = $(this);
        var $limit = 10;
        if($('.imagesLimit').val() < 10){
            $limit = $('.imagesLimit').val();
        }
		$this.prepend('<div class="help"></div>');
		var count = -1;
		var attImgMain = $('.what-get p', this).html();
		$('.what-get', this).remove();
		$('.std-btn', this).click(function(){
			count = count + 1
			if (count%2==1 && count<$limit) {
				$('.help', $this).append('<p class="col-h-r">'+ attImgMain +'</p><div class="break"></div>');
			}
			else if (count==0 || count<$limit) {
				$('.help', $this).append('<p class="col-h-l">'+ attImgMain +'</p>');
			}
			
			if (count==$limit-1) {
				$(this).hide();
			}
			
			$('label[for="file-0"], label[for="name-0"], label[for="text-0"]', $this).each(function(){
				if ( $(this).hasClass("file") ) {
					$(this).attr('for', 'file'+count);
					$('input', this).attr('id', 'file'+count).attr('name', 'imagesF['+count+']');
				}
				else if ( $(this).hasClass("name") ) {
					$(this).attr('for', 'name'+count);
					$('input', this).attr('id', 'name'+count).attr('name', 'imagesI[name]['+count+']');
				}
				else if ( $(this).hasClass("text") ) {
					$(this).attr('for', 'text'+count);
					$('textarea', this).attr('id', 'text'+count).attr('name', 'imagesI[desc]['+count+']');
				}
				
			});
			
			return false;
		});
	});
	
	// ADMIN ATT FILES	
	$('.admin-files').each(function(){
		var $this = $(this);
        var $limit = 5;
        if($('.imagesLimit').val() < 5){
            $limit = $('.imagesLimit').val();
        }
		$this.prepend('<div class="help"></div>');
		var count = -1;
		var attFilesMain = $('.what-get p', this).html();
		$('.what-get', this).remove();
		$('.std-btn', this).click(function(){
			count = count + 1
			if (count%2==1 && count<$limit) {
				$('.help', $this).append('<p class="col-h-r">'+ attFilesMain +'</p><div class="break"></div>');
			}
			else if (count==0 || count<$limit) {
				$('.help', $this).append('<p class="col-h-l">'+ attFilesMain +'</p>');
			}
			
			if (count==$limit-1) {
				$(this).hide();
			}
			
			$('label[for="sfile-1"], label[for="sname-1"], label[for="stext-1"]', $this).each(function(){
				if ( $(this).hasClass("file") ) {
					$(this).attr('for', 'sfile'+count);
					$('input', this).attr('id', 'sfile'+count).attr('name', 'files['+count+']');			
				}
				else if ( $(this).hasClass("name") ) {
					$(this).attr('for', 'sname'+count);
					$('input', this).attr('id', 'sname'+count).attr('name', 'files[name]['+count+']');			
				}
				else if ( $(this).hasClass("text") ) {
					$(this).attr('for', 'stext'+count);
					$('textarea', this).attr('id', 'stext'+count).attr('name', 'files[desc]['+count+']');			
				}
				
			});
			
			return false;
		});
	});
	
	$('#discussion .newReply').each(function(){
		var el = $(this);
		var intID = $('.int input', this).attr('id');
		intID = intID.split('_')[1];
		intID = parseInt(intID);
		$('.std-btn', this).click(function(){
			if (intID < 3) {
				intID = intID + 1
				$('.int', el).append('<input id="file_'+intID+'" type="file" name="file_'+intID+'"/><br />')
				if (intID == 3) {
					$(this).hide();
				}
			}
			return false;
		});
	});

	$('#temphelper .newReply').each(function(){
		var el = $(this);
		var intID = $('.int input', this).attr('id');
		intID = intID.split('_')[1];
		intID = parseInt(intID);
		$('.std-btn', this).click(function(){
			if (intID < 3) {
				intID = intID + 1
				$('.int', el).append('<input id="file_'+intID+'" type="file" name="file_'+intID+'"/><br />')
				if (intID == 3) {
					$(this).hide();
				}
			}
			return false;
		});
	});
	
    
    // FILTER ADVERTS
    $('.std-form .param-tabs ul').each(function(){
		var $this = $(this);
		$('label', this).click(function(){
			$('li', $this).removeClass('active');
			$(this).parent().addClass('active');
		});
	});
    
	// COMMENTS
	$('.comments .notice').hide();
	$('.comments .reset li .make-reaction').click(function(){
		$('.comments .std-form .notice').show();
	});
		
	// REGIONS
  $('#state').change(function () {
         var hidden = $('#noOptgroup');
         $('#region').attr('disabled', 'disabled');
          $.get("ajax/regions?id="+this.value+"&nogroup="+hidden.val()+"&ide=region&name="
                                  +$('#region').attr('name')+"&class="+$('#region').attr('class'),
          function(data){
              $('#region').replaceWith(data);
              //alert("Data Loaded: " + data);
              //$('#region').append(data);
          });
      });

   $('#stateContact').change(function () {
         var hidden = $('#noOptgroup');
         $('#regionContact').attr('disabled', 'disabled');
          $.get("ajax/regions?id="+this.value+"&nogroup="+hidden.val()+"&ide=regionContact&name="
                                  +$('#regionContact').attr('name')+"&class="+$('#regionContact').attr('class'),
          function(data){
              $('#regionContact').html("");
              //alert("Data Loaded: " + data);
              $('#regionContact').append(data);
          });
      });

    $('#make-clone').click(function(){
              $('tr.clone-this:first').clone(true).insertAfter('tr.term:last');
              $('tr.clone-this:last').removeClass('clone-this');
              $('tr.term:last > :hidden').val(0);
                  var message = 0;
              $('tr.term').each(function(){
                  $(':input', this).each(function(){
                      var name = $(this).attr('name');
                      var newName = name.replace(/(.*)(\d)(.*)/g, "$1"+message+"$3");
                      $(this).attr('name', newName);
                  });
                  message++;
              });
              return false;
          });
    
    $('.remove').click(function(){
                $('.term:has('+this+')').remove();
            });

    $('#series').change(function(){
        $('#rubrics').val(0);
    });

    $('#rubrics').change(function(){
        $('#series').val(0);
    });

    $('#addToSubjectForm').hide();

    $('#addToSubject').click(function(){
        $('#addToSubjectForm').show();
    });	
    
    $('object').each(function(){
    	if ( $('param[name="wmode"]', this).size() > 0){
    		$('param[name="wmode"]', this).attr('value', 'transparent');	
    	}
    	else {
	    	var html = $(this).html();
	    	$(this).html(html+'<param value="transparent" name="wmode"/>');    	
	    }
    });
    
    $('embed').attr('wmode', 'transparent');
    $('embed, object').each(function(){
		var embedParent = $(this).parent();
	    var embedParentHtml = embedParent.html();
	    embedParent.html(embedParentHtml);
	});
    
    
    $('object').parent().css('position', 'relative');
})


