/**
 * @author u1005stulz
 */
/* AJAX function */

jQuery(document).ready( function(){
	

	jQuery('#commentsForm').validate();
	jQuery('#invoice').validate();
	// comments form
	jQuery('#tx_comments_pi1_submit').click(function() {
		alert(Recaptcha.get_challenge());
		if (jQuery("#commentsForm").validate().form() && jQuery('#tx_comments_pi1_human').val()==4) setComment();
		return false;
	});
});

var delimiterCustom = '&';
jQuery(document).ready(function() {

	// disabling the right mouse-click
	function click (e) {
	  if (!e)
	    e = window.event;
	  if ((e.type && e.type == "contextmenu") || (e.button && e.button == 2) || (e.which && e.which == 3)) {
	    if (window.opera)
	      window.alert("Sorry: Diese Funktion ist deaktiviert.");
	    return false;
	  }
	}
	if (document.layers)
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown = click;
	document.oncontextmenu = click;

	// Initialize history plugin.
	jQuery.history.init(function(url) {
        
    	if (url != '') {
			jQuery("#menu a").removeClass("active");
			jQuery("a[href$='" + url + "']").addClass("active");
		}
        getContent(url == "" ? "" : url);
    });
	//Links per Ajax nachladen
	jQuery("#menu a, .ajax").live('click', function(e) {
		if(jQuery(this).attr('href')=='nc/shop/') return true; 
    url = jQuery(this).attr("href");
		url = url.replace(/^.*#/, '');
		delimiterCustom = '?';
		jQuery.history.load(url);
		
		// handling css styling 
		jQuery('#menu a.active').removeClass('active');
		jQuery(this).addClass('active');
		
		return false;
    });
	
	jQuery('#blogSearchButton').live('click', function(e) {
		blogSearchFire();
		return false;
	});
	// news-links
	jQuery(".news-list-item a, .news-catmenu a").live('click', function(e) {
		url = jQuery(this).attr("href");
		url = url.replace(/^.*#/, '');
		delimiterCustom = '&';
		jQuery.history.load(url);
		//getContentLeft(url == "" ? "" : url);
		return false;
	});
	// news-back link
	jQuery(".backlink, .internal-link").live('click', function(e) {
		url = jQuery(this).attr("href");
		url = url.replace(/^.*#/, '');
		delimiterCustom = '?';
		jQuery.history.load(url);
		return false;
	});
	//Informieren das etwas passiert
	jQuery("#content, .big").ajaxStart(function() {
		jQuery('#loader').fadeIn(300);
	});
	jQuery("#content, .big").ajaxStop(function() {
		jQuery('#loader').fadeOut(300);
	});
	jQuery("#toggler").live('click', function(e) {
		jQuery('#comment_form').slideToggle(300);
	});
	jQuery("#toggler").live('mouseover', function(e) {
		jQuery(this).css('cursor','pointer');
	});
	
	jQuery('#tx_comments_pi1_submit').live('click', function(e) {
		;
		if (jQuery("#commentsForm").validate().form() && checkCaptcha(Recaptcha.get_response(),Recaptcha.get_challenge())=='success') setComment();
		return false;
	});
	jQuery('.tx-comments-comments h2').live('click', function(e) {
		if (jQuery("#commentsForm").validate().form() && jQuery('#tx_comments_pi1_human').val()==4) setComment();
		return false;
	});
	
});

// holt die catcha-geschichte

function checkCaptcha(response, challenge) {
	
	var url = 'fileadmin/scripts/ajax.recaptcha.php';
	var html = jQuery.ajax({
   type: "POST",
   url: url,
	 data: "recaptcha_challenge_field=" + challenge + "&recaptcha_response_field=" + response,
	 async: false
 }).responseText;
 return html;
}
// Holt den Inhalt vom Server
function getContent(url) {
	if (url=="") return;
	jQuery.ajax({
		url: url+'/ajax.html',
		success: function(html) {
			jQuery('#content')
			//.removeClass("loading")
			.css("display", "none")
			//.fadeOut(300)
			.html(html)
			.fadeIn(200);
			//Adresszeile aktualisieren
			url = url.replace(/^.*#/, '');
			location.hash = url;
			// make fancybox reinit
		    setTimeout(
		        function() {
		            jQuery(".jqfancybox").fancybox({
					"padding": 0,
					"speedIn": 300,
					"speedOut": 300,
					"changeSpeed": 300,
					"transitionIn": "elastic",
					"transitionOut": "elastic",
					"titlePosition": "over",
					"titleShow": true,
					"easingIn": "swing",
					"easingOut": "swing",
					"showCloseButton": false,
					"showNavArrows": true,
					"enableEscapeButton": true,
					"overlayShow": true,
					"overlayOpacity": 0.4,
					"overlayColor": "#666",
					"centerOnScroll": false,
					"hideOnContentClick": false,
					"onComplete"		 :	function() {
												jQuery("#fancybox-wrap").hover(function() {
													jQuery("#fancybox-title").show();
												}, function() {
													jQuery("#fancybox-title").hide();
												});
										 	},  "titleFormat": function(title, currentArray, currentIndex, currentOpts) { return '<span id="fancybox-title-over">Picture ' + (currentIndex + 1) + ' of ' + currentArray.length + (title.length ? ' : &nbsp; ' + title : '') + '</span>';}
				});
				jQuery("#commentsForm").validate();
				Recaptcha.create("6LeMvLsSAAAAAMk2oTIbPn3tUCLEXsr83Z4lM_2h",
			   "recaptcha_div", 
			   {  
			     theme: "clean",
			     callback: Recaptcha.focus_response_field 
			   }
			 );
			 
				jQuery('#tx_comments_pi1_submit').mouseup(function() {
					jQuery("#commentsForm").attr("action", jQuery('#formAction').val());
					if (jQuery("#commentsForm").validate().form()==false && jQuery('#tx_comments_pi1_human').val()!=4) return false;
				});
		    },
		    600
		    );
		}
	});
}
// Schreibt die Comments
function setComment() {
	//itemUrl = jQuery('#itemUrl').val()+'?tx_comments_pi1[firstname]='+jQuery('#tx_comments_pi1_firstname').val()+'&tx_comments_pi1[email]='+jQuery('#tx_comments_pi1_email').val()+'&tx_comments_pi1[content]='+jQuery('#tx_comments_pi1_content').val()+'&tx_comments_pi1[submit]=Submit';
	itemUrl = jQuery('#itemUrl').val()+'?tx_comments_pi1[firstname]='+jQuery('#tx_comments_pi1_firstname').val()+'&tx_comments_pi1[email]='+jQuery('#tx_comments_pi1_email').val()+'&tx_comments_pi1[content]='+jQuery('#tx_comments_pi1_content').val()+'&tx_comments_pi1[submit]=Submit';
	//itemUrl += data;
	jQuery.ajax({
		//type: 'POST',
		url: itemUrl,
		//data: data,
		success: function(html) {
			jQuery('#content')
			//.removeClass("loading")
			.css("display", "none")
			//.fadeOut(300)
			.html(html)
			.fadeIn(200);
			setTimeout(
		        function() {
		            jQuery("#commentsForm").validate();
								Recaptcha.create("6LeMvLsSAAAAAMk2oTIbPn3tUCLEXsr83Z4lM_2h",
							   "recaptcha_div", 
							   {  
							     theme: "clean",
							     callback: Recaptcha.focus_response_field 
							   }
							 );
		    	},
		    	600
		    );
		}
	});
}
// Schreibt die Comments
function blogSearchFire() {
	data='tx_ttnews[swords]='+jQuery('#blogSearchInput').val();
	jQuery.ajax({
		type: 'POST',
		url: 'nc/blog/result/ajax.html',
		data: data,
		success: function(html) {
			jQuery('.big')
			//.removeClass("loading")
			.css("display", "none")
			//.fadeOut(300)
			.html(html)
			.fadeIn(200);
			//Adresszeile aktualisieren
			url = url.replace(/^.*#/, '');
			location.hash = url;
		}
	});
}
/* end AJAX */

