var choosenTags = new Array();
var choosenTagsEdit = new Array();

$.fn.isVisible = function () {
	return this.css('visibility') !== 'hidden' && this.css('display') !== 'none';
};

function switchVisibility(id,speed){
	if($('#'+id).isVisible()) $('#'+id).hide(speed);
	else $('#'+id).show(speed);	
}

function switchClassVisibility(class,speed){
	if($('.'+class).isVisible()) $('.'+class).hide(speed);
	else $('.'+class).show(speed);	
}

function showLoadingScreen(){
	$('#sendButton').attr('disabled','disabled');
	$('#loadingBox').show();
}


function loadKategorieData(kategorieID,tiefe,isEdit){	
	$.ajax({
		type: "GET",
		url: '/ajax/reply_kategorie_request.php',
		data: "katID="+kategorieID+"&isEdit="+isEdit,
		dataType: "html",
		success: function(data){
			insertKategorieData(data,tiefe);
		}
	});
}

function insertKategorieData(data,tiefe){
	$('.artikel_select_element:gt('+(tiefe-1)+')').replaceWith('');
	$('#kategorie_select').append(data);
}


function bidCheck(minimalgebot){
	var gebot = kovertKomma($('#gebot').attr('value'));
	if(isNaN(gebot)){
		jAlert('Das abgegebene Gebot ist ung&uuml;ltig','Gebot nicht akzeptiert');
		return false;
	}
	
	if(gebot < minimalgebot){
		jAlert('Das Mindest-Gebot betr&auml;gt ' + number_format(minimalgebot,2,',') + ' Swapytaler','Mindestgebot unterschritten');
		return false;
	}
	else{
		bidAlert(gebot);
	}
}

function bidAlert(gebot){
	jConfirm(number_format(gebot,2,',') + ' Swapytaler bieten?', 'Gebot abgeben', function(r) {
		$('#gebot').attr('value',gebot); // set #gebot.value to gebot (with '.'-seperator)
		if(r) $('#bid').submit();
	});
}


function switchEAN(){
	if($('#input').attr('disabled')){
		$('#input,#EANButton').removeAttr("disabled");
		$('#response').show();
		$('#submit_without_ean').hide();
	}
	else{
		$('#input,#EANButton').attr('disabled', 'disabled');
		$('#response').hide();
		$('#submit_without_ean').show();
	}
}

function requestEAN(EAN,kategorieID){
	if(EAN != ''){
		$.ajax({
			type: "GET",
			url: '/ajax/reply_ean_request.php',
			data: "ean="+EAN+"&kategorieID="+kategorieID,
			dataType: "html",
			success: function(data){
				$('#response').html(data);
			}
		});
	}
}

function requestEANInhaltsangabe(EAN){
	tinyMCE.execCommand("mceRemoveControl", true, 'beschreibung');
	if(EAN != ''){
		$.ajax({
			type: "GET",
			url: '/ajax/reply_ean_inhaltsangabe_request.php',
			data: "ean="+EAN,
			dataType: "html",
			success: function(data){	
				$('#eanReply').html(data);
				
			}
		});
		
	}
}

function selectWunsch(id){
	$('#wunsch_form').show();
	$('#taler').hide();
	$('#artikel').hide();
	if(id != 'both'){
		$('#'+id).show();
		$('#choosen').attr('value',id);
	}
	else{
		$('#taler').show();
		$('#artikel').show();
		$('#choosen').attr('value','both');
	}
}

function chooseAllgemeineBewertung(posNeg){
	if(posNeg > 0){
		$('#allgemeine_bewertung_negativ').removeClass('choosenAllgemeineBewertung');
		$('#allgemeine_bewertung_negativ').addClass('notChoosenAllgemeineBewertung');
		$('#allgemeine_bewertung_positiv').removeClass('notChoosenAllgemeineBewertung');
		$('#allgemeine_bewertung_positiv').addClass('choosenAllgemeineBewertung');
		$('#tauschpartnerBewertung').attr('value',1);
		$('#allgemeine_bewertung_negativ_radio').removeAttr('checked');
		$('#allgemeine_bewertung_positiv_radio').attr('checked','checked');
	}
	if(posNeg < 0){
		$('#allgemeine_bewertung_positiv').removeClass('choosenAllgemeineBewertung');
		$('#allgemeine_bewertung_positiv').addClass('notChoosenAllgemeineBewertung');
		$('#allgemeine_bewertung_negativ').removeClass('notChoosenAllgemeineBewertung');
		$('#allgemeine_bewertung_negativ').addClass('choosenAllgemeineBewertung');
		$('#tauschpartnerBewertung').attr('value',-1);
		$('#allgemeine_bewertung_positiv_radio').removeAttr('checked');
		$('#allgemeine_bewertung_negativ_radio').attr('checked','checked');
	}
}


/* Bewertungen */

var maxBewertung = 5;

function bewertung_rating_over(ebene, element){
	for(var i = element; i > 0; i--) 
		if(i != $('#bewertung_' + ebene).attr('value'))
			$('#bewertung_' + ebene + '_' + i).attr('src','/img/symbol/rating_over.gif');
	
}

function bewertung_rating_off(ebene, element){
	
	for(var i = element; i > 0; i--) 
		if(i > $('#bewertung_' + ebene).attr('value')) // wenn i nicht der bereits selektierte Wert ist
			$('#bewertung_' + ebene + '_' + i).attr('src','/img/symbol/rating_off.gif');
}

function bewertung_select(ebene, element){

	for(var i = 1; i <= maxBewertung; i++) $('#bewertung_' + ebene + '_' + i).attr('src','/img/symbol/rating_off.gif');

	for(var i = element; i > 0; i--) $('#bewertung_' + ebene + '_' + i).attr('src','/img/symbol/rating_on.gif');
	
	
	$('#bewertung_' + ebene).attr('value',element);
	
}


function limitChars(textid, limit, infodiv){
	var text = $('#'+textid).val(); 
	var textlength = text.length;
	if(textlength > limit){
		$('#' + infodiv).html('Es sind maximal '+limit+' Zeichen erlaubt!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else{
		$('#' + infodiv).html('Du kannst noch '+ (limit - textlength) +' Zeichen eingeben.');
		return true;
	}
}
 
 


function getSuggestedTags(startingLetters){
		
	$.ajax({
		type: "GET",
		url: '/ajax/suggest_tags.php',
		data: "startingLetters="+startingLetters,
		dataType: "html",
		success: function(data){
			//insertKategorieData(data,tiefe);
			$('#suggestedTags').html(data);
		}
	});

}

function completeSuggestionTag(tagName){
	$('#tagInputField').attr('value',tagName);
	$('#suggestedTags').html('');
}

function addTag(tagName){

	tagName = tagName.toLowerCase();

	if(tagName != '' && tagName.match(/^[a-z0-9äöüß\s]+$/)){
	
		var contains = false;
	
		for(var i = 0; i < choosenTags.length; i++){
			if(choosenTags[i] == tagName) contains = true;
		}
		
		
		
		if(!contains){
	
			$.ajax({
				type: "GET",
				url: '/ajax/add_tag.php',
				data: "tagName="+tagName,
				dataType: "html",
				success: function(data){				
					$('#tagInputField').attr('value','');
					$('#suggestedTags').html('');
					
					$('#choosenTags').html(data);
					choosenTags.push(tagName);
					
				}
			});
		}
		
	}
}

function removeTag(tagName){

	var elementFound = false;
	for(var i = 0; i < choosenTags.length; i++){
		if(choosenTags[i] == tagName) elementFound = true;
		if(elementFound) choosenTags[i] = choosenTags[i+1];
	}
	choosenTags.pop();	

	if(elementFound){
	
		$.ajax({
			type: "GET",
			url: '/ajax/remove_tag.php',
			data: "tagName="+tagName,
			dataType: "html",
			success: function(data){					
				
				$('#choosenTags').html(data);
					
			}
		});
	}

}


function number_format (number, decimals, dec_point, thousands_sep){ // from http://www.fobit.com
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1){
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null){
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0){
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != ""){
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}

function kovertKomma(Wert){
  var PosPunkt = Wert.indexOf(".",0);
  var PosKomma = Wert.indexOf(",",0);
  if (PosKomma < 0) return Wert;

  while ((0 <= PosPunkt) && (PosPunkt < PosKomma))
    {
    Wert = Wert.substring(0, PosPunkt) + Wert.substring(PosPunkt + 1, Wert.length);
    PosPunkt = Wert.indexOf(".",0);
    PosKomma--;
    }

  PosKomma = Wert.indexOf(",",0);
  if (PosKomma >= 0)
    {
    Wert = Wert.substring(0, PosKomma) + "." + Wert.substring(PosKomma + 1, Wert.length);
    }

  return parseFloat(Wert);
}