/*
	@created: 8.10.2009 16:42
	@edit: 9.10.2009 9:06
*/

if( typeof jQuery == 'undefined' ){}
else
{
	$(document).ready(function(){
		$('a[rel="popup"]').click(function(){
			uus_popup_aken(this.href);
			return false;
		});

		if( (location.href.split('#') != 'undefined') && ($('#galerii_parem_kylg').length >0) ){
			var loc_href = location.href.split('#')[1];

			$('#galerii_parem_kylg li a#' + loc_href).click(function(){
				if( $(this).parent().parent().hasClass('album') ){}
				else
				{
					var opic = $(this).attr('href');
					var big_thumb = $(this).find('img').attr('src');
					var pic_name = $(this).attr('title');
					var vid_bthumb = $(this).find('span');

					if( big_thumb )
					{
						if( vid_bthumb.length == 1 )
						{
							big_thumb = $(vid_bthumb).text();
						}
						else
						{
							big_thumb = big_thumb.replace(/110110_/, '');
						}

						$('#pildiraami_sisu img').attr('src', big_thumb);
						$('#pildiraam').attr({ href: opic, title: pic_name });
					}

					return false;
				}
			}).trigger('click');
		}

		// tagasiside vorm
		if( $('#tagasiside').length > 0 ){
			// esmalt peidame kõik valikud, mis peavad tulema hiljem
			$('.tagasiside_valik_1_opt, #tagasiside_valik_2, .tagasiside_valik_2_opt').hide();

			$('#tagasiside_valik_1').ripploendi_valikud();
			$('#tagasiside_valik_2').ripploendi_valikud(0);
		}
	});

	$(window).load(function(){
		/* prettyphoto, lightbox */
		if( $('a[rel^=prettyphoto], a[rel^=lightbox]').length > 0 )
		{
			$('<link>').appendTo('head').attr({
				rel: 'stylesheet', 
				type: 'text/css', 
				href: kodulehe_aadress  + 'kujundused/kukeraadsik/assets/css/prettyPhoto.css'
			});

			$.getScript(kodulehe_aadress + 'js/jquery.prettyPhoto.js', function(){
				$("a[rel^=prettyPhoto], a[rel^=lightbox]").prettyPhoto({
					animationSpeed: 'normal', /* fast/slow/normal */
					opacity: 0.35, /* Value between 0 and 1 */
					showTitle: true, /* true/false */
					allowresize: true, /* true/false */
					default_width: 500,
					default_height: 344,
					counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
					theme: 'facebook', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
					hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
					wmode: 'opaque', /* Set the flash wmode attribute */
					autoplay: false, /* Automatically start videos: True/False */
					modal: false, /* If set to true, only the close button will close the window */
					changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
					callback: function(){} /* Called when prettyPhoto is closed */
				});

			$('#galerii_parem_kylg li a').click(function(){
				if( $(this).parent().parent().hasClass('album') ){}
				else
				{
					var opic = $(this).attr('href');
					var big_thumb = $(this).find('img').attr('src');
					var pic_name = $(this).attr('title');
					var vid_bthumb = $(this).find('span');

					if( big_thumb )
					{
						if( vid_bthumb.length == 1 )
						{
							big_thumb = $(vid_bthumb).text();
						}
						else
						{
							big_thumb = big_thumb.replace(/110110_/, '');
						}

						$('#pildiraami_sisu img').attr('src', big_thumb);
						$('#pildiraam').attr({ href: opic, title: pic_name });
					}

					return false;
				}
			});


			$('#galerii_nav_prev').click(function(){
				// võtame hetkel pildiraami vahel oleva pildilt lingi
				var a = $('#pildiraam').attr('href');
				if( a )
				{
					var c = $('#galerii_parem_kylg ul li a');
					$.each(c, function(i){
						// võrdluse tarbeks võtame leitud linkidelt lingid
						var k = $(c[i]).attr('href');

						// võrdleme leitud väärtust pildiraamil oleva pildi lingiga
						if( k == a ){
							// otsime järgmise <li> elemendi
							var n = $(c[i]).parent().parent().prev();
							if( n ){
								// kui oli album siis ei sööda järgmise pildina albumit ette, kui oli pilt siis võtame pildiinfo
								if( $(n).hasClass('album') ){}
								else
								{
									// otsime leitud elemendile lingi
									var n_img = $(n).find('a');
									var n_img_href = $(n_img).attr('href');
									var big_thumb = $(n_img).find('img').attr('src');
									var pic_name = $(n_img).attr('title');

									if( big_thumb ){
										big_thumb = big_thumb.replace(/110110_/, '');

										$('#pildiraami_sisu img').attr('src', big_thumb);
										$('#pildiraam').attr({ href: n_img_href, title: pic_name });
									}
								}
							}
						}
					});
				}

				return false;
			});

			$('#galerii_nav_next').click(function(){
				// võtame hetkel pildiraami vahel oleva pildilt lingi
				var a = $('#pildiraam').attr('href');
				if( a )
				{
					var c = $('#galerii_parem_kylg ul li a');
					$.each(c, function(i){
						// võrdluse tarbeks võtame leitud linkidelt lingid
						var k = $(c[i]).attr('href');

						// võrdleme leitud väärtust pildiraamil oleva pildi lingiga
						if( k == a ){
							// otsime järgmise <li> elemendi
							var n = $(c[i]).parent().parent().next();
							if( n ){
								// kui oli album siis ei sööda järgmise pildina albumit ette, kui oli pilt siis võtame pildiinfo
								if( $(n).hasClass('album') ){}
								else
								{
									// otsime leitud elemendile lingi
									var n_img = $(n).find('a');
									var n_img_href = $(n_img).attr('href');
									var big_thumb = $(n_img).find('img').attr('src');
									var pic_name = $(n_img).attr('title');
									if( big_thumb ){
										big_thumb = big_thumb.replace(/110110_/, '');

										$('#pildiraami_sisu img').attr('src', big_thumb);
										$('#pildiraam').attr({ href: n_img_href, title: pic_name });
									}
								}
							}
						}
					});
				}

				return false;
			});

			});

		}

	});

	jQuery.fn.extend({
		/* @added: 21.09.2010 15:53 */
		isEmptyObj: function(obj){
			for(var prop in obj){
				if(obj.hasOwnProperty(prop))
					return false;
			}
			return true;
		},
		ripploendi_valikud: function(valik){
			if( typeof valik == 'undefined' ){
				var valik = false;
			}

			if( $(this).length > 0 ){
				var t = this, tid = $(this).attr('id').split(/\D/).reverse()[0];

				$('#tagasiside_valik_' + tid + ' select').change(function(){
					var selIdx = this.selectedIndex, selectVal = this.options[selIdx];

					// kui oli valitud jah
					if( selIdx == (!valik ? 1 : valik) ){
						// avame peidetud valikud
						$('.tagasiside_valik_' + tid + '_opt').show();
					}
					else
					{
						$('.tagasiside_valik_' + tid + '_opt').hide();

						if( tid == 1 && selIdx == 0 ){
							$('#tagasiside_valik_2 select')
								.find('option')
									.removeAttr('selected')
										.andSelf()
											.find('option:first')
												.attr('selected','selected');

							$('.tagasiside_valik_2_opt').hide();
							$('#tagasiside_valik_2_opt').hide();
						}
					}
				});
			}
		}
	});


	/*asukoha_kaart(aadress);*/
	function asukoha_kaart(x, y, m_data, icon_image, layer_id)
	{
		$.getScript(kodulehe_aadress + 'js/markerclusterer.js', function(){
			var center = new google.maps.LatLng(x, y);
			var options = {
				zoom: 15,
				center : center,
				mapTypeControl: true,
				mapTypeControlOptions: {
					style: google.maps.MapTypeControlStyle.DROPDOWN_MENU, 
					position: google.maps.ControlPosition.TOP_RIGHT
				},
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};

			var map = new google.maps.Map(document.getElementById(layer_id), options);
			var markers = [];
			//var infoWindow = new google.maps.InfoWindow({});

			if( icon_image == 'none' || !icon_image ){}
			else
			{
				var image = new google.maps.MarkerImage(icon_image, 
				  // This marker is 20 pixels wide by 32 pixels tall. 
				  new google.maps.Size(32, 32), 
				  // The origin for this image is 0,0. 
				  new google.maps.Point(0,0), 
				  // The anchor for this image is the base of the flagpole at 0,32. 
				  new google.maps.Point(0, 32));
			}

			for(var i=0;i<m_data.length;i++)
			{
				var infoWindow = new google.maps.InfoWindow({});

				var bodytxt = ( m_data[i].info == '' ? m_data[i].title : m_data[i].info.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g, '"') );
				var latLng = new google.maps.LatLng(m_data[i].latitude, m_data[i].longitude);

				if( icon_image == 'none' || !icon_image )
				{
					var marker = new google.maps.Marker({
						position:latLng,
						title:m_data[i].title,
						draggable:false
					});
				}
				else
				{
					var marker = new google.maps.Marker({
						position:latLng,
						title:m_data[i].title,
						icon:image,
						draggable:false
					});
				}

				infoWindowHtml = '<div class="gmap_sisu" style="height:' + (bodytxt.length+10) + 'px;">' + bodytxt + '<\/div>';
				infoWindow.setContent(infoWindowHtml);
				infoWindow.open(map, marker);

				markers.push(marker);
			}

			var mc = new MarkerClusterer(map, markers);
		});
	}
}


/*
	Abiunktsioonid
	==============
*/
/*
	uus popup aken
	--------------
*/
function uus_popup_aken(link, laius, korgus)
{
	if(!laius) var laius = 800; // 770
	if(!korgus) var korgus = 600; // 650

	var left = parseInt((screen.availWidth/2) - (laius/2));
	var top = parseInt((screen.availHeight/2) - (korgus/2));

	window.open(link, '_blank', 
		'scrollbars=yes,toolbar=no,location=no,status=no,width=' + laius + ',height=' + korgus 
		+ ',screenX=' + left + ',screenY=' + top);
};


