/**
 * @author Jenny Sjšgren, IXD AS
 */
$(document).ready(function() {
	
	var lb = new Lightbox('M');
	var lb_small = new Lightbox('S');
	var ratingObj;
	
	// Init rating module
	$('.rating').each(function() {
		if( $(this).attr('id') == 'add_rating' ) {
			ratingObj = new Rating($(this), 24, storeValue);
		} else {
			new Rating($(this), 24);
		}
	});
	
	// Activate rating buttons
	$('.add_review').click(function() {
		lb.show(true);
		document.getElementById('image').src = '/eng/tellus/secure/?sid=' + Math.random();
		return false;
	});
	
	$('#add_review').submit(function() {
		// code: storing data to server...
		
		var formfields 	= new Object();
		var lang		= 'eng'; 
		var url			= '';
		$("#add_review input").each(function() {
			// unless it's the first, general item
			if ( this.name == 'siteaccess' )
			{
				lang = this.value;
			}
			else
			{
				formfields[this.name] = this.value;
			}
	    });
		$("#add_review textarea").each(function() {
			// unless it's the first, general item
			formfields[this.name] = this.value;
	    });
		url = '/' + lang + '/tellus/rate';
		$.ajax({
			  type: 'post', 
			  url: url,
			  data: formfields,
			  success: handleResponse
		});
		function handleResponse(data)
		{
			$("#ratingValueHeading").removeClass("error");
			$("#email").removeClass("error");
			$("#comment_review").removeClass("error");
			$("#name").removeClass("error");
			$("#captcha_header").removeClass("error");
			
			if ( data == 'ok' )
			{
				$('#captcha').val('');
				$('#customer_name').val('');
				$('#customer_email').val('');
				$('#comment').val('');
				$('#ratingValue').val('');
				ratingObj.reset();
				lb.hide(false);
				lb_small.show();
				
			}
			else
			{
				$("#rating_error").css("display", "block");
				if ( data == 'error' )
				{
					$("#captcha_header").addClass("error");
				}
				else
				{
					var error_list=data.split(';');
					for (i=0; i<error_list.length; i++)
                    {
						if ( error_list[i] == 'ratingValue' )
						{
							$("#ratingValueHeading").addClass("error");
						}
						else
						{
							$("#" + error_list[i]).addClass("error");
						}
                    }
				}
				document.getElementById('image').src = '/eng/tellus/secure/?sid=' + Math.random();
				$('#captcha').val('');
			}
		}
		return false;
		
		// when complete, show "Thank you"-lightbox & clear rating value
		
	});
	
	function storeValue(score) {
		$('#add_review #ratingValue').attr('value', score);
	}
	
});

function Rating($list, itemWidth, callback) {
	this.width = itemWidth || 30;
	this.current = $list.find('.current-rating');
	var that = this;
	
	$list.find('a').click(function() {
		var rating = that.getRatingValue(this);
		if( callback && typeof callback == 'function' ) {
			callback(rating);
		}
		that.setRating(rating);
		return false;
	});
}
Rating.prototype = {
	setRating: function(score) {
		this.current.width(score*this.width);
	},
	getRatingValue: function(target) {
		return target.className.split('-')[1];
	},
	reset: function() {
		this.current.width(0);
	}
};

function Lightbox(size) {
	if( size !== 'S' && size !== 'M' ) return;
	var that = this;
	this.overlay = $('#overlay').css({ opacity: 0, height: $(document).height() });
	this.popup = $('.lightbox.' + size);
	this.size = {
		name: size,
		width: this.popup.width(),
		height: this.popup.height()
	};
	this.popup.find('.close').click(function() {
		that.hide(true);
		return false;
	});
	
	// fix ie6 transparency
	if( $.browser.msie === true && $.browser.version < 7 ) {
		this.popup.filter('.shadow').find('img')
			.css({ filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="design/img/lightbox/popup_bg_'+ this.size.name.toLowerCase() +'.png",sizingMethod="scale")' })
			.attr('src', 'design/img/lightbox/blank.gif');
	}
}

Lightbox.prototype = {
	show: function(showOverlay) {
		if( showOverlay ) {
			this.showOverlay();
		}
		var pos = this.getPosition();
		this.popup.css({ left: pos.left, top: pos.top });
		this.popup.show();
		
	},
	hide: function(hideOverlay) {
		this.popup.hide();
		if( hideOverlay ) {
			this.hideOverlay();
		}
	},
	getPosition: function() {
		var y = $(window).scrollTop() + ($(window).height()/2 - this.size.height/2);
		var x = $(window).width()/2 - this.size.width/2;
		return { top: y, left: x };
	},
	showOverlay: function() {
		this.overlay.stop().show().fadeTo('slow', 0.5);
	},
	hideOverlay: function() {
		this.overlay.fadeTo('slow', 0, function() {
			$(this).hide();
		});
	}
};
