/* ------------------------ */
/* Insert Facebook-Button  	*/
/* ------------------------ */
function insert_facebook_button() {
	var html = "";
	$(".facebook_like_button").each(function (index, element) {
		var $element = $(element);
		var href = $element.attr("href");
		html = "<iframe class=\"facebook_like_button\" src=\"http://www.facebook.com/plugins/like.php?href=" + escape(href) + "&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=21\""
				+ " scrolling=\"no\""
				+ " frameborder=\"0\""
				+ " style=\"border:none; overflow:hidden; width:75px; height:21px;\""
				+ " allowTransparency=\"true\"></iframe>";
		$element.replaceWith(html);
	});
}

//Browser
var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Chrome"
			},
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari",
				versionSearch: "Version"
			},
			{
				prop: window.opera,
				identity: "Opera",
				versionSearch: "Version"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				   string: navigator.userAgent,
				   subString: "iPhone",
				   identity: "iPhone/iPod"
		    },
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]

	};
BrowserDetect.init();


function handleSliderChange(e, ui) {
	var maxScroll = $("#content-scroll").attr("scrollWidth")
			- $("#content-scroll").width();
	$("#content-scroll").animate({
		scrollLeft : ui.value * (maxScroll / 100)
	}, 1000);
}

function handleSliderSlide(e, ui) {
	var maxScroll = $("#content-scroll").attr("scrollWidth")
			- $("#content-scroll").width();
	$("#content-scroll").attr({
		scrollLeft : ui.value * (maxScroll / 100)
	});
}

function func_get_widget_id(elem, widget_name) {
	var widget_id = $(elem).attr("id");
	widget_id = widget_id.replace("widget-" + widget_name + "-", "");
	return widget_id;
}

/* ---------------------------- */
/* Dropdownlisten Submit */
/* ---------------------------- */
function submit_dropdownliste(elem, dropdown_id) {
	// Wert aus dropdown
	var form = $(elem).closest("form");
	var value = $(elem).attr("rel");
	$("#" + dropdown_id).val(value);
	func_post_titelliste(form);
}

function submit_dropdownltiteliste(elem, dropdown_id) {
	// Wert aus dropdown
	var form = $(elem).closest("form");
	$(form).find("input[name='titel_ids']").val("");
	var value = $(elem).attr("rel");
	$("#" + dropdown_id).val(value);
	func_post_titelliste(form);
}

function func_post_titelliste(form, sortierung) {
	$(form).spin();
	var form_id = "#" + $(form).attr("id");
	var form_url = $(form).find("input[name='url']").val();
	var form_data = $(form).serialize();
	$.post(form_url, form_data, function(data) {
		$(form).replaceWith(data);
		// dropdowns
		$("select").wh_dropdown({
			speed : "fast",
			type : "hiddenfield",
			width_selector : "37",
			width_shadow : "10"
		});
		// external links
		$("a.external, a[rel*=external]").attr("target", "_blank");
	});
	return false;
}

/* -------------------------------- */
/* Flashplayer */
/* -------------------------------- */

function replace_flashplayer_popup(file, autoplay) {
	$("#player").flash({
		src : "/_files_global/player.swf",
		width : 480,
		height : 29,
		wmode : "transparent",
		flashvars : {
			autostart : autoplay,
			type : "mp3",
			allowfullscreen : false,
			file : "/_files_global/audio/" + file + ".mp3",
			skin : "/_files_global/skin_370.swf",
			wmode : "transparent"
		}
	});
}

function replace_flashplayer(file, autoplay) {
	$("#audio_player_" + file).flash({
		src : "/_files_global/player.swf",
		width : 180,
		height : 23,
		wmode : "transparent",
		flashvars : {
			autostart : autoplay,
			type : "mp3",
			allowfullscreen : false,
			file : "/_files_global/audio/" + file + ".mp3",
			skin : "/_files_global/skin_180.swf",
			wmode : "transparent"
		}
	});
}


function closetooltip(id) {
	$("#tooltip_" + id).hide();
}

// document ready functions
$(document).ready(function() {

	$("a[rel='submit']").click(function() {
		var $form = $(this).closest("form");
		$form.submit();
		return false;
	});

	// external links
	$("a.external, a[rel*=external]").attr("target", "_blank");

	$("a.chronik_zurueck").click(function() {
		history.back();
		return false;
	});

	$.fn.spin = function() {
		this.each(function() {
			var $this = $(this), data = $this.data();
			var opts = {
				lines : 12, // The number of lines to draw
				length : 7, // The length of each line
				width : 4, // The line thickness
				radius : 10, // The radius of the inner circle
				color : '#ff0000', // #rgb or #rrggbb
				speed : 1, // Rounds per second
				trail : 60, // Afterglow percentage
				shadow : false
			// Whether to render a shadow
			};

			if (data.spinner) {
				data.spinner.stop();
				delete data.spinner;
			}
			if (opts !== false) {
				data.spinner = new Spinner($.extend({
					color : $this.css('color')
				}, opts)).spin(this);
			}
		});
		return this;
	};

//	Katalogbestellung
	$('.newsletter_email').example('Ihre E-Mail-Adresse');
	tips_leftOffset = -280;
	if (BrowserDetect.browser == "Explorer") {
		tips_leftOffset = -420;
	}
	if (BrowserDetect.browser == "Firefox" && BrowserDetect.version < 5) {
		tips_leftOffset = -420;
	}
	$("a.tips").cluetip({
		sticky : true,
		mouseOutClose: true,
		width : '280px',
		showTitle : false,
		ajaxCache : true,
		arrows : false,
		waitImage : true,
		positionBy : "fixed",
		topOffset : -170,
		leftOffset: tips_leftOffset,
		clickThrough : true,
		closeText : "x",
		onShow: function(ct, c) {
			$(".colorbox_bestellen").colorbox({width:"400", height:"180", iframe:true, overlayClose:false, close:""});
			$("a.colorbox").colorbox();
			}
	});
	/* ---------------- */
	/* Dropdowns */
	/* ---------------- */
	$("select.wh_dropdown").wh_dropdown({
		speed : "fast",
		type : "hiddenfield",
		width_selector : "37",
		width_shadow : "10"
	});

	$("a[rel='ajax']").click(function() {
		var form = $(this).closest("form");
		$(form).spin();
		var form_id = $(form).attr("id");
		var form_url = $(form).find("input[name='url']").val();
		var form_data = $(form).serialize();
		$.get(form_url, form_data, function(data) {
			$(form).replaceWith(data);
		});
		return false;
	});

	// COLORBOX
	$("a.colorbox").colorbox({width: "650px"});


	$("a[rel='colorbox_close']").click(function() {
		parent.$.fn.colorbox.close();
		return false;
	});

	$("a[rel='colorbox ajax']").click(function() {
		var form = $(this).closest("form");
		var form_url = $(form).attr("action");
		var form_data = $(form).serialize();
		if (form_data.length) {
			form_url = form_url + '?' + form_data
		}
		$.colorbox({
			href : form_url,
			open : true,
			iframe : true,
			width : "760px",
			height : "650px",
			fixed : true
		});
		return false;
	});

	$("a.colorbox.iframe").click(function() {
		$.colorbox({
			iframe : true,
			innerWidth : 700,
			innerHeight : 620
		})
	});

	$("a[rel='colorbox iframe']").click(function() {
		var form = $(this).closest("form");
		var url = $(form).attr("action");
		$.colorbox({
			iframe : true,
			innerWidth : 700,
			innerHeight : 620,
			scrolling : false,
			href : url
		})
		return false;
	});


	// Facebook Button
	insert_facebook_button();

	$("a.close").click(function() {
		var container = $(this).closest(".alert-message");
		$(container).slideUp("slow");
	})

	// scrollto
	$(".scrollto").click(function() {
		var anchor = $(this).attr("href");
		$.scrollTo(anchor, "slow");
	});
});

