function GetWindowSize(w)
{
	w = w ? w : window;

	var width	= w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
	var height	= w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);

	return {width: width, height: height};
}

/*
	spinner
*/
Spinner = function(id) {
	this.init(id);
}
$.extend(Spinner.prototype, {
	id: '',

	init: function(id)
	{
		this.id = id;
	},

	Show: function()
	{
		var dims = GetWindowSize();

		if ( dims )
		{
			var properties = {
				//'left':	left + 'px',
				//'top':	top + 'px',
			}

			var eSpinner = $('#' + this.id);
			eSpinner.css(properties);
			eSpinner.show();
		}
	},

	Hide: function()
	{
		var eSpinner = $('#' + this.id);
		eSpinner.hide();
	}
});

Messager = function(id) {
	this.init(id);
}
$.extend(Messager.prototype, {
	id: '',

	init: function(id)
	{
		this.id = id;

		if ( $('#' + id + '_text').html() != '' )
		{
			$('#' + id).show();
		}

		$('#' + id + '_closer').click(function() {
			$('#' + id).hide();
		});
	},

	Show: function(msgType, msgText)
	{
		var id = this.id;

		var eMsg = $('#' + id + '_text');

		eMsg.attr('class', msgType)
		eMsg.html(msgText);

		$('#' + id).show();
	},

	Hide: function()
	{
		var eMsg = $('#' + this.id + '_text');
		eMsg.html('');
		eMsg.attr('class', '')

		$('#' + id).hide();
	}
});

var oSpinner = new Spinner('spinner');

$(document).ready(function() {

	var oMessager = new Messager('messager');

	if ( $('#mycarousel_new').length )
	{
		$(document).ready(function() {
			$('#mycarousel_new').jcarousel();
		});
	}
	if ( $('#mycarousel_featured').length )
	{
		$(document).ready(function() {
			$('#mycarousel_featured').jcarousel();
		});
	}

	if ( !$('#no_rating_onload').length )
	{
		$('form .star-rating').rating({
			required: true,
			callback: function(value, link) {
				if (this.form.id == 'frm_submit_rate')
				{
					$.ajax({
						type: "POST",
						url: "/service/submit-rate/",
						data: $(this.form).serialize() + '&lang_id=' + $("#fld_lang_id").val(),
						dataType: "xml",
						complete: function(XMLHttpRequest, textStatus) {
							oSpinner.Hide();
						},
						success: function(xml) {
							$('.star-rating').rating('readOnly',true);
						}
					});
				}
			}
		});
	}

	// logout form
	$('.lnk_logout').click(function() {
		$('#frm_logout').submit();
		return false;
	});

	// review minus submit
	$('.minus_b').click(function() {

		var review_id = $(this).attr('id').replace('review_minus_', '');
		
		$.ajax({
			type: "POST",
			url: "/service/submit-review-vote/",
			data: 'review_id=' + review_id + '&vote=0&lang_id=' + $("#fld_lang_id").val(),
			dataType: 'json',
			complete: function(XMLHttpRequest, textStatus) {
				oSpinner.Hide();
			},
			success: function(response) {
				var votes = response.votes > 0 ? '+' + response.votes : response.votes;
				$('#review_' + response.review_id).html(votes);
			}
		});

		return false;
	});

	// review plus submit
	$('.plus_b').click(function() {
		
		var review_id = $(this).attr('id').replace('review_plus_', '');

		$.ajax({
			type: "POST",
			url: "/service/submit-review-vote/",
			data: 'review_id=' + review_id + '&vote=1&lang_id=' + $("#fld_lang_id").val(),
			dataType: 'json',
			complete: function(XMLHttpRequest, textStatus) {
				oSpinner.Hide();
			},
			success: function(response) {
				$('#review_' + response.review_id).html(response.votes);
			}
		});

		return false;
	});
	
	//captcha
	$('#lnk_captcha_reload').click(function() {
		var d = new Date();
		$('#_captcha_').attr('src', '/captcha.php?t=' + d.getTime());
		return false;
	});

	// countries & states

	if ( $('#fld_b_country_id').length )
	{
		if ( $('#fld_b_country_id').get(0).tagName == 'SELECT' )
		{
			load_country_options('b');

			$('#fld_b_country_id').change(function(){
				load_state_options('b');
			})
		}
		else if ( $('#fld_b_state_id').get(0).tagName == 'SELECT' )
		{
			load_state_options('b');
		}
	}

	if ( $('#fld_s_country_id').length )
	{
		if ( $('#fld_s_country_id').get(0).tagName == 'SELECT' )
		{
			load_country_options('s');

			$('#fld_s_country_id').change(function(){
				load_state_options('s');
			})
		}
		else if ( $('#fld_s_state_id').get(0).tagName == 'SELECT' )
		{
			load_state_options('s');
		}
	}

	$('input.quant').keyup(function () {
		var value = $(this).val().replace(/[^\d]/, '');
		$(this).val(value);
	});

	$('.cont_show_review_form').click(function () {
		$('.cont_review_form').show();
	});

});

function load_country_options(prefix)
{
	var eCountry = $('#fld_' + prefix + '_country_id').get(0);

	if ( eCountry && eCountry.tagName == 'SELECT' )
	{
		var label = eCountry.options[0].text;

		eCountry.options.length = 0;
		eCountry.options[0] = new Option('---', '');

		var enabled = $('#fld_load_all_countries').val() == '1' ? 0 : 1;

		oSpinner.Show();

		$.ajax({
			type: "POST",
			url: "/service/get-country-options/",
			data: 'enabled='+enabled+'&lang_id='+$("#fld_lang_id").val(),
			dataType: "xml",
			complete: function(XMLHttpRequest, textStatus) {
				oSpinner.Hide();
			},
			success: function(xml) {

				root = xml.documentElement;

				eCountry.options[0] = new Option(label, '');

				var options = root.getElementsByTagName('option');

				if ( options && options.length )
				{
					for ( var i = 0; i < options.length; i++ )
					{
						opt_value = options[i].getAttribute('id');
						opt_title = options[i].getAttribute('value');

						index = eCountry.options.length;

						eCountry.options[index] = new Option(opt_title, opt_value);

						if ( opt_value == $('#fld_current_' + prefix + '_country_id').val() )
						{
							eCountry.options[index].selected = true;
						}
					}
				}
				load_state_options(prefix);
			}
		});
	}
}

function load_state_options(prefix)
{
	var country_id = $('#fld_' + prefix + '_country_id').val();

	if ( country_id )
	{
		var eState = $('#fld_' + prefix + '_state_id').get(0);

		if ( eState && eState.tagName == 'SELECT' )
		{
			var label = eState.options[0].text;

			eState.options.length = 0;
			eState.options[0] = new Option('---', '');

			var enabled = $('#fld_load_all_states').val() == '1' ? 0 : 1;

			oSpinner.Show();

			$.ajax({
				type: "POST",
				url: "/service/get-country-state-options/",
				data: 'country_id='+country_id+'&enabled='+enabled+'&lang_id='+$("#fld_lang_id").val(),
				dataType: "xml",
				complete: function(XMLHttpRequest, textStatus) {
					oSpinner.Hide();
				},
				success: function(xml) {

					root = xml.documentElement;

					eState.options[0] = new Option(label, '');

					var options = root.getElementsByTagName('option');

					if ( options && options.length )
					{
						for ( var i = 0; i < options.length; i++ )
						{
							opt_value = options[i].getAttribute('id');
							opt_title = options[i].getAttribute('value');

							index = eState.options.length;

							eState.options[index] = new Option(opt_title, opt_value);

							if ( opt_value == $('#fld_current_' + prefix + '_state_id').val() )
							{
								eState.options[index].selected = true;
							}
						}
					}
				}
			});
		}
	}
}


