var docWidth, docHeight, newAddressCounter = 0;

$(document).ready(function() {
	
	// initialize GUI
	GUI.init();
});

var GUI = {
		
	init : function () {
		for (i in this) {
			if ( typeof( this[i] ) == 'object') {
				if ( typeof( this[i].init == 'function' ) ) {
					this[i].init();
				}
			}
		}
		
		$('#category_id').live('change', function () {
			if (this.value == '0') {
				$(this).fadeOut();
				$('#category_suggest').fadeIn();
			}
		});
		
		$('.popup').live('click', function (e) {
			e.preventDefault();
			
			var newwindow = window.open(this.href,'name','height=600,width=400,scrollbars=yes');
			if (window.focus) {newwindow.focus();}
			return false;
		});
		
		$('#miestas').live('change', function () {
			$.ajax({
				type	: "GET",
				url		: baseUrl+'ajax_category/'+$('#miestas').val(),
				dataType: "html",
				cache	: false,
				success	: function (msg) {
					$('#categories').html(msg);
				}
			});
		});
		
		$('#add_address').focus(function() {
			if (this.value == 'Parduotuvės adresas') {
				$(this).val('');
			}
		});
		
		$('#add_address').blur(function() {
			if (this.value == '') {
				$(this).val('Parduotuvės adresas');
			}
		});
		
		$('#keyword').focus(function() {
			if (this.value == 'Ieškoma prekė/paslauga') {
				$(this).val('');
			}
		});
		
		$('#keyword').blur(function() {
			if (this.value == '') {
				$(this).val('Ieškoma prekė/paslauga');
			}
		});
		
		if ( !($.browser.msie && $.browser.version == '6.0') ) {
			$('#keyword').autocomplete(baseUrl+'autocomplete', {
				width: 277
			});
		}
		
		$('.help_links .save').jFav();
		
		$('.recommend').live('click', function() {
			$('#recommend_div').modal();
		});
		
		$('.help_links .print').live('click', function(e) {
			e.preventDefault();
			
			window.open(this.href, "Spausdinti", "menubar=no,width=700,height=360,toolbar=no");
		});
		
		$('.confirm').live('click', function (e) {
			return confirm('Ar tikrai?');
		});
		
		$('.ratingOnList a').live('click', function (e) {
			e.preventDefault();
			
			var link	= this;
			var $stars	= $(this).parents('ul.rating');

			$.ajax({
				type	: "GET",
				url		: link.href,
				dataType: "json",
				success	: function (msg) {
					
					var rating = Math.round(msg.rating);
					switch (rating) {
						case 1: $stars.addClass('onestar'); break;
						case 2: $stars.addClass('twostar'); break;
						case 3: $stars.addClass('threestar'); break;
						case 4: $stars.addClass('fourstar'); break;
						case 5: $stars.addClass('fivestar'); break;
					}
				}
			});
		});
		
		$('.rating a').live('click', function (e) {
			e.preventDefault();
			
			var link	= this;
			var $stars	= $(this).parents('ul.rating');

			$.ajax({
				type	: "GET",
				url		: link.href,
				dataType: "json",
				success	: function (msg) {
					
					var rating = Math.round(msg.rating);
					switch (rating) {
						case 1: $stars.addClass('onestar'); break;
						case 2: $stars.addClass('twostar'); break;
						case 3: $stars.addClass('threestar'); break;
						case 4: $stars.addClass('fourstar'); break;
						case 5: $stars.addClass('fivestar'); break;
					}
				}
			});
		});		
		
		$("input[type=text][title], textarea[title]").tooltip({ 
			 
		    // place tooltip on the right edge 
		    position: "center right", 
		 
		    // a little tweaking of the position 
		    offset: [-2, 10], 
		 
		    // use the built-in fadeIn/fadeOut effect 
		    effect: "fade", 
		 
		    // custom opacity setting 
		    opacity: 0.9, 
		 
		    // use this single tooltip element 
		    tip: '.tooltip' 
		 
		});
	},
	
	recommend_form : {
		$form	: null,
		init	: function () {
			this.$form = $('#recommend_form');
			
			this.$form.live('submit', function(e) {
				e.preventDefault();
				GUI.recommend_form.send();
				return false;
			});
		},
	
		send	: function () {
			this.init();
			
			$.ajax({
				type	: "POST",
				url		: this.$form.attr('action'),
				data	: this.$form.serialize(),
				dataType: "json",
				success	: function (msg) {
					if (msg.status == 0) {
						for (i in msg.errors) {
							$('#'+msg.errors[i]).addClass('error');
						}
					} else if (msg.status == 1) {
						$.modal.close();
					} else {
						alert('Nenumatyta klaida');
					}
				}
			});
		}
	},
	
	address_editor : {
		$editor 		: null,
		$addBtn			: null,
		$addressRows	: null,
		$newCitySelect	: null,
		$newAddress		: null,
		init			: function () {
			this.$editor		= $('#addresses_editor');
			this.$addBtn		= $('#add_btn');
			this.$addressRows	= $('#address_rows');
			this.$newCitySelect	= $('#add_city');
			this.$newAddress	= $('#add_address');
			
			$('.remove_address').live('click', function(e) {
				e.preventDefault();
				
				GUI.address_editor.removeAddress(this);
				
				return false;
			});
			
			$('#add_btn').live('click', function(e) {
				
				e.preventDefault();
				
				GUI.address_editor.addAddress();
				
				return false;
			});
		},
		removeAddress	: function (link) {
			var $row = $(link).parents('.address_row');
			
			if ($row.attr('id')) {
				var id = $row.attr('id').replace(/^a_/, '');
				$('dl.zend_form').prepend('<input type="hidden" name="delete_address[]" value="'+id+'" />')
			}
			
			
			
			$row.remove();
		},
		addAddress		: function () {
			
			row = new addressRow();
			row.createNew(this.$newCitySelect, this.$newAddress, newAddressCounter);
			this.$addressRows.append(row.$dom);
			newAddressCounter++;
			
			this.$newAddress.val('Parduotuvės adresas');
			this.$newCitySelect.find('option').each(function () {
				$(this).attr('selected', '');
			});
			console.log('Address');
		}
	}
};

function addressRow (editor) {
	this.$dom = null;
}

addressRow.prototype.createNew = function ($citySelect, $address, i) {
	
	//if ($address.val() == '' || $address.val('Parduotuvės adresas')) return false; 
	
	var $newCitySelect	= $citySelect.clone()
						.attr('name', 'edit_address['+i+'][city_id]')
						.attr('id', '');
	var $newAddress		= $address.clone()
						.attr('name', 'edit_address['+i+'][address]')
						.attr('id', '');
	
	var cityId			= $citySelect.val();
	$newCitySelect.find('option').each(function () {
		if (this.value == cityId) $(this).attr('selected', 'selected');
	});
	
	this.$dom = $('<div class="address_row"><div class="address_city_dropdown"></div><div class="address_input"></div></div>');
	this.$dom.find('.address_city_dropdown').append($newCitySelect);
	this.$dom.find('.address_input').append($newAddress);
	this.$dom.find('.address_input').append(' <a href="#" class="remove_address"><img alt="Šalinti" src="'+baseUrl+'img/minus.gif" /></a>');
	
};