$(document).ready(function() {

	div1 = $('div.lastNews .content');
	div2 = $('div.lastForum .content');
	if (div1.height() < div2.height()) {
		div1.css('min-height',div2.height()-6);
	} else {
		div2.css('min-height',div1.height()+6);
	}


// Change text value 4 all text fields
	$('form input').focus(function(){
		if ($(this).attr('value') == $(this).attr('default_val')) {
			$(this).attr('value','');
		}
	})
	$('form input').blur(function(){
		if ($(this).attr('value') == '') {
			$(this).attr('value',$(this).attr('default_val'));
		}
	})
	$('#search a.cancel, #contentSearch a.cancel, ').click(function(){
		$(this).prev().attr('value','').focus();
		return false;
	});


	$('#fader, div.popup .close').live('click', function(){
		$('div.popup').fadeOut();
		$('#fader').fadeOut();
		return false;
	})

	$('#participate').live('click', function(){
		$('#participateForm').fadeIn();
		$('#fader').fadeIn();
		return false;
	})

	/*
	$(document).keydown(function(e) {
		if (e.keyCode == 27) {
		$('div.popup').fadeOut();
		$('#fader').fadeOut();
		}
	});
	*/

	
	
// radio and checkbox buttons change	
	$('#vote input').change(function() {
		 $('#vote input').each(function(){
			if ($(this).attr('checked')) {
				$(this).parent().find('span').addClass('selected');
			} else {
				$(this).parent().find('span').removeClass('selected');
			}
		})
	})
	$('#vote div.oneItem span').live('click', function() {
		$(this).parents('form').find('span.selected').removeClass('selected');
		$(this).addClass('selected');
		$(this).parent().find('input').attr('checked','checked');
	})
	

// articles carousel
	var scroller = $('#carousel ul');
	var contWidth = $('#carousel').width();
	var scrollerWidth = 0;
	var scrollerLeft = 0;
	var marRight = 0;
	$('#carousel ul li').each(function(){
		if ($('#main').hasClass('frontpage')) {
			marRight = 44;
			scrollerWidth = scrollerWidth+$(this).width()+marRight;
		} else if ($('#main').hasClass('news')) {
			marRight = 10;
			scrollerWidth = scrollerWidth+$(this).width();
		} else if ($('#main').hasClass('companyView')) {
			marRight = 8;
			scrollerWidth = scrollerWidth+$(this).width();
		} else {
			marRight = 16;
			scrollerWidth = scrollerWidth+$(this).width();
		}
	})
	scroller.width(scrollerWidth);
	scrollerWidth = scrollerWidth-marRight;

	$('a.prev').addClass('inactive');
	if (contWidth > scrollerWidth) $('div.articles a.next').addClass('inactive');

	$('a.prev:not(.inactive)').live('click', function(){
		scroller.stop(function(){
			scrollerLeft = scroller.position().left;
		})
		$('a.next').removeClass('inactive');
		if (-scrollerLeft < contWidth) {
			shiftVal = 0;
			scrollTime = contWidth+scrollerLeft;
			$('a.prev').addClass('inactive');
		} else {
			shiftVal = contWidth+scrollerLeft+marRight;
			scrollTime = 1000;
		}
		scroller.animate({'left':shiftVal}, scrollTime, function(){
			scrollerLeft = scroller.position().left;
		});
		return false;
	});
	
	$('a.next:not(.inactive)').live('click', function(){
		scroller.stop(function(){
			scrollerLeft = scroller.position().left;
		})
		$('a.prev').removeClass('inactive');
		if (2*contWidth >= scrollerWidth+scrollerLeft*1) {
			shiftVal = contWidth-scrollerWidth;
			scrollTime = scrollerWidth+scrollerLeft-contWidth;
			$('a.next').addClass('inactive');
		} else {
			shiftVal = -contWidth+scrollerLeft-marRight;
			scrollTime = 1000;
		}
		scroller.animate({'left':shiftVal}, scrollTime, function(){
			scrollerLeft = scroller.position().left;
		});
		return false;
	});

	$('a.inactive').live('click', function(){
		return false;
	});
	
	$('div.tovar #carousel li, div.article #carousel li').live('click', function(){
		$('#carousel li.selected').removeClass('selected');
		$(this).addClass('selected');
		$('div.big img').attr('src', $(this).find('img').attr('imgUrl'));
		return false;
	})


// catalog saler popup

	$('a.button').live('click', function(){
		$('div.active').hide().removeClass('active');
		$(this).parent().find('div.salerInfo').fadeIn(100).addClass('active');
		return false;
	});
	$('div.active a.close').live('click', function(){
		$(this).parent().hide().removeClass('active');
		return false;
	});


// rating

	rateVal = 0;
	$('div.rating').find('input').val(rateVal);
	$('div.rating span.rateBG').live('mousemove', function(e){
		mPos = e.pageX-$(this).offset().left;
		if (mPos >= 71) {
			$(this).find('span.rateFill').width(86);
			rateVal = 5;
		} else if (52 <= mPos && mPos < 70) {
			$(this).find('span.rateFill').width(68);
			rateVal = 4;
		} else if (34 <= mPos && mPos < 52) {
			$(this).find('span.rateFill').width(50);
			rateVal = 3;
		} else if (16 <= mPos && mPos < 34) {
			$(this).find('span.rateFill').width(32);
			rateVal = 2;
		} else if (mPos < 16) {
			$(this).find('span.rateFill').width(14);
			rateVal = 1;
		}
	})
	$('div.rating span.rateBG').click(function(){
		$(this).parents('div.rating').find('input').val(rateVal);
	})
	$('div.rating span.rateBG').live('mouseout', function(){
		$(this).find('span.rateFill').width($(this).parents('div.rating').find('input').val()*18);
	});


// comments 

	$('a.addcomment').click(function(){
		$(this).parent().find('form').slideToggle();
		return false;
	})

	$('div.comments div.commentsList').each(function(){
		$(this).find('div.item:odd').css('background-color','#FDFCF6');
	})

	$('a.more').click(function(){
		$(this).next().show();
		$(this).remove();
		return false;
	})


// required fields

	$('div.required input, div.required textarea').each(function(){
		if ($(this).val() != '') {
			$(this).parents('div.required').addClass('ok')
		} else {
			$(this).parents('div.required').removeClass('ok')
		}
	});

        // initial check
        inpChckInit = 0;
        txtChckInit = 0;

        inpChckCount = $("form.companyEdit div.required input").length;
        txtChckCount = $("form.companyEdit div.required textarea").length;

        $('form.companyEdit div.required input').each(function(){
                if ($(this).val() != '') {
                        inpChckInit++;
                        $(this).parents('div.required').addClass('ok')
                } else {
                        $(this).parents('div.required').removeClass('ok')
                }
        })
        $('form.companyEdit div.required textarea').each(function(){
                if ($(this).val() != '') {
                        txtChckInit++;
                        $(this).parents('div.required').addClass('ok')
                } else {
                        $(this).parents('div.required').removeClass('ok')
                }
        })
        if (txtChckInit == txtChckCount && inpChckInit == inpChckCount) {
                $('form.companyEdit input.submit').attr('disabled','')
        } else {
                $('form.companyEdit input.submit').attr('disabled','disabled')
        }

	$('div.required input, div.required textarea').live('keyup', function(){
		inpChck = 1;
		txtChck = 1;
		$('form.companyEdit div.required input').each(function(){
			if ($(this).val() != '') {
				inpChck = 1;
				$(this).parents('div.required').addClass('ok')
			} else {
				inpChck = 0;
				$(this).parents('div.required').removeClass('ok')
			}
		})
		$('form.companyEdit div.required textarea').each(function(){
			if ($(this).val() != '') {
				txtChck = 1;
				$(this).parents('div.required').addClass('ok')
			} else {
				txtChck = 0;
				$(this).parents('div.required').removeClass('ok')
			}
		})
		if (txtChck && inpChck) {
			$('form.companyEdit input.submit').attr('disabled','')
		} else {
			$('form.companyEdit input.submit').attr('disabled','disabled')
		}
	})
	

// add images fields

	var i = 2;
	$('form.companyEdit div.images a.add').live('click', function(){
		addNew = $(this).parents('div.images').find('div.oneRow:first');
		addNew.find('input').attr('id','photo'+i);
		i++;
		//$(this).parents('div.oneRow');
		$('form.companyEdit div.images').append('<div class="oneRow">'+addNew.html()+'</div>');
		$(this).remove();
		return false;
	});


	$('#main.tenders ul.tendList li').mouseover(function(){
		$(this).css('background','#FCFAF0');
	});
	$('#main.tenders ul.tendList li').mouseout(function(){
		$(this).css('background','none');
	});

	$("#main a").each(function(){
		var temp = $(this).text();
		if(!$(this).attr("href") && $(this).text().substring(0,4) == "http" ){
			$(this).attr("href", $(this).text());
		}
		if(!$(this).attr("href") && $(this).text().substring(0,4) != "http" ){
			
			$(this).addClass("no_href");
		}
	});
	
	$("input#price").blur(function(){
		if($(this).val()*1>99000){
			$(this).val("99000");
			alert($(this).attr("error_msg"));
		}
	});
	
});








