/**
 * Öffnet ein Dialog-Fenster
 * @param url			URL des Inahlts
 * @param beschreibung	Titel des Dialogs
 * @param width			Breite des Fensters
 * @param height		Höhe des Fenster
 * @param configuration JQuery Dialog Configuration
 * @param ajaxConfiguration jQuery Ajax Configuration
 */
function ShowDialog(url, beschreibung, width, height, configuration, ajaxConfiguration) {
	if(typeof configuration == "undefined") {
		configuration = {};
	}	
	if(typeof ajaxConfiguration == "undefined") {
		ajaxConfiguration = {};
	}	
	if (typeof width == "undefined") {
		width = 960;
	}
	if (typeof height == "undefined") {
		height = 440;
	}
	
	configuration.width = width;
	configuration.height = height;
	configuration.title = beschreibung;	
	
	ajaxConfiguration.url = url;
	ajaxConfiguration.success = function(response) {
		$("#dialog").html(response);
        $("#dialog").dialog(configuration);
        $("#dialog").dialog("open");

	}
	
	$.ajax(ajaxConfiguration); 
}

function ShowContentDialog(content, beschreibung, width, height) {
	if (typeof width == "undefined") {
		width = 960;
	}
	if (typeof height == "undefined") {
		height = 440;
	}

	$("#dialog").html(content);
	$("#dialog").dialog("open");
    $("#dialog").dialog({ title: beschreibung, width: width, height: height });
}

function SendMail(id_user, betreff, id_artikel, id_transaktion) {
	var url = "/index.php?page=my-neu-msg&id_user="+id_user+"&subject="+encodeURIComponent(betreff)+
		"&id_ad="+id_artikel+"&id_trans="+id_transaktion+"&frame=ajax";
	$.get(url, function(html_ajax) {
		$("#dialog_mail").html(html_ajax);
		
		var title = $("#dialog_mail h1").detach().html();
		$("#dialog_mail").dialog("open");
        $("#dialog_mail").dialog({ title: title, width: 640 });
	});
}

function SendMailPost() {
	var mailForm = $("#dialog_mail form");
	$.post("/index.php?page=my-neu-msg&frame=ajax", mailForm.serialize(), function(html_ajax) {
		$("#dialog_mail").html(html_ajax);
		
		var title = $("#dialog_mail h1").detach().html();
		$("#dialog_mail").dialog("open");
        $("#dialog_mail").dialog({ title: title, width: 640 });
	});
}

function openAdsRatingWindow(adSoldId) {
	var url = "my-marktplatz-rating,"+adSoldId+".htm?frame=ajax";	
	$.get(url, function(html_ajax) {
		var dialogContainer = $('<div id="dialog_rating" class="dialog"></div>');
		dialogContainer.insertAfter("body");
		
		dialogContainer.html(html_ajax);
		
		dialogContainer.dialog("open");
		dialogContainer.dialog({ title: "Bewerten", width: 640 });
	});
}

/**
 * Initialisiert den "Produkt merken"-Button einer anzeige
 * @param id_ad		ID der Anzeige
 * @param what		Schmalle fragen / ad_reminder.php ansehen
 * @param elm		Schmalle fragen / ad_reminder.php ansehen
 */
function ad_reminder(id_ad, what, elm)
{   
	var id_kat = 0;
	var element = (typeof elm == 'undefined' ? '' : elm);
	var id = 'ad_reminder_'+id_ad+element; 
	$.ajax({
		url: 		'/index.php?frame=ajax&page=ad_reminder&ID_AD=' + id_ad + '&ID_KAT=' + id_kat + '&what=' + what+'&elm='+element,
		type: 		'GET',
		success: 	function(result) {
			$('#'+id).html(result);
		}
	});
}

/**
 * Wechselst zwischen Anzeige gefällt mir und Anzeige gefällt mir nicht mehr 
 * @param adId
 */
function toggleAdLike(adId, callback) {
	$.ajax({
		url: '/marktplatz_anzeige_gefallen,'+adId+',toggle.htm?frame=ajax',
		dataType: 'json',
		success: function(response) {
			if(response.success == true) {
				callback.call(this, response)
			}
		} 
	});
}
function getAdLikeButton(adId, button) {
	$.ajax({
		url: '/marktplatz_anzeige_gefallen,'+adId+',get.htm?frame=ajax',
		dataType: 'json',
		success: function(response) {
			if(response.success == true) {
				if(response.like == true) {
					$(button).html("Anzeige gef&auml;llt mir doch nicht");
				} else {
					$(button).html("Anzeige gef&auml;llt mir");
				}
			}
		} 
	});
}
function getAdLikeCount(adId, callback) {
	$.ajax({
		url: '/marktplatz_anzeige_gefallen,'+adId+',count.htm?frame=ajax',
		dataType: 'json',
		success: function(response) {
			if(response.success == true) {
				callback.call(this, response)
			}
		} 
	});
}

function verkauf_bestaetigen(adId, sellId, confirm) {
	var url = 'sale_handle,'+adId+','+sellId+',accept,'+(confirm ? 1 : 0)+'.htm';
	ShowDialog(url, 'Verkauf akzeptieren', 560, 460, { });
}

function verkauf_bestaetigen_post(adId, sellId) {
	if (confirm('Verkauf wirklich bestätigen?')) {
		verkauf_bestaetigen(adId, sellId, 1);	
		show_details(sellId);
		show_details(sellId);
	}
}

function verkauf_ablehnen(adId, sellId, confirm, reason_text, disable_ad) {
	var url = 'sale_handle,'+adId+','+sellId+',decline,'+(confirm ? 1 : 0)+'.htm';
	if (confirm) {
		$.post(url, { reason: reason_text, disable: disable_ad }, function(html_ajax) {
			ShowContentDialog(html_ajax, 'Verkauf ablehnen', 560, 460);
			show_details(sellId);
			show_details(sellId);
		});
	} else {
		ShowDialog(url, 'Verkauf ablehnen', 560, 460, { });
	}
}

function verkauf_ablehnen_post(adId, sellId) {
	if (confirm('Verkauf wirklich ablehnen?')) {
		var id_reason = $("input[name=reason]:checked").attr("id");
		var disable = $("input[name=disable]:checked").length;
		if (id_reason == "reason_other") {
			verkauf_ablehnen(adId, sellId, 1, $("#reason_custom").val(), disable);
		} else {
			verkauf_ablehnen(adId, sellId, 1, $("#"+id_reason).val(), disable);
		}
	}
}

function YoutubeCheckInput(input) {
	var name = $(input).attr("name");
	var validation = $("#youtube_"+name);
	var validation_img = $("#youtube_"+name+" > img");
	var submit_button = $(input).parent().children("input[type=submit]");
	if (validation.length > 0) {
		var url = $(input).val();
		var matches = false;
		if (url.match(/youtu.be\/([A-Za-z0-9-_]+)(\&|$)/gi)) matches = true;
		if (url.match(/youtube.com\/watch\?.*v=([A-Za-z0-9-_]+)(\&|$)/gi)) matches = true;
		if (matches) {
			validation.attr("title", "Youtube-Link ist korrekt.");
			validation_img.attr("src", "/bilder/ok.png");
			submit_button.attr("disabled", false);
		} else {
			validation.attr("title", "Ist kein (g&uuml;ltiger) Youtube-Link!");
			validation_img.attr("src", "/bilder/stop_check.png");
			submit_button.attr("disabled", true);
		}
	}
}

function YoutubePlayVideo(href, width, height) {
	var width = (typeof width == "undefined" ? 400 : width);
	var height = (typeof height == "undefined" ? 300 : height);
	$("#dialog").html('<iframe width="'+width+'" height="'+height+'" src="'+href+'" frameborder="0" allowfullscreen></iframe>');
	$("#dialog").dialog({
		width: width + 26,
		height: height + 48,
		modal: true,
		resizable: false,
		draggable: false,
		autoOpen: true,
        stack: true
	}).parent().show('scale', { percent: 100 }, 500, function() {
		$("#dialog iframe").removeAttr("style");
	});
	return false;
}

