var J = jQuery.noConflict();
var text_height = 30;
var flag = false;
J(document).ready(function() {

    J('.new-head .main-menu li UL').width(J(window).width());
    if ( J('.new-head .main-menu li.active UL LI').size() == 0){
        J('.new-head .main-menu li.active UL').hide();
    }

    J('.new-head .main-menu>li').hover(function(){       
        if (!J(this).hasClass('active')){
            J('.new-head .main-menu li.active UL').hide();
        }
        J(this).addClass('curr');
        if (J(this).find('li').size() == 0){
            J(this).find('ul').hide();
        }
    }, function(){
        if ( J('.new-head .main-menu li.active UL LI').size() > 0){
            J('.new-head .main-menu li.active UL').show();
        }
        J(this).removeClass('curr');
    });
	if ( !(J.browser.msie && J.browser.version == 6) ){
        J('select.select-one').selectbox({animationSpeed:0});
        J('select.select-two').selectbox({className: 'select-grey', animationSpeed:0});
    } 
	//J('.check-on input[type="checkbox"]').custCheckBox();
	//J('.radio-on input[type="radio"]').custCheckBox();
	
	resizeArrow();
	
	J('textarea').focus(function(){
        new_sz(this);
    });
	J('textarea').keyup(function(){
		new_sz(this);
	});
	
	
	getWidthPages();
	
	J('li a.page').hover(function(){
		J(this).parent().addClass('active');
	}, function(){
		J(this).parent().removeClass('active');
	});
	
	J('.search-res-1 .search-carousel li').hover(function(){
		J(this).find('.elem').addClass('select-li');
	},function(){
		J(this).find('.elem').removeClass('select-li');
	}); 
	
	J('.col-r UL li A').hover(function(){
		J(this).parent().parent().parent().addClass('sel');
	},function(){
		J(this).parent().parent().parent().removeClass('sel');
	});
	
	
	J(window).resize(function(){
        resizeArrow();
        J('.new-head .main-menu li UL').width(J(window).width());

        if (J('.gal2').size() > 0){
            addSeparator();
        }
    });

    
	if (J('.gal2').size() > 0){
		addSeparator();
	}
	
});

function getWidthPages(){
	if (J('li span.fl').size() > 0){		
		J('li span.fl').each( function(){
			var el = J(this).parent();
			var fl = el.find('span.fl').width();
			var ct = el.find('span.ct').width();
			var fr = el.find('span.fr').width();
			var mar_fr = parseInt(el.find('span.fr').css('margin-left'));
			var mar_fl = parseInt(el.find('span.fl').css('margin-right'));
			var sum = mar_fl + mar_fr + ct + fr + fl;
			el.width(sum);
		});
	}
}

function new_sz(obj) {
    var new_height = parseInt(J(obj).attr('scrollHeight'))
    if (text_height < new_height ){
        if (!flag){
            J(obj).css('padding-bottom', text_height + 'px')
            flag = true;
        }
        if (J.browser.safari){
            padd = parseInt(J(obj).css('padding-bottom')) +  parseInt(J(obj).css('padding-top'));
            new_height = new_height - padd;
        }
        J(obj).height(new_height);
    }
}

function sz(t) {
    a = t.value.split('\n');
    b=1;
    for (x=0;x < a.length; x++) {
        if (a[x].length >= t.cols) b+= Math.floor(a[x].length/t.cols);
    }
    b+= a.length;
    if (b > t.rows) t.rows = b;
}

function resizeArrow(){
	var height = parseInt(J('.search-res .search-cont').height()) - 6;
	J('.search-res .arrow A,.search-res .arrow A .c, .search-res .arrow A .t, .search-res .arrow A .b').height(height);
	
}

function addSeparator(){	
	
	J('.gal2-content LI.separator').remove();
	var size = J('.gal2 LI').size();
	var widthLi = J('.gal2 LI').width();
	var widthUl = J('.gal2').width();
	var padd = J('.gal2 LI:first').css('padding-left').replace('px','');
	widthLi = parseInt(widthLi) + parseInt(padd);
	var count = parseInt(widthUl / widthLi);
	J('.gal2 LI').each(function(i){
		if (((i+1) % count) == 0 && (i+1) != size){
			J(this).after('<li class="separator"><div></div></li>');
		}
	});
}

function change_active(num){
      var selected='#'+num;
      J('.select-elem').removeClass('select-elem');

       J(selected).addClass('select-elem');
     }

function change_li(el, li_class){
        J('UL.tab LI').removeClass(li_class);
        J(el).parent().addClass(li_class);
    }

function change_id_li(elem_id){
        J('UL.tab LI').removeClass('active');
        J('#'+elem_id).addClass('active');
    }

 function CountColumnsAndSetClass(){
    if ( document.documentElement.offsetWidth - 810 >= 390 ) {
        document.getElementById( "col-2" ).style.width = document.documentElement.offsetWidth - 810 + "px";
    } else {
        document.getElementById( "col-2" ).style.width = "99.5%";
    }
     }

    function turnvisible() {
		document.getElementById('big_photo').style.visibility="visible";
	}

  function runTest() {
    hCarousel = new UI.Carousel("horizontal_carousel");
  }

function insertAtCursor(myField, myValue) {
   
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
                  + myValue
                  + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
  myField.focus();
}

 function pc_res() {
      parent.J.colorbox.resize({width:'250px', height: '200px'});
     document.getElementById('pcid').innerHTML='sending....';

}






