﻿/*
	(c)2011, Michael A. Vesely, ART-ist - CreativeCommunication
*/

var currentPage;
var imageOffsets;
var map;

function scrollImagesToChapter(target) {
	var targetImages = $(target + '_images');
	var offset = 0;
	if (targetImages.length != 0) {
		offset = targetImages.position().left; //* -1;
	}
	$('#images').stop().animate(
			{ scrollLeft: offset },
			{ duration: 1000, easing: 'easeInOutQuart' }
		);
}

function scrollImagesRight() {
  var offset = 0;
  var images = $("#images");
  $("#images img").each(function (index) {
    if (($(this).position().left + $(this).width()) >= (images.scrollLeft() + images.width()+10)) {
      offset = $(this).position().left;
      return false; //breack out of the each function
    }
  });
  $('#images').stop().animate(
		{ scrollLeft: offset },
		{ duration: 1000, easing: 'easeInOutQuart' }
	);
	return false;
}

function scrollImagesLeft() {
  var offset = 0;
  var offset1 = 0;
  var images = $("#images");
  $("#images img").each(function (index) {
    if ($(this).position().left >= images.scrollLeft() - images.width()-10) {
      offset = $(this).position().left;
      return false; //breack out of the each function
    }
  });
  $('#images').stop().animate(
		{ scrollLeft: offset },
		{ duration: 1000, easing: 'easeInOutQuart' }
	);
  return false;
}

function go2Page(target) {
	if (target == "/") target = "#Home";
	var targetPage = $(target);
	//alert("C:" + currentPage.attr('id') + " T:" + targetPage.attr('id'));

	currentPage.fadeOut('middle',
		function () {
			targetPage.fadeIn('middle',
				function () {
					//scrollImagesToChapter(target);
				}
			); 
		}
	);
	scrollImagesToChapter(target);
	currentPage = targetPage;

}

// pre-submit callback 
function showRequest(formData, jqForm, options) {
  // formData is an array; here we use $.param to convert it to a string to display it 
  // but the form plugin does this for you automatically when it submits the data 
  var queryString = $.param(formData);

  // jqForm is a jQuery object encapsulating the form element.  To access the 
  // DOM element for the form do this: 
  // var formElement = jqForm[0]; 

  alert('About to submit: \n\n' + queryString);

  // here we could return false to prevent the form from being submitted; 
  // returning anything other than false will allow the form submit to continue 
  return true;
}

// post-submit callback 
function showResponse(responseText, statusText, xhr, $form) {
  // for normal html responses, the first argument to the success callback 
  // is the XMLHttpRequest object's responseText property 

  // if the ajaxForm method was passed an Options Object with the dataType 
  // property set to 'xml' then the first argument to the success callback 
  // is the XMLHttpRequest object's responseXML property 

  // if the ajaxForm method was passed an Options Object with the dataType 
  // property set to 'json' then the first argument to the success callback 
  // is the json data object returned by the server 

  alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
        '\n\nThe output div should have already been updated with the responseText.');
} 

//(document).ready
$(document).ready(function () {

  //Set Top Margin
  //$("#container").css({ 'top': ((window.innerHeight / 2)-300) + 'px' })

	//Email-Links aktivieren
	$('.email').each(function () {
		$(this)
			.text($(this).text().replace('[at]', '@').replace('[dot]', '.').replace('email: ', 'mailto:'))
			.attr('href', $(this).attr('href').replace('[at]', '@').replace('[dot]', '.').replace('email: ', 'mailto:')
			);
	});

	//Opens a link in a new window
	$('.openNew')
		.click(function () {
			window.open(this.href); return false;
		});

	//Select current link
	$("#nav a[href$='" + location.hash + "']").addClass('selected');

	//
	currentPage = $(location.hash);
	if ($(location.hash).length <= 0) {
		currentPage = $('#Home');
	}
	else {
		var targetImages = $(location.hash + '_images');
		var offset = 0;
		if (targetImages.length != 0) {
			offset = targetImages.position().left;
		}
		$('#ctl00_imageFinder').scrollLeft(offset);
	}
	$('.page, #Home').hide();
	currentPage.show();

	//Navigate
	$("#nav a, #impressum a, #termine a, #Teaser a")
		.click(function () {

			$("#nav a, #impressum a, #termine a").removeClass('selected');

			var target = $(this).attr('href');
			$('a[href$="' + target + '"]').addClass('selected');
			go2Page(target);
			$(this).blur(); //removes keyboardfocus from link

			return false;
		});

	//Scroll
//  $("#images").scrollview({
//    grab: "images/openhand.cur",
//    grabbing: "images/closedhand.cur"
//  });

	$('#imageFrame')
  .mouseenter(function () {
    $("#imageScrollLeft, #imageScrollRight").fadeIn('middle');
  })
  .mouseleave(function () {
    $("#imageScrollLeft, #imageScrollRight").fadeOut('middle');
  })

  $('#images')
	.mousedown(function (event) {
		$(this)
			.data('down', true)
			.data('x', event.clientX)
			.data('scrollLeft', this.scrollLeft)
			.css({'cursor': 'hand'})
			;
		return false;
	})
	.mouseup(function (event) {
		$(this)
			.data('down', false)
			.css({ 'cursor': 'e-resize' })
			;
	})
	.mousemove(function (event) {
		if ($(this).data('down') == true) {
			this.scrollLeft = $(this).data('scrollLeft') + $(this).data('x') - event.clientX;
		}
	})
	.css({
		'overflow': 'hidden'/*,
		'cursor': 'pointer'*/
  })
	.mousewheel(function (event, delta) {
		this.scrollLeft -= (delta * 50);
  })
  ;

//  //Show map
//  map = new VEMap('mapViewer');
//  map.LoadMap();

//  // bind form using 'ajaxForm' 
//  var options = {
//    target: '#ajaxResponse',   // target element(s) to be updated with server response 
//    beforeSubmit: showRequest,  // pre-submit callback 
//    success: showResponse  // post-submit callback 

//    // other available options: 
//    //url:       url         // override for form's 'action' attribute 
//    //type:      type        // 'get' or 'post', override for form's 'method' attribute 
//    //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
//    //clearForm: true        // clear all form fields after successful submit 
//    //resetForm: true        // reset the form after successful submit 

//    // $.ajax options can be used here too, for example: 
//    //timeout:   3000 
//  };
//  // bind to the form's submit event 
//  $('#aspnetForm').submit(function () {
//    $(this).ajaxSubmit(options);
//    return false;
//  }); 

	//activate CMS
	if (document.getElementById("ctl00_md").value == "cms") {
		insertEditBars(".updateable");
	}

	$(".box").fancybox({
		'width': '90%',
		'height': '90%',
		'autoScale': false,
		'transitionIn': 'none',
		'transitionOut': 'none',
		'type': 'iframe'
	});

	$(".dialog").fancybox({
//		'width': '30',
//		'height': '30',
//		'autoScale': false,
		'transitionIn': 'none',
		'transitionOut': 'none',
	  'type': 'iframe'		
});

}); //$(document).ready

//reset scroll
$(window).mouseout(function (event) {
	if ($('#images').data('down')) {
		try {
				$('#images')
					.data('down', false)
					.css({'cursor': 'pointer'})
					;
		} catch (e) { }
	}
	});

//function GetNewsLetter() {
//	if (document.getElementById("nlEmail").value == "") {
//		alert("Geben Sie mindestens Ihre Emailadresse an, um sich zum Newsletter anzumelden.");
//		return false;
//	} 
////	var data =
////		"Anrede=" + document.getElementById("nlGeschlecht").value + "&" +
////		"Vorname=" + document.getElementById("nlVorname").value + "&" +
////		"Nachname=" + document.getElementById("nlNachname").value + "&" +
////		"Email=" + document.getElementById("nlEmail").value + "&" +
////		"pID=" + document.getElementById("ctl00_id").value
////		;  
////		$.ajax({
////		type: "POST",
////		url: "/WebService.asmx/GetNewsletter",
////		data: data,
////		//contentType: "application/json; charset=utf-8",
////		//dataType: "json",
////		success: function (data, status) {
////			alert(data.d); 
////		},
////		error: function (request, status, error) {
////			alert(request.statusText + "\n" + error); 
////		}
////	});

//	var md = document.getElementById("ctl00_md").value;
//  document.getElementById("ctl00_md").value = "nl";
//  $.fancybox.close();
//  $('#aspnetForm').ajaxSubmit();
//  document.getElementById("ctl00_md").value = md;
//	return false;
//}
