	/*
	*	Simple slideshow
	*/
	
	function Slide(container, options) {
		var 
			defaults = {
				start		: 0,
				speed		: "normal",
				cyclic		: false,
				interval	: 0,
				onChange	: function() {}
			},
			tabs	= container.children(),
			current = 0,
			ticker,
			opts;

		opts = $.extend(defaults, options);

		function slideNext() {
			if (current < tabs.length - 1) {
				current++;
				
				show();
			} else if (opts.cyclic) {
				current = 0;
				
				show();
			}
		}

		function slidePrev() {
			if (current > 0) {
				current--;
				
				show();
			} else if (opts.cyclic) {
				current = tabs.length -1;
				
				show();
			}
		}
		
		function slideTo( nr ) {
			if (current !== nr) {
				current = nr;
				show();
			}
		}
		
		function show() {
			opts.onChange( current );
			
			tabs.stop(true, true);

			tabs.filter(':visible').fadeOut( opts.speed );

			tabs.eq( current ).fadeIn( opts.speed );
		}
		
		function init() {
			current = opts.start;

			if (current > tabs.length) {
				current = tabs.length;
			}
			
			if (current < 0) {
				current = Math.floor(Math.random() * tabs.length);
			}

			tabs.hide().eq( current ).show();

			if (opts.interval > 0) {
				ticker = setInterval(slideNext, opts.interval);

				container.parent().hover(function() {
					clearInterval(ticker);
				}, function() {
					ticker = setInterval(slideNext, opts.interval);
				});
			}
		}

		init();
		
		return {
			next	: slideNext,
			prev	: slidePrev,
			pos		: slideTo
		}
	};
	
	function initializeMap() {
		var myLatlng = new google.maps.LatLng(56.975157, 24.137576);
		var myLatlng2 = new google.maps.LatLng(56.973157, 24.137576);
		var myOptions = {
			zoom: 14,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		
		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		
		var contentString = '<div id="map_info">'+
	        '<h2>SIA"Princess Boutique"</h2>' +
			'<p>Hospitāļu iela 33,<br /> Rīga, LV 1013</p>'+
	        '</div>';
	        
	    var infowindow = new google.maps.InfoWindow({
	        content: contentString
	    });
	 
	    var marker = new google.maps.Marker({
	        position: myLatlng2,
	        map: map,
	        title: 'SIA "ML Princeses'
	    });
	    
		google.maps.event.addListener(marker, 'click', function() {
			if ($("#map_info").is(':visible')) {
				infowindow.close(map,marker);
			} else {
				infowindow.open(map,marker);
			}
		});
		
		infowindow.open(map,marker);
	}

	function loadMapScript() {
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initializeMap";
		document.body.appendChild(script);
	}
	
	function DraugiemSay(title, url, titlePrefix ) {
		window.open(
		'http://www.draugiem.lv/say/ext/add.php?title=' + encodeURIComponent( title ) + 
		'&link=' + encodeURIComponent( url ) + 
		( titlePrefix ? '&titlePrefix=' + encodeURIComponent( titlePrefix ) : '' ), 
		'', 
		'location=1,status=1,scrollbars=0,resizable=0,width=530,height=400' 
		); 
		return false; 
	}

	$(document).ready(function() {
		/*
		*	Langbar switcher
		*/
/*
		$('#langbar').click(function(e) {
			var ul = $(this);
			
			if (typeof e.target.tagName !== 'undefined' && e.target.tagName.toLowerCase() == 'a' && !$(e.target).hasClass('active')) {
				ul.stop();
				return;	
			}

			if (ul.is(':animated')) {
				return;	
			}

			if (ul.data('opened')) {
				ul.animate({'top' : '-70px'}, 'fast', function() {
					ul.data('opened', false);
				});

			} else {
				ul.animate({'top' : 0}, 'fast', function() {
					ul.data('opened', true);
	
					$(document).bind('click.menu', function() {
						$(document).unbind('click.menu');
						ul.trigger('click');
					});	
				});
			}		
		});
*/

		/*
		*	Box
		*/
		
		$("div.box").wrapInner('<div class="box_middle"></div>').prepend('<div class="box_top"></div>').append('<div class="box_bottom"></div>');
		$("div.box_s").wrapInner('<div class="box_middle_s"></div>').prepend('<div class="box_top_s"></div>').append('<div class="box_bottom_s"></div>');
		
		
		/*
		*	Init slider
		*/
		
		var slider_items = $(".slider_items");
		
		if (slider_items.length) {
			var slider_nav = $('<ul class="slider_nav"></ul>').insertAfter( slider_items );

			$.each(slider_items.children(), function(i, v) {
				slider_nav.append('<li><a ' + ( i == 0 ? 'class="active"' : '') + ' href="javascript:;" rel="' + i + '">' + (i + 1) + '</a></li>');
			});

			var slide = new Slide(slider_items , {
				speed		: "slow",
				cyclic		: true,
				interval	: 2500,
				start		: 0,
				onChange	: function( current ) {
					$('a', slider_nav).removeClass('active').eq( current ).addClass('active');
				}
			});

			$('a', slider_nav).click(function() {
				slide.pos( parseInt( $(this).attr('rel') ) );
			});
		}
		
		
		/*
		*	Left menu
		*/
		
		$("#leftnav").click(function(e) {
			if (e.target.tagName.toLowerCase() !== 'a') {
				return;
			}

			var 
				ul	= $(this),
				a	= $(e.target),
				li	= a.parent(),
				ch	= $( 'div:first', li ),
				hide;
		
			if (ch.length) {
				hide = a.hasClass('active');

				li.parent().find('div').addClass('hidden').end().find('a').removeClass('active');
				
				if (hide) {
					a.removeClass('active');
					ch.addClass('hidden');
				} else {
					a.addClass('active');
					ch.removeClass('hidden');
				}
				
				return false;
			} else {
				if (a.hasClass('level_0')) {
					ul.find('a').removeClass('active');
					a.addClass('active');
				}
			}
		});
		
		/*
		*	Init Google maps
		*/
		
		if ($("#map_canvas").length) {
			loadMapScript();
		}
		
		/*
		* Init draugiem
		*/
		
		 $(".draugiem_say").click(function() {
		 		DraugiemSay($(this).attr("title"), $(this).attr("href"), location.hostname );
				return false;
		 });
	});
	
