// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERFACE.JS                                                                                                                                            //
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// @package VIDEIRA 2011
// @author Pedro Gonçalves
// @copyright Copyright (c) Front.end 2011
// @version 1.0
// @access public
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var $j = new jQuery.noConflict();
// ########################################################################################################################################################/
// #### FORMS CABEÇALHO ###################################################################################################################################/
// ########################################################################################################################################################/
$j(document).ready(function() {
	try {
		$j('#fms-username').live('focus', function() {
			$j(this).siblings('label').fadeOut('fast');
		});
		$j('#fms-username').live('blur', function() {
			var val = $j.trim($j(this).val());
			if (val == '') {
				$j(this).siblings('label').fadeIn('fast');
			}
		});
		$j('#fms-password').live('focus', function() {
			$j(this).siblings('label').fadeOut('fast');
		});
		$j('#fms-password').live('blur', function() {
			var val = $j.trim($j(this).val());
			if (val == '') {
				$j(this).siblings('label').fadeIn('fast');
			}
		});
		$j('#form-pesquisa > a').live('click', function() {
			$j('#form-pesquisa').submit();
		});
	} 
	catch (o) {
	}
});
// ########################################################################################################################################################/
// #### SLIDESHOW ##############################################################################################################################################/
// ########################################################################################################################################################/
$j(document).ready(function() {
	try {
		if ($j('#slideshow').length > 0 && $j('#slideshow > li').length > 1) {
			var slideshow = null;
			slideshow = $j('#slideshow').bxSlider({
				mode: 'fade',
				controls: false,
				pager: true,
				auto: true,
				autoDelay: 5000,
				speed: 800,
				pause: 7000
			});
		}
	} 
	catch (o) {
	}
});
// ########################################################################################################################################################/
// #### FORMS #############################################################################################################################################/
// ########################################################################################################################################################/
$j(document).ready(function() {
	try {
		$j('#questoes-form-submit').live('click', function() {
			$j('#questoes-form').submit();
		});
		$j('#questoes-form-clear').live('click', function() {
			$j('#questoes-form .field input').val('');
			$j('#questoes-form .field textarea').val('');
		});
	} 
	catch (o) {
	}
});
// ########################################################################################################################################################/
// #### GALERIA ###########################################################################################################################################/
// ########################################################################################################################################################/
$j(document).ready(function() {
	try {
		if ($j('ul#main-gallery').length > 0 && $j('ul#main-gallery > li').length > 1) {
			var maingallery = null;
			maingallery = $j('ul#main-gallery').bxSlider({
				mode: 'fade',
				controls: false,
				speed: 800
			});
			if ($j('ul#sub-gallery').length > 0 && $j('ul#sub-gallery > li').length > 1) {
				var subgallery = null;
				subgallery = $j('ul#sub-gallery').bxSlider({
					mode: 'horizontal',
					controls: true,
					prevSelector: '.pagerGalery > .prev',
					prevText: $j('.pagerGalery > .prev').attr('rel'),
					nextSelector: '.pagerGalery > .next',
					nextText: $j('.pagerGalery > .next').attr('rel'),
					pager: true,
					pagerSelector: '.pagerGalery > .pages',
					speed: 800
				});
			}
			$j('#sub-gallery a').hover(function() {
				if ($j('#sub-gallery a.active').length > 0) {
					$j('#sub-gallery a.active').stop().animate({
						'opacity': '1'
					}, 200, function() {
						$j('#sub-gallery a').removeClass('active');
					});
				}
				$j(this).stop().animate({
					'opacity': '0.6'
				}, 200);
			}, function() {
				$j(this).stop().animate({
					'opacity': '1'
				}, 200);
			})
			$j('#sub-gallery a').live('click', function() {
				var element = $j(this).attr('rel');
				maingallery.goToSlide(element);
			});
		}
	} 
	catch (o) {
	}
});
// ########################################################################################################################################################/
// #### MAPAS #############################################################################################################################################/
// ########################################################################################################################################################/
$j(document).ready(function() {
	try {
		$j('#generate-itinerary').live('focus', function() {
			var val = $j.trim($j(this).val());
			var rel = $j.trim($j(this).attr('rel'));
			if (val == rel) {
				$j(this).val('');
			}
		});
		$j('#generate-itinerary').live('blur', function() {
			var val = $j.trim($j(this).val());
			var rel = $j.trim($j(this).attr('rel'));
			if (val == '') {
				$j(this).val(rel);
			}
		});
		if ($j('#map_canvas').length > 0) {
			var map = null;
			var directionDisplay;
			var directionsService = null;
			directionsService = new google.maps.DirectionsService();
			directionsDisplay = new google.maps.DirectionsRenderer();
			$j('#directions_panel').html('');
			$j('#map_canvas').html('');
			var lat = 41.170251;
			var lng = -8.439308;
			var zoom = 10;
			var titulo = 'Videira, Lda.';
			var myLatlng = new google.maps.LatLng(lat, lng);
			var myOptions = {
				zoom: parseFloat(zoom),
				center: myLatlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				panControl: true,
				zoomControl: true,
				zoomControlOptions: {
					style: google.maps.ZoomControlStyle.LARGE
				},
				streetViewControl: false
			}
			map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
			directionsDisplay.setMap(map);
			directionsDisplay.setPanel(document.getElementById("directions_panel"));
			var infowindow = new google.maps.InfoWindow({
				content: titulo
			});
			var contentString = '';
			contentString += '<div class="mapsContent">';
			contentString += '<img src="' + endereco_site + '/templates/images/mapsLogo.png" alt="Videira, Lda" width="200" height="26" />';
			contentString += '<p>Zona Industrial de Terronhas<br />Apartado 3 | 4589-904 Recarei<br />Paredes | Portugal</p>';
			contentString += '<p>Tel. +351 224 119 030<br />Fax +351 224 119 039<br />Email: info@videira.pt</p>';
			contentString += '</div>';
			var infowindow = new google.maps.InfoWindow({
				content: contentString,
				maxWidth: 220
			});
			var marker = new google.maps.Marker({
				position: myLatlng,
				map: map,
				title: titulo,
				icon: endereco_site + '/templates/images/marker.png'
			});
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(map, marker);
			});
			$j('#get_directions').live('click', function() {
				var end = new google.maps.LatLng(lat, lng);
				var start = $j('#generate-itinerary').val();
				var request = {
					origin: start,
					destination: end,
					travelMode: google.maps.DirectionsTravelMode.DRIVING
				};
				directionsService.route(request, function(response, status) {
					if (status == google.maps.DirectionsStatus.OK) {
						directionsDisplay.setDirections(response);
					}
					else {
						alert('Não foi possível calcular o trajeto.');
					}
				});
			});
		}
	} 
	catch (o) {
	}
});
// ########################################################################################################################################################/
// #### MAPA PT ############################################################################################################################################/
// ########################################################################################################################################################/
$j(document).ready(function() {
	try {
		if ($j('#mapa-content').length > 0 && $j('#mapa-index').length > 0) {
			$j('#mapa-index a').each(function() {
				var cords = new String($j(this).attr('rel')).split(';');
				if (cords.length == 2) {
					/*
					 var style = 'style="top: ' + cords['0'] + 'px; left: ' + cords['1'] + 'px;"';
					 var location = '';
					 location += '<a href="' + $j(this).attr('href') + '">';
					 location += $j(this).html();
					 location += '</a>';
					 */
					$j(this).clone().appendTo('#mapa-content');
					var width = '';
					if ($j.browser.msie && $j.browser.version < 8) {
						width = ' style="width: ' + $j(this).outerWidth() + 'px"';
					}
					$j('#mapa-content > a:last-child').removeAttr('rel').css({
						'left': cords['0'] + 'px',
						'top': cords['1'] + 'px'
					}).append('<span' + width + '>&nbsp;</span>').addClass();
				}
			});
			$j('#mapa-content > a').hover(function() {
				var css = $j(this).attr('class');
				$j('#mapa-index a.' + css).parent().addClass('active');
			}, function() {
				var css = $j(this).attr('class');
				$j('#mapa-index a.' + css).parent().removeClass('active');
			});
			$j('#mapa-index a').hover(function() {
				var css = $j(this).attr('class');
				$j('#mapa-content a.' + css).addClass('active');
			}, function() {
				var css = $j(this).attr('class');
				$j('#mapa-content a.' + css).removeClass('active');
			});
		}
	} 
	catch (o) {
	}
});
// ########################################################################################################################################################/
// #### PRODUTOS ##########################################################################################################################################/
// ########################################################################################################################################################/
$j(document).ready(function() {
	try {
		$j('.produtosShowcase a').hover(function() {
			$j(this).children('span').stop().animate({
				'top': '0px'
			}, 300, 'easeOutExpo');
		}, function() {
			$j(this).children('span').stop().animate({
				'top': '230px'
			}, 300, 'easeInExpo');
		});
		$j('.filtros fieldset label').live('click', function() {
			$j(this).parent('fieldset').children('label').removeClass('checked');
			$j(this).addClass('checked');
		});
		if ($j('.filtros').length > 0 && $j.browser.msie && $j.browser.version < 9) {
			$j('.filtros fieldset label').append('<span></span>');
		}
		if ($j('#pruduto-gallery').length > 0 && $j('#pruduto-gallery > li').length > 1) {
			var pruduto_gallery = null;
			pruduto_gallery = $j('#pruduto-gallery').bxSlider({
				mode: 'fade',
				controls: false,
				pager: true,
				auto: true,
				speed: 800,
				pause: 5000
			});
		}
	} 
	catch (o) {
	}
});
// ########################################################################################################################################################/
// #### Scroll Top ############################################################################################################################################/
// ########################################################################################################################################################/
$j(document).ready(function() {
	try {
		$j('#scroll-top').click(function() {
			$j('html, body').animate({
				scrollTop: 0
			}, 1000, 'easeOutExpo');
		});
	} 
	catch (o) {
	}
});
// ########################################################################################################################################################/
// #### RODAPÉ ############################################################################################################################################/
// ########################################################################################################################################################/
$j(document).ready(function() {
	try {
		checkFooter = checkFooter = function() {
			if (($j(window).height() - 50) > $j('body').height()) {
				$j('.footer').addClass('fill');
			}
			else {
				$j('.footer').removeClass('fill');
			}
		}
		$j(window).resize(function() {
			checkFooter()
		});
		checkFooter();
		$j('a[rel="tool"]').tooltip({
			tipClass: 'tooltipContainer',
			position: 'top center',
			offset: [8, 0]
		});
		$j('.imprimir').live('click', function() {
			print();
		});
		$j('#ftr-favoritos').live('click', function() {
			var currenturl = window.location;
			var currenttitle = document.title;
			try {
				if (window.sidebar) {
					window.sidebar.addPanel(currenttitle, currenturl, "");
				}
				else 
					if (window.external) {
						window.external.AddFavorite(currenturl, currenttitle);
					}
					else 
						if (window.opera && window.print) {
							alert(mensagemFavoritosErro());
						}
						else {
							alert(mensagemFavoritosErro());
						}
			} 
			catch (o) {
				alert(mensagemFavoritosErro());
			}
		});
		var mensagemFavoritosErro = function() {
			var string = '';
			if (lang == 'pt') {
				string = 'Funcionalidade não suportada pelo seu browser.\nPressione Ctrl + D para adicionar esta página aos seus favoritos.';
			}
			else {
				if (lang == 'fr') {
					string = 'Fonctionnalité non supporté par votre navigateur.\nAppuyez sur Ctrl + D pour ajouter cette page à vos favoris.';
				}
				else {
					string = 'Functionality not supported by your browser.\nPress Ctrl + D to add this page to your favorites.';
				}
			}
			
			return string;
		}
	} 
	catch (o) {
	}
});

