var old='';
$(document).ready(function(){
	var is_ie='no';
	jQuery.each(jQuery.browser, function(i, val) {
		if(i=="msie" && val==true){
			is_ie='yes';
		}
	});
	if(is_ie=='yes'){
		$('#closepopup').css('margin-left','645px');
	}
	var pIdentifier;
	$('#newmap').animate({opacity:0.0},1);
	$('.pinpoint').live('click',function(){
		var pType=$(this).attr('pintype');
		old=$('#printlink').attr('href');
		pIdentifier=$(this).attr('identifier');
		$('#printlink').attr('href','http://kaart.beebifoto.ee/print.php?id='+pIdentifier);
		if(pType=='map'){
			$.ajax({
				type: "POST",
				url: "engine.php?act=getmap",
				data: ({identifier : pIdentifier}),
				success: function(msg){
					if(msg!=''){
						result=msg.split("[:SPLITTER:]");
						var pinpoints=result[1];
						var mapbackground=result[0];
						$('#newmap').css('display','');
						$('#pinpoints').css('display','none');
						$('#pinpoints').html(pinpoints);
						$('#newmap').attr('src',mapbackground);
						setTimeout("$('#newmap').animate({opacity:1.0,marginLeft:'0px',marginTop:'0px',height:'460px',width:'720px'},500);",150);
						setTimeout("$('#pinpoints').fadeIn(150);",650);
						setTimeout("$('#map').attr('style','background:url(\""+mapbackground+"\") top center no-repeat;');$('#newmap').css('display','none');$('#newmap').attr('src','');",850);
						setTimeout("$('#newmap').animate({opacity:0.0,marginLeft:'180px',marginTop:'115px',height:'230px',width:'360px'},10);",850);
					}
				}
			});
		} else {
			var content=$('#info_'+pIdentifier).html();
			var comments=$('#comments_'+pIdentifier).html();
			$('#popupcontent').html(content);
			$('#existingcomments').html(comments);
			$('#popup').fadeIn(300);
			setTimeout("$('#popupcontent').fadeIn(200);",300);
		}
	});
	$('.backbutton').live('click',function(){
		pIdentifier=$(this).attr('identifier');
		$('#printlink').attr('href','http://kaart.beebifoto.ee/print.php?id='+pIdentifier);
		$.ajax({
			type: "POST",
			url: "engine.php?act=getmap",
			data: ({identifier : pIdentifier}),
			success: function(msg){
				if(msg!=''){
					result=msg.split("[:SPLITTER:]");
					var pinpoints=result[1];
					var mapbackground=result[0];
					$('#pinpoints').css('display','none');
					$('#pinpoints').html(pinpoints);
					$('#map').attr('style','background:url("'+mapbackground+'") top center no-repeat;');
					$('#pinpoints').fadeIn(150);
				}
			}
		});
	});
	$('#closepopup').click(function(){
		$('#printlink').attr('href',old);
		$('#popup').fadeOut(150);
		setTimeout("$('#popupcontent').html('');",150);
		$('#myname').val('Name');
		$('#mycomment').val('');
	});
	$('#closepicture').click(function(){
		$('#largepicture').fadeOut(150);
		$('#largepicture').css('background-image','none');
	});
	$('[openlarge]').live('click',function(){
		$('#largepicture').fadeIn(150);
		$('#largepicture').css('background','url('+($(this).attr('openlarge'))+') center center no-repeat #FFFFFF');
	});
	$('#submitcomment').click(function(){
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var cName = $('#myname').val();
		var cEmail = $('#myemail').val();
		var cComment = $('#mycomment').val();
		if(cName == ''){
			alert('Palun sisestage nimi!');
		} else if(cEmail == '') {
			alert('Palun sisestage e-mail!');
		} else if(cComment ==''){
			alert('Palun sisestage kommentaar!');
		} else if(reg.test(cEmail) == false) {
			alert('Palun sisestage korrektne e-mail!');
		} else {
			$.ajax({
				type: "POST",
				url: "engine.php?act=postcomment",
				data: ({name : cName, email : cEmail, comment : cComment, identifier : pIdentifier}),
				success: function(msg){
					$('#comments_'+pIdentifier).html(msg);
					$('#existingcomments').fadeOut(10);
					$('#existingcomments').html(msg);
					$('#existingcomments').fadeIn(150);
				}
			});
		}
	});
});
