/* ******************************************** */
/*                                              */
/*                    SETTINGS                  */
/*                                              */
/* ******************************************** */

var newKindInitVal;
var add_content_title;
var add_content_url;
var add_content_description;
var add_content_kind;
var add_content_kind_text;
var add_content_comment;
var add_content_new_kind;



function getKeyCode(event) {
   event = event || window.event;
   return event.keyCode;
}

function load_add_content_preview() {

	tmp_kind = '';
	add_content_kind = jQuery('#add_content_kind').val();
	add_content_kind_text = add_content_kind;
	
	if(jQuery('#add_content_url').val().length < 3) {
		jQuery('.add_additional_preview_error').show().html('Please enter a valid URL.');
	} else {
		if(add_content_kind) {

		jQuery('.add_additional_preview_error').hide();

			if(add_content_kind) { tmp_kind = add_content_kind; }
			if(add_content_new_kind) { tmp_kind = add_content_new_kind.substr(0,40); }

			if(add_content_new_kind && add_content_new_kind.match(/mailto:|ftp:|https:|www.|http:/ig) ) {
				jQuery('.add_additional_preview_error').show().html('Please DO NOT enter an URL as<br />new type of site.');
			} else if(add_content_new_kind && !add_content_new_kind.match(/^[a-zA-Z0-9-_ \']+$/ig) || add_content_new_kind && !add_content_new_kind.replace(/ /, "").match(/[a-zA-Z]{1,}/ig) || add_content_new_kind && add_content_new_kind.replace(/ /, "").length < 3) {
				jQuery('.add_additional_preview_error').show().html('Please DO NOT use special<br />characters in new type of site.');
			} else {


				jQuery('.add_additional_preview_indicator').show();
				
				preURL = typeof(AdditionalContentList) != "undefined" ? "../" : "";
				jQuery.getJSON(preURL + "ajax_preview_url?jsoncallback=?",

				{ 	url: encodeURI(jQuery('#add_content_url').val()),
					kind: tmp_kind },
			        function(data){
			          if(data.state == "SUCCESS") {

					  	add_content_title = data.title;
					  	add_content_url = data.url;
					  	add_content_description = data.description;
					  	add_content_new_kind = data.new_kind;

						if(add_content_kind) { jQuery('#add_additional_contant_preview_kind').html(add_content_kind_text); }
						
						if(add_content_kind) {
							jQuery('#add_additional_contant_preview_description').html(add_content_description);
						  	jQuery('#add_additional_contant_preview_title').html(add_content_title);
						  	jQuery('#add_additional_contant_preview_url').html(add_content_url);
						  	jQuery('.add_additional_content_comment').val('').focus();
							jQuery('.add_additional_preview_buttons').hide();
							jQuery('.add_additional_active_buttons').show();
							jQuery('.add_content_enter_information_wrapper').hide();
							jQuery('.add_content_preview').show();
						jQuery('#add_content_thankyou').html('').fadeOut(500);
							jQuery('.add_additional_preview_indicator').fadeOut(500);
						} else {
					  		jQuery('.add_additional_preview_indicator').hide();
							jQuery('.add_additional_preview_error').show().html('No valid Categorie given!');
						}

					  } else if(data.state == "EXISTS"){
					  	jQuery('.add_additional_preview_indicator').hide();
						jQuery('.add_additional_preview_error').show().html('Content already added.');
					} else {
						jQuery('.add_additional_preview_indicator').hide();
						jQuery('.add_additional_preview_error').show().html('No Data found.');
					  }
			        });
			}


		} else {
			jQuery('.add_additional_preview_error').show().html('Please choose or enter<br />the Kind of Information.');
		}

	}


}


jQuery().ready(function() {
	
	

		if(typeof(cp_getarticle_data) != "undefined") {
			jQuery.ajax({
			   type: "GET",
			   dataType: "jsonp",
			   url: cp_getarticle_data.requestURL + '?key=' + cp_getarticle_data.key + "&jsoncallback=?",
			   success: function(response){
			     	jQuery.each(response, function(i, data) {
						jQuery('.get_this_item_ul').prepend(
							jQuery('<li />').append(jQuery('<a />').attr({href: data.url, target: '_blank'}).html(data.title)).append(jQuery('<span />').addClass('grey smaller').html(' (external Link)'))
						);
					});
			   },
			   error: function(){		
			     	//
			   }
			 });	
		}


	if(typeof(cp_addinfo_data) != "undefined") {
		jQuery('.additional-content-loader').show();
	
		addinfoajaxurl = cp_addinfo_data.requestURL;
		
		jQuery.ajax({
		   type: "GET",
		   dataType: "jsonp",
		   url: addinfoajaxurl + "?key=" + cp_addinfo_data.key + "&count=1&jsoncallback=?",
		   success: function(data){
		     	preURL = typeof(AdditionalContentList) != "undefined" ? "../" : "";
				if(data.length > 0) {
					jQuery(data).each(function(i, item) {
						jQuery('<li />').appendTo('.additional_content_ul').append(
							jQuery('<a />').attr({ href: preURL+ "additional-info/" + encodeURIComponent(item.cat)}).html(item.cat)
						).append(" ").append(
							jQuery('<span />').addClass('grey smaller').html("("+item.count+")")
						);
					});
					jQuery('.additional-content-loader').remove();
					jQuery('.additional_content_group').show();
				} else {
					jQuery('.additional-content-loader').fadeOut(500);
					jQuery('.additional_content_group').remove();
				}
		   },
		   error: function(){		
		     	jQuery('.additional-content-loader').hide();
		   }
		 });
	} else {
		jQuery('.additional-content-loader').hide();
	}



	if (typeof(AdditionalContentList) != "undefined" && AdditionalContentList) {

		jQuery.getJSON(addinfoajaxurl + "?key=" + addinfo_data.key + "&cat=" + addinfo_data.cat + "&jsoncallback=?", function(data){
			if (data[0].links) {

				jQuery.each(data[0].links, function(i, item){

					if(item.comment.length < 1)  { comment = item.description; } else { comment = item.comment; }

					jQuery('<li />').appendTo('#additionalinfo_list').append(jQuery('<h2 />').addClass("title").append(jQuery('<a />').attr({
						href: item.url,
						target: addinfo_data.target
					}).html(item.title))).append(jQuery('<div />').addClass("info").append(jQuery("<span />").html(comment)).append(jQuery("<br />")).append(jQuery("<span />").addClass("green thinner").html(item.url)));

				});

			}
		});

	}


	newKindInitVal = jQuery('#add_content_new_kind').val();
	
	add_background_information_kind_wrapper_width = jQuery('#add_background_information_kind_wrapper').width() + 4;
	add_background_information_kind_wrapper_height = jQuery('#add_background_information_kind_wrapper').height() + 4;
	
	jQuery('#add_background_information_kind_wrapper .list').css({
		width: add_background_information_kind_wrapper_width + 'px'


	});
	

	jQuery('#add_background_information_kind_wrapper .switcher').click(function() {
		jQuery('#add_background_information_kind_wrapper li').removeClass('match').show();
		jQuery(this).toggleClass("switcher_open");		
		jQuery('#add_background_information_kind_wrapper .list').toggle().scrollTo(0,0).height('220px');		
		jQuery('#add_background_information_kind_wrapper li').unbind().click(function () {
			jQuery('#add_background_information_kind_wrapper .list').fadeOut(100);
			jQuery('#add_background_information_kind_wrapper .switcher').removeClass("switcher_open");
			jQuery('#add_background_information_kind_wrapper input').val(jQuery(this).html().replace(/(<([^>]+)>)/ig,""));
		});		
		
		jQuery.each(jQuery('#add_background_information_kind_wrapper li'), function(i, item) {				
			jQuery(item).html(jQuery(this).html().replace(/(<([^>]+)>)/ig,""));
		});
	});
		
	jQuery('#add_background_information_kind_wrapper input').keyup(function() {
		value = jQuery(this).val();
		results = [];
		j = 0;
		if(value.length > 2) {	
			jQuery('#add_background_information_kind_wrapper li').addClass('match').show();		
			jQuery.each(jQuery('#add_background_information_kind_wrapper li'), function(i, item) {				
				if(jQuery(this).html().replace(/(<([^>]+)>)/ig,"").toLowerCase().indexOf(value.toLowerCase()) == -1) {
					jQuery(this).removeClass('match').hide();
				} else {					
					text = jQuery(this).text().replace(/(<([^>]+)>)/ig,"");					
					start = text.toLowerCase().indexOf(value.toLowerCase())
					length = value.length;					
					text = text.substr(0, start) + '<b><u>' + text.substr(start, length) + '</b></u>' + text.substr(start+length, text.length);					
					jQuery(this).html(text);
				}
			});
			shownItems = jQuery('#add_background_information_kind_wrapper li.match').length;
			if(shownItems > 0) {									
				jQuery('#add_background_information_kind_wrapper .list').show().scrollTo(0,0).height(shownItems*22-1 + "px");
			} else {
				jQuery('#add_background_information_kind_wrapper .list').fadeOut(100);				
			}
			
			jQuery('#add_background_information_kind_wrapper li').unbind().click(function () {
				jQuery('#add_background_information_kind_wrapper .list').fadeOut(100);
				jQuery('#add_background_information_kind_wrapper .switcher').removeClass("switcher_open");
				jQuery('#add_background_information_kind_wrapper input').val(jQuery(this).html().replace(/(<([^>]+)>)/ig,""));
			});	
		} else {
			jQuery('#add_background_information_kind_wrapper .list').fadeOut(100);
		}
		
		
	});
	
	
	jQuery('body').click(function(e) {
		if(!jQuery(e.target).hasClass('switcher')) {
			jQuery('#add_background_information_kind_wrapper .list').fadeOut(100);
			jQuery('#add_background_information_kind_wrapper .switcher').removeClass("switcher_open");
		}
	});
	
	
	jQuery('#add_content_kind, #add_content_url').keypress(function(e){
		pressedKey = getKeyCode(e);
		if (pressedKey == 13) {
			load_add_content_preview();
		}
	});


	jQuery('#add_additional_preview_btn').click(function() {
		load_add_content_preview();
	});


	jQuery('#add_additional_save_btn').click(function() {

		jQuery('.add_additional_save_indicator').show();

		jQuery('.add_additional_active_buttons').hide();
		jQuery('.add_additional_deactivated_buttons').show();

		add_content_comment = jQuery('.add_additional_content_comment').val();
		preURL = typeof(AdditionalContentList) != "undefined" ? "../" : "";

		jQuery.ajax({
			type: "POST",
			data: { comment : add_content_comment,
					url: preURL + "" + add_content_url,
					title: add_content_title,
					description: add_content_description,
					kind: add_content_kind
			 },
			url: 'ajax_add_content',
			success: function(response){
				jQuery('#add_content_thankyou').html('Your suggestion was successfully sent. Thank you!').fadeIn(500);
				jQuery('.add_additional_preview_buttons, .add_content_enter_information_wrapper').show();
				jQuery('.add_additional_active_buttons, .add_content_preview, .add_additional_deactivated_buttons, .add_additional_save_indicator, .add_additional_preview_indicator').hide();
				jQuery('#add_content_kind, #add_content_url').val('');
			},
			error: function(response){
				jQuery('.add_additional_save_indicator').hide();
				jQuery('.add_additional_preview_error').show().html("An error occurred.<br />We're working on fixing it.<br />Please try again later!");
			}
		});
	});

	jQuery('.add_additional_cancle_btn').click(function() {
				jQuery('#add_content_kind, #add_content_url').val('');				
	});

	jQuery('.add_additional_preview_cancle_btn').click(function() {
			jQuery('.add_content_enter_information_wrapper').show();
			jQuery('.add_content_preview').hide();
			jQuery('.add_additional_preview_buttons').show();
			jQuery('.add_additional_active_buttons').hide();
			jQuery('.add_additional_preview_error').fadeOut(500);
	});
}); 