// JavaScript Document

//gmaps

var map;

var marker;

function gmap_ini(){
	map = new GMap2(document.getElementById("map_canvas"),{ size: new GSize(360,250) });
	var mapcontrol = new GMapTypeControl();
	map.addControl(new GLargeMapControl3D());
	map.setCenter(new GLatLng(47.4984056,19.0407578), 8); //bp

//	map.setUIToDefault();
	GEvent.addListener(map,"click", function(overlay, point) {     
		map.clearOverlays();
		if(point) { 
			$("#maps input:eq(0)").val(point.y);
			$("#maps input:eq(1)").val(point.x);
		}
		marker = new GMarker(new GLatLng(point.y, point.x), {draggable: true});
		GEvent.addListener(marker, "dragend", function(point) {
			$("#maps input:eq(0)").val(point.y);
			$("#maps input:eq(1)").val(point.x);
		});
		map.setCenter(new GLatLng(point.y, point.x), map.getZoom());
		map.addOverlay(marker);
	});
}

function toTop(){
	var dest = $("#headerbg").offset().top-200;
	$("html:not(:animated),body:not(:animated)").animate({ scrollTop: dest}, 1000 );
}

function navi(url) {
	document.location=url;
}

function addFav() {
  if(document.all) {
    window.external.AddFavorite(location.href,document.title)
  } else {
    if(window.sidebar) {
      window.sidebar.addPanel(document.title,location.href,'');
    }
  }
}

function PrintCode(){
	src = 'ct='+codeType+'&type='+type+'&size='+size+'&t='+t;
	window.open("/content/print.php?"+src,'print','left=100,top=100,status=0,scrollbars=1,width=300,height=300');
}

function urlencode(str) {
//str = str.replace(/\+/g,'%2B').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
str = str.replace(/\+/g,'%2B').replace(/\*/g, '%2A').replace(/ /g,'+').replace(/@/g, '%40').replace(/\$/g, '%24').replace(/#/g, '%23').replace(/&/g, '%26').replace(/\\r\\n/g,'%0A').replace(/,/g,'%2C').replace(/\?/g,'%3F').replace(/:/g,'%3A').replace(/\(/g,'%28').replace(/\)/g,'%29').replace(/\//g, '%2F');

return encodeURI(str);
}

var type = 'maps';
var src;
var size;
var t;
var codeType = 'qr';

var html;

function kuld(){
	var ref_form = $("#send-to form").serializeArray();
	$("#send-to").html('<img src="/img/load.gif" alt="" />');														  
	$.post('/action/address.php',ref_form,function(d){
		$("#send-to").html(d);														  
	});
}

$(document).ready(function(){
	var maxleft = $("#left a").length;
	$.each($("#left a"), function(i,o){
		if($(o).hasClass('active')){
			if(i>0){

				$("#left a:eq("+(i-1)+")").addClass('ab-b');

			}

			if(i<maxleft-1){

				$("#left a:eq("+(i+1)+")").addClass('ab-t');

			}

		}

	});

$("#send-to-wrap").hide();

$("#code a:eq(3)").click(function(){
	$("#send-to input[name=qr]").val($('#qr-kod img').attr('src'));
});

$("#share a:last").click(function(){
	$("#send-to input[name=qr]").val('');
});
$("a[rel='cbox']").colorbox({inline:true, href:"#send-to", rel:'nofollow',
	onOpen:function(){
//		$("#send-to input[name=qr]").val($('#qr-kod img').attr('src'));
		html = $("#send-to").html();
	}, 
	onCleanup: function(){ 
		$("#send-to").html(html);
	}
});

	$("#maps input:eq(0)").blur(function(){

		var val = $(this).val();

		if(val!==''){

			$.post('/action/address.php',{action:'conv',val:val},function(d){

				if(d.data=='lon'){

					alert('Ez nem szélességi koordináta!');

					$("#maps input:eq(0)").val('');

				}else{

					$("#maps input:eq(0)").val(d.val);

				}

			},'json');

		}

	});

	$("#maps input:eq(1)").blur(function(){

		var val = $(this).val();

		if(val!==''){

			$.post('/action/address.php',{action:'conv',val:val},function(d){

				if(d.data=='lat'){

					alert('Ez nem hosszúsági koordináta!');

					$("#maps input:eq(1)").val('');

				}else{

					$("#maps input:eq(1)").val(d.val);

				}

			},'json');

		}

	});



	$("a[rel^='newtab']").click(function(){
		window.open(this.href);
		return false;
	});



	$('#text textarea').keyup(function(){
		var Max = 250;
		if($(this).val().length > Max){
			$(this).val($(this).val().substr(0, Max));
		}
		$(this).parent().find('small').html('még ' + (Max - $(this).val().length) + ' karakter');
	});

	$('#sms textarea').keyup(function(){
		var Max = 160;
		if($(this).val().length > Max){
			$(this).val($(this).val().substr(0, Max));
		}
		$(this).parent().find('small').html('még ' + (Max - $(this).val().length) + ' karakter');
	});

	$(".form").hide();

	$(".form:eq(0)").show();

	$('#ears a').click(function(){

		$('#ears a').removeClass('active');

		$(this).addClass('active');

		var id = $(this).attr('rel');

		$(".form").hide();

		$('#'+id).show();

		type = id;

	});

	
	$("#maps img").click(function(){

		var addr = $("#maps input:eq(2)").val();

		if(addr!==''){

			map.clearOverlays();

			$.post('/action/address.php',{action:'coord',addr:addr},function(d){

				$("#maps input:eq(0)").val(d.sz);

				$("#maps input:eq(1)").val(d.h);

				map.clearOverlays();

				marker = new GMarker(new GLatLng(d.sz, d.h), {draggable: true});

				GEvent.addListener(marker, "dragend", function(point) {

					$("#maps input:eq(0)").val(point.y);

					$("#maps input:eq(1)").val(point.x);

				});

				map.setCenter(new GLatLng(d.sz, d.h), 12);

				map.addOverlay(marker);

			},'json');

		}else{

			var sz = $("#maps input:eq(0)").val();

			var h = $("#maps input:eq(1)").val();

			if(sz!=='' && h !==''){

				marker = new GMarker(new GLatLng(sz,h), {draggable: true});

				GEvent.addListener(marker, "dragend", function(point) {

					$("#maps input:eq(0)").val(point.y);

					$("#maps input:eq(1)").val(point.x);

				});

				map.setCenter(new GLatLng(sz,h), 12);

				map.addOverlay(marker);

			}

		}

	});

	
	$("#selectType a").click(function(){
		codeType = $(this).attr('rel');
		$("#selectType a").removeClass('active');
		$(this).addClass('active');
		$("#button").trigger('click');
		return false;
	});

	$("#button").click(function(){

		size = $("#size input[@name='size']:checked").val();

		if(type=='text'){

			t = urlencode($('#text textarea').val());

		}

		if(type=='url'){

			t = urlencode($('#url input').val());		

		}

		if(type=='contact'){

			var name = $("#contact input:eq(0)").val();

			var phone = $("#contact input:eq(1)").val();

			var mail = $("#contact input:eq(2)").val();

			t = urlencode('BEGIN:VCARD N:'+name+' TEL:'+phone+' EMAIL:'+mail+' END:VCARD');		

		}

		if(type=='sms'){

			var num = $("#sms input").val();

			var mes = $("#sms textarea").val();

			t = urlencode('SMSTO:'+num+':'+mes); 

		}

		if(type=='email'){

			var mail = $("#email input:eq(0)").val();

			var subj = $("#email input:eq(1)").val();

			var mes = $("#email textarea").val();

			t = urlencode('MATMSG:TO:'+mail+'SUB:'+subj+'BODY:'+mes); 

		}

		if(type=='maps'){

			var sz = $("#maps input:eq(0)").val();

			var h = $("#maps input:eq(1)").val();

			var p = $("#maps input:eq(2)").val();

			if(p==''){

				p = 'My location';

			}

			//t = urlencode('geo:'+sz+','+h+',100');

			t = urlencode('http://maps.google.com/maps?f=q&q='+sz+','+h);

		}

		src = 'http://qr-kod.hu/'+codeType+'-kod.php?type='+type+'&size='+size+'&t='+t;

		$("#qr-kod img").attr('src','/img/load.gif');

		$("#qr-kod img").attr('src',src);

		$("#embed input").val('<img src="'+src+'" alt="qr-kod" />');

	});

});

function createBanner(id,url,src,w,h){
	$(id).html('<a href="'+url+'" rel="newtab"><img width="'+w+'" height="'+h+'" src="'+src+'" alt="" /></a>');
}

//analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-16879143-1']);
_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);
})();

