function ready() {
	/* text inputs with default values */
	$.each(
		$('.default-text-value'),
		function() {
			if(this.type == 'password') {
				try {
					this.setAttribute('type', 'text');
					this.setAttribute('ispassword', 'true');
				} catch(e) {}
			}
			
			this.setAttribute('defaultvalue', this.value);
			
			this.onfocus = function() {
				if(this.value == this.getAttribute('defaultvalue')) {
					this.value = '';
				}
				
				if(this.getAttribute('ispassword') == 'true') {
					try {
						this.setAttribute('type', 'password');
					}
					catch(e) {}
				}
			}
			
			this.onblur = function() {
				if(this.value == '') {
					this.value = this.getAttribute('defaultvalue');
					
					if(this.type == 'password') {
						try {
							this.setAttribute('type', 'text');
						}
						catch(e) {}
					}
				}
			}
		}
	);
	
	/* news ticker */
	var NewsTicker = {
		total_items: $('#news-ticker h4').length,
		
		shown_item: 1,
		
		init: function() {
			if(this.total_items > 1) {
				setInterval(this.change, 5000);
			}
		},
		
		change: function() {
			if(NewsTicker.shown_item < NewsTicker.total_items) {
				$('#news-ticker div').css('top', -NewsTicker.shown_item * $('#news-ticker h4')[0].offsetHeight);
				NewsTicker.shown_item ++;
			}
			else {
				$('#news-ticker div').css('top', 0);
				NewsTicker.shown_item = 1;
			}
			$('#news-ticker div').css('display', 'none');
			$('#news-ticker div').fadeIn();
		}
	}
	
	NewsTicker.init();
	
	/* news slider */
	function Slider(div) {		
		var _this = this;
		var duration = 5000;
		
		this.init = function() {
			this.div = div;
			this.total_items = $('.slideshow div', this.div).length;
			this.slide = 1;
			$('.controls a', this.div).click(
				function() {
					clearTimeout(_this.timeout);
					_this.goTo(parseInt(this.rel.replace(/slide-/, '')));
					_this.timeout = setTimeout(_this.next, duration);
					return false;	
				}
			);
			this.timeout = setTimeout(_this.next, duration);
		}
		
		this.goTo = function(slide) {
			var left = 0;
			for(var i = 0; i < slide - 1; i++) {
				left -= $('.slideshow div', _this.div)[i].offsetWidth;
			}
			$('.slideshow', _this.div).animate({left: left}, {queue: false});
			$('.controls a', _this.div).attr('class', '');
			$('.controls a', _this.div)[slide - 1].className = 'selected';
			_this.slide = slide;
		}
		
		this.next = function() {
			if(_this.slide < _this.total_items) {
				_this.goTo(_this.slide + 1);
			}
			else {
				_this.goTo(1);	
			}
			_this.timeout = setTimeout(_this.next, duration);
		}
		
		this.init();
	}
	
	if($('#main-article').length > 0) {
		new Slider($('#main-article'));
	}
	
	if($('#featured-products').length > 0) {
		new Slider($('#featured-products'));	
	}
	
	/* left categories */
	if($('#left-sidebar').length > 0) {
		$('#left-sidebar nav div').attr('def-height', function() {return this.offsetHeight});
		$('#left-sidebar nav div').not('.selected').css('height', 0);
		
		$('#left-sidebar nav a.nav-category').mouseover(
			function() {
				$('#left-sidebar nav div').each(
					function() {
						$(this).animate({height: 0}, {queue: false});
					}
				);
				$(this).next().animate({height: $(this).next().attr('def-height')}, {queue: false});
			}
		);
		
		$('#left-sidebar nav').mouseover(
			function() {
				$('#categories-tooltip').show();	
			}
		);
		
		$('#left-sidebar nav').mouseleave(
			function() {
				$('#categories-tooltip').fadeOut();
				$('#left-sidebar nav div').animate({height: 0}, {queue: false});
				$('#left-sidebar nav div.selected').animate({height: $('#left-sidebar nav div.selected').attr('def-height')}, {queue: false});
			}
		);
		
		$('#left-sidebar nav a').not('.button').mouseover(
			function() {
				eval("var data = " + this.rel);
				$('#categories-tooltip fieldset').hide();
				if(this.className.indexOf('nav-category') == -1) {
					$('#categories-tooltip fieldset span.title').html($(this.parentNode).prev().html() + ' / ' + $(this).html());
				}
				else {
					$('#categories-tooltip fieldset span.title').html($(this).html());
				}
				if(data.src != '') {
					$('#categories-tooltip fieldset img').show();
					$('#categories-tooltip fieldset img').attr('src', data.src);
				}
				else {
					$('#categories-tooltip fieldset img').hide();
				}
				$('#categories-tooltip fieldset strong').html(data.title);
				$('#categories-tooltip fieldset p').html(data.text);
				$('#categories-tooltip fieldset span.price span').html(data.price);
				$('#categories-tooltip fieldset a.button').attr('href', data.href);
				$('#categories-tooltip fieldset').fadeIn();
			}
		);
	}
	
	/* show overlay */
	function showOverlay(url) {
		if(!document.getElementById('overlay')) {
			var overlay = document.createElement('div');
			overlay.id = 'overlay';
			document.body.appendChild(overlay);
		}
		$('#overlay').fadeIn();
		$('#overlay').html('<div class="overlay-content"><a href="#" onclick="$(\'#overlay\').fadeOut(); return false;" class="close"></a><iframe frameborder="0" allowtransparency="true" src="' + url + '"></iframe></div>');
		$('#overlay').css('height', document.body.offsetHeight);
	}
	
	$('.show-overlay').click(
		function() {
			showOverlay(this.href);
			top.location.href = '#';
			return false;
		}
	);
	
	/* check location */
	$('span#shipment-price a').click(
		function() {
			$('form#shipment-location').slideDown();
			return false;
		}
	);
	
	/* expandable list */
	$('ul.expandable').each(
		function() {
			$('div.list-content', this).hide();
			
			$('li span.list-title', this).click(
				function() {
					if($('div.list-content', this.parentNode).css('display') == 'none') {
						$('span.list-title span', this.parentNode).html('-');
						$('div.list-content', this.parentNode).slideDown();
					}
					else {
						$('span.list-title span', this.parentNode).html('+');
						$('div.list-content', this.parentNode).slideUp();	
					}
				}
			);
		}
	);
	
	/* my orders flag */
	$('img.order_flag').click(
		function() {
			if(this.src.indexOf('inactive') != -1) {
				this.src = this.src.replace(/inactive/, 'active');
			}
			else {
				this.src = this.src.replace(/active/, 'inactive');	
			}
			return false;
		}
	);
	
	$('#full-text a.print').click(
		function() {
			window.print();
			return false;
		}
	);
	
	$('div.select select').change(
		function() {
			this.parentNode.getElementsByTagName('span')[0].innerHTML = this.options[this.selectedIndex].innerHTML;
		}
	);
	
	/* switch homepage products */
	/*$('#homepage-products a').click(
		function() {
			$('#main-product div.main-product').hide();
			currClass = $(this).attr('class');
			$('#' + currClass).show();
			return false;
		}
	);*/
	
	/* store locator */
	function showStores(id) {
		$('.store-list').hide();

		$.post(site_url+"/ajax/store_locator_store",
				{sid: id},
				function(data) {
					$('#stores-city').html(data);
				});
		$('.store-list').fadeIn();
	}

	/* store locator */
	function showStoresService(id) {
		$('.store-list').hide();

		$.post(site_url+"/ajax/store_locator_store_service",
				{sid: id},
				function(data) {
					$('#stores-city').html(data);
				});
		$('.store-list').fadeIn();
	}
	
	$('ul#map_block li a.shops').mouseover(
		function() {
			$('ul#map_block li a').removeClass('selected');
			$(this).addClass('selected');
			$('#map-stores').css('top', $(this).offset().top - 10);
			$('#map-stores').css('left', $(this).offset().left - 80);
			$('#map-stores').hide();
			
			$.post(site_url+"/ajax/store_locator_cities",
					{county: $(this).attr('id')},
					function(data) {
						$('#map-stores').html(data);
						$('#map-stores').fadeIn();	
						$('#map-stores a').click(
								function() {
									showStores($(this).attr('rel'));
								}
							);
					});
			return false;
		}
	);

	$('ul#map_block li a.service').mouseover(
		function() {
			$('ul#map_block li a').removeClass('selected');
			$(this).addClass('selected');
			$('#map-stores').css('top', $(this).offset().top - 10);
			$('#map-stores').css('left', $(this).offset().left - 80);
			$('#map-stores').hide();
			
			$.post(site_url+"/ajax/store_locator_cities_service",
					{county: $(this).attr('id')},
					function(data) {
						$('#map-stores').html(data);
						$('#map-stores').fadeIn();
						$('#map-stores a').click(
								function() {
									showStoresService($(this).attr('rel'));
								}
							);
					});
			return false;
		}
	);
	
	/* product tabs */
	$('section#product .tabs a').not('.not-tab').click(
		function() {
			$('.product-specs').hide();
			$('section#product .tabs a').removeClass('selected');
			currClass = $(this).attr('class');
			$(this).addClass('selected');
			$('.' + currClass + '-tab').fadeIn();
			return false;
		}
	);
	
	/* about us tabs */
	$('.despre-noi-tab').click(function(){
		$('.despre-noi-tab').removeClass('selected');

		currClass = $(this).attr('class');
		currClass = currClass.split(' ');

		$(this).addClass('selected');
		$('.despre-noi-content').hide();
		$('#'+currClass[0]).fadeIn();
		return false;
	});
	
	function faq_toggle(number) {
		$('.faq-content').hide();
		$('#faq-'+number).show();
	}
	
	function showCity(city_name) {
		$('.store-list').hide();
	
		$.post(site_url+"/ajax/store_locator_store_name",
				{sname: city_name},
				function(data) {
					$('#stores-city').html(data);
					$(document).scrollTop($('#stores-city').position().top)
				});
		$('.store-list').fadeIn();
	}
	
	$('.ui-menu-item .ui-corner-all').live('click', function(){
		city = $(this).html();
		showCity(city);
	});
	
	/* IE6 is outdated */
	if($.browser.msie == true && parseInt($.browser.version) <= 6) {
		var div = document.createElement('div');
		div.id = 'upgrade-ie6';
		div.innerHTML = 'You are using an outdated version of Internet Explorer. To view this site please <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home">upgrade your browser</a>!';
		$(div).insertBefore('header');
	}
}

function playNextPresentation() {
	var i;
	$('.main-product').each(
		function() {
			if($(this).css('display') == 'block') {
				i = parseInt(this.id.replace(/main\-product\-/, ''));
			}
		}
	);
	
	$('#main-product div.main-product').hide();
	if(i < 3) {
		$('#main-product-' + (i + 1)).show();
	}
	else {
		$('#main-product-1').show();
	}
}

function showMessage(text, type) {
	if(!document.getElementById('messages')) {
		var message = document.createElement('div');
		message.id = 'messages';
		message.className = 'message';
		document.body.appendChild(message);
	}
	else {
		$('#messages').show();
	}
	
	if(type == 'success') {
		$('#messages').attr('class', 'message success');	
	}
	else {
		$('#messages').attr('class', 'message error');	
	}
	$('#messages').html('');
	$('#messages').html(text);
	setTimeout(
		function() {
			$('#messages').fadeOut();
		},
		5000
	);
}

ready();

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-12813630-1']);
  _gaq.push(['_setDomainName', '.evolio.ro']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

