var corners = true;
$(document).ready(function(){
	corners = allowCorners();
	
	if(corners){
		$('.rounded').corners('7px');
		$('.rounded-small').corners('3px');
	}
	initInputFields();
});

function allowCorners(){
	return !(/MSIE (\d+\.\d+)/.test(navigator.userAgent) || /Chrome/.test(navigator.userAgent) || /Opera/.test(navigator.userAgent) || /Firefox\/2/.test(navigator.userAgent));
}

function initInputFields()
{
	$('FORM INPUT.autohide-text, FORM TEXTAREA.autohide-text').each(function(){
		$(this).focus(function(){
			if($(this).val() == $(this).attr('title')){
				$(this).val('');
				$(this).addClass('autohide-off');
			}
		});	
		$(this).blur(function(){
			if($(this).val() == ''){
				$(this).val($(this).attr('title'));
				$(this).removeClass('autohide-off');
			}
		})
		if($(this).val() == ''){
			$(this).val($(this).attr('title'));
			$(this).removeClass('autohide-off');
		}
	});
}

var slideshowTimer = null;
function doSlideshow(objid, start, time){
	if(!$('#' + objid) || !isVisible($('#' + objid))){
		if(slideshowTimer != null){
			clearTimeout(slideshowTimer);
			slideshowTimer = null;
		}
		return;
	}
	
	if(start){
		clearTimeout(slideshowTimer);
		slideshowTimer = null;
	}
	
	var currObj = $('#' + objid + ' li.current');
	var nextObj = currObj.next();
	if($('#' + objid + ' li:last').hasClass('current')){ 
		nextObj = $('#' + objid + ' li:first');
	}

	nextObj.addClass('current');
	nextObj.css('opacity', 0);
	nextObj.css('display', 'block');
	currObj.fadeOut('normal', function(){ $(this).removeClass('current'); });
	nextObj.animate({opacity:1}, 'fast');
	
	slideshowTimer = setTimeout('doSlideshow("' + objid + '", false, ' + time + ');', time);
}

function isVisible(obj)
{
	return (obj.css('display') == 'block');
}

function initCompaniesTooltip()
{
	$('.company-row').mousemove(function(e){
		if(!isVisible($('#popup-info'))){
			$('#popup-info').children('.features').children('SPAN').html($(this).children('.additional-info').children('.features').html());		
			$('#popup-info').children('.bonus-features').children('SPAN').html($(this).children('.additional-info').children('.bonus-features').html());
			$('#popup-info').show();
		}
		$('#popup-info').css('left', e.pageX + 20);
		$('#popup-info').css('top', e.pageY + 20);
	});
	$('.company-row').mouseleave(function(){
		$('#popup-info').hide();
	});
}

function initReviewForm()
{
	$('#addreview-link').click(function(){
		if($(this).hasClass('button')){
			$(this).removeClass('button');
			$('#commentform').slideDown('normal');
			var formcontrols_str = '#commentform input,#commentform button, #commentform textarea,#commentform label';
			$('#commentform').submit(function(){
				var formData = $(this).serialize();
				
				$('#comment-errormsg').hide();
				$(formcontrols_str).attr('disabled', 'disabled');
				$(formcontrols_str).animate({opacity:0.5}, 'normal');
				$(this).children('.processing-request').show();
				
				$.ajax({
					type: 'POST', url: $(this).attr('action'), data: formData,
					complete: function(response){
						if(response.responseText.substr(0, 7) == 'Error: '){
							$(formcontrols_str).removeAttr('disabled');
							$(formcontrols_str).animate({opacity:1}, 'normal');
							$('#commentform').children('.processing-request').hide();
							$('#comment-errormsg').html(response.responseText);
							$('#comment-errormsg').show('normal');
						} else {
							$(formcontrols_str).hide();
							$('#commentform').children('.processing-request').addClass('done');
							$('#commentform').children('.processing-request').html('<strong>Thank you for submittion your review!</strong><br/><br>Your review has been successfully submitted. It will appear at our website after it passes moderation.');
						}
					}
				});
				return false;
			});
		}	
	});
}
