(function($){
    $.fn.extend({
        clearInputs: function(a) {
            return $(this).each(function() {
                var $t = $(this),
                d = a || $t.val() || $t.text();
                if(!$t.val() || $t.val() == '')$t.val(d);
                $t.focus(function() {
                    var b = $(this);
                    if(b.val() == d)b.val("");
                    b.removeClass("error");
                }).blur(function() {
                    var b = $(this);
                    if (b.val() == d || b.val() == "")b.val(d);
                    b.removeClass("error");
                }).parents('form').submit(function() {
                    if ($t.val() == d) {
                        $t.val('');
                    }
                });
            });
        },
        equalHeights: function() {
            var tallest = jQuery(this).eq(0).addClass('tallest').height();
            return jQuery(this).each(function(i){
                var $t = jQuery(this), h = $t.height();
                if (h > tallest){
                    tallest = h;
                    $t.addClass('tallest').siblings().removeClass('tallest');
                }
            }).css('height', tallest);
        }
    });

    function printPage() {
    	$('.print').click(function() {  
			window.print();
		});
    }

	function slideShow() {
		if($('#slideshow').length > 0) {
			var alt = $('#slideshow img.active').attr("alt");
	        $("#slideshow").append("<span class=\"pngfix\">" + alt + "</span>");

			function slideIt() {
				var $active = $('#slideshow img.active'); // select the active image
			    if ( $active.length == 0 ) {
			        $active = $('#slideshow img:last'); // if there is no active image then it sets the last image to the last
			    }

			    var $next =  $active.next('img').length ? $active.next('img')
			    : $('#slideshow img:first');

			    $('#slideshow span').text($next.attr("alt")); //ads the alt text to the span tag
					
			    $active.addClass('last-active'); //ads the last-active class to the last slided img
				
				
			    $next.css({
			        opacity: 0.0
			    })
			    .addClass('active')
			    .animate({
			        opacity: 1.0
			    }, 1000, function() {
			        $active.removeClass('active last-active');
			        $('#slideshow span').text(null);
			    });
			}

			setInterval( function() {
				slideIt();
			}, 5000 );//5000ms = 5sec

		}
	}

	function lightBox() {
		$(".lightbox").prettyPhoto();
	}

	function pContainerShadow() {
		$('#pMain').wrap('<div class="shadow_w925"></div>');
	}

	function prettyForm() {
    	$('form').jqTransform();
	}

	function menuToggle() {
		if ( $("div#leftSidebar ul.active") == 'undefined' ) {
		    $("div#leftSidebar h2:not(:first)").next().hide();
			$("div#leftSidebar h2:first").next("ul").addClass('active');
		}
		else {
			$("div#leftSidebar ul:not(.active)").hide();
		}
	    $("div#leftSidebar h2").click(function() {
	        $(this).removeClass("active");
	        $(this).next("ul").addClass("active");

	        $("div#leftSidebar ul").slideUp("medium");
	        //slides up the given element
	        $(this).next().slideDown("medium");
	        //makes the next element slidedown, so the next .slider
	        return false;
	    });
		
		if ( $("div.sliderContent.active") == 'undefined' ) {
		    $("h2.slider").next().hide();
			$("h2.slider:first").next("div.sliderContent").addClass('active');
		}
		
		$("h2.slider").click(function() {
			$(this).next("div.sliderContent").slideToggle("medium");
		});
	}

	function browserSupport() {
		if ($.browser.msie == true) {
			if ($.browser.version < 7 ) {
				$('div#pContent').append('<div class="update">Az ön böngészője elavult.<br/>Kérjük frissítse böngészője verzióját vagy használjon egy másikat.<br style="display: block; height: 10px; width: 10px;"/><a href="http://www.mozilla-europe.org/hu/firefox/" target="_blank">Firefox</a> / <a href="http://www.google.com/chrome" target="_blank">Chrome</a></div>');
			}
		}
	}

	function contactFormEmailSending() {
		$('form#contactus_form #send').click(function() {
			$('#pContent').append('<span class="loading">Email küldése</span>');
			
			var name = $('input#contactus_name').val();
			var email = $('input#contactus_email').val();
			var address = $('input#contactus_address').val();
			var phone = $('input#contactus_phone').val();
			var subject = $('input#contactus_subject').val();
			var message = $('textarea#contactus_message').val();
			
			var sample = $('input#contactus_sample').attr('checked'); 
			
			$.ajax({
				url: '/magunkrol/kapcsolat/mailprocess.php',
				type: 'POST',
				data: 'name=' + name + '&mailprocess=true&email=' + email + '&address=' + address + '&phone=' + phone + '&subject=' + subject + '&message=' + message + '&sample=' + sample,

				success: function(result) {
					$('.response').remove();
					$('#pContent').append('<div class="response">' + result + '</p>');
					$('.loading').fadeOut(500, function() {
						$(this).remove();
					});
				}
			});			
			return false;
		});
	}

	function googleMaps() {
		var map, lastI, BoundsArray = [], myOptions = {};
		function initMaps() {
				myOptions = {
					zoom: 12,
					center: new google.maps.LatLng(47.498343, 19.040508),
					mapTypeId: google.maps.MapTypeId.ROADMAP,
					mapTypeControl: false,
					scaleControl: false,
					scrollwheel: false
				};
			//loading in the map, based on the myOption setting
			map = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions);			
		}


		function showMarkers() {
			$('#distributorList li').each(function(i, v){
				var $t = $(this),
					lat = $t.find('.lat').text(),
					lng = $t.find('.long').text(),
					title = $t.find('span:eq(0)').text(),
					address = $t.find('span.address').text(),
					$sideBar = $('#gmap_markerslist li'),
					marker;
				
				BoundsArray[i] = {};

				// Create the marker...
				marker = new google.maps.Marker({
					position: new google.maps.LatLng(lat,lng),
					map: map,
					title: title,
					icon: 'http://img.livingstones.hu/gmaps/lsmarker.png' //37x32
				}); //marker() close

				BoundsArray[i].marker = marker;

				// Create the infoWindow...
				BoundsArray[i].infoWindow = new google.maps.InfoWindow({
					content: title + "<br/>" + address
				});

				// Create the listener with a closure...
				BoundsArray[i].listener = makeClosure(i, BoundsArray[i].marker);

				//adding the coordinates to the markers list
				$sideBar.eq(i).click(function() {
					map.panTo(marker.position);
					google.maps.event.trigger(marker, 'click');
				});

				$t.click(function() {
					map.panTo(marker.position);
					google.maps.event.trigger(marker, 'click');
				});
			}); // iteration close

			// Make a simple closure with the listener...
			function makeClosure(i, marker ) {
				var listener = google.maps.event.addListener(marker, 'click', function() {
					openInfoWindow(i);		// necessary to open the infowindow
				});
				return listener;
			}

			// Open the infoWindow - called from the closure...
			function openInfoWindow(i) {
				if ( typeof(lastI) == 'number' && typeof(BoundsArray[lastI].infoWindow) == 'object' ) {
					BoundsArray[lastI].infoWindow.close();
				}
				lastI = i;
				BoundsArray[i].infoWindow.open(map,BoundsArray[i].marker);
			}
		}

		if ($('#gmap_canvas').length) {
			initMaps();
			showMarkers();	
		}
		
	}

    $(function() {
    	browserSupport();
        printPage();
        slideShow();
        lightBox();
        pContainerShadow();
        prettyForm();
        menuToggle();
        contactFormEmailSending();
        googleMaps();
    });

})(jQuery);
