jQuery(function ($) {

  // SEARCH
	  if (window.location.href.indexOf("&searchwords=") > 0) {
		  var searchWords = decodeURI(window.location.href.substr(window.location.href.indexOf("&searchwords=")+13));
		  searchWords = searchWords.split(" ");
			$(searchWords).each(function (i) {
		  	var searchWord = searchWords[i].toLowerCase();
				var foundTextNodes = $('#contentContainer *:not(iframe)').contents().filter(function(){ return this.nodeType === 3; }).filter(function(){ return this.nodeValue.toLowerCase().indexOf(searchWord) != -1; });
				foundTextNodes.each(function () {
				  var pos = this.nodeValue.toLowerCase().indexOf(searchWord);
				  $(this).replaceWith(this.nodeValue.substr(0, pos) + '<span class="foundInSearchResult">' + this.nodeValue.substr(pos, searchWord.length) + "</span>" + this.nodeValue.substr(pos + searchWord.length));
				});
			});
		}
		
	// helFlightGalery
		$('#helFlightGalery a img').each(function () {
			var me = $(this);
			var alignMe = function () { me.css('margin-top', parseInt((me.parent().height() - me.height()) / 2) + "px"); };
			if (this.complete) alignMe();
			else me.bind('load', alignMe);
		});
		
	// FILE OR FOLDER
		$('.cms__contentElements__fileOrFolder:first').css('margin-top', '-4px');
		$('.cmsFolderInfo img, .cmsFolderInfo span').bind('click', function () {
		  var me = $(this).parents('li:first');
		  if (me.data('childrenShown')) me.children("ul").hide();
		  else me.children("ul").show();
		  me.data('childrenShown', !me.data('childrenShown'));
		  return false;
		});

	// TOP-LINK
	  $(window).bind('load', function () {
	    if ($('#contentContainer').height() < 1000) return;
	    $('#bodyWrapper2').prepend('<a name="gotoPageTop"></a>');
	    $('#contentContainer').append('<div style="padding-top:20px; clear:both; text-align:right;"><a href="#gotoPageTop">zum Anfang der Seite</a> <a href="#gotoPageTop" style="text-decoration:none;"><img align="absbottom" src="templates/Helipartner_09/images/toTop.gif" alt="zum Anfang der Seite" /></a></div>');
		});
		
});



// SHADOWBOX
  Shadowbox.init({
    autoDimensions: true,
    handleOversize: "drag",
    handleUnsupported: "remove",
    overlayColor: "#fff"
	});



// SHOP
	var cwxShopBasketItemText = "";
	// cwxShopBasketItemCount is defined in template-header (../beforeProcessing.php)
	
	function showCwxShopBasketItems () {
	  var item = jQuery('#warenkorbMenuItem');
	  if (cwxShopBasketItemText == "") cwxShopBasketItemText = item.text();
		item.text(cwxShopBasketItemText + " (" + cwxShopBasketItemCount + ")");
	}
	jQuery(showCwxShopBasketItems);
	
	function addCwxShopBasketItem (itemId) {
	  // options
		  var optionSelect = jQuery('#cmsContentElement_' + itemId).find('select[name=cwxShopArticleOptions]');
		  var options = [];
		  var optionsCombo;
		  if (optionSelect.length) {
				optionSelect.find('option').each(function () { options.push([jQuery(this).text(), jQuery(this).text()]); });
		 		// create ComboBox
				  optionsCombo = new Ext.form.ComboBox({
		        fieldLabel: 'Variante',
				    name: 'option',
				    store: new Ext.data.SimpleStore({
							fields: ['key', 'value'],
							data: options
						}),
				    valueField: 'value',
				    displayField: 'value',
				    typeAhead: true,
				    mode: 'local',
				    triggerAction: 'all',
				    selectOnFocus: true,
				    forceSelection: true,
				    allowBlank: false
				  });
			}
	  var win = new Ext.Window({
	    layout: 'fit',
	    title: "Warenkorb",
	    modal: true,
	    width: 355,
	    height: (optionSelect.length ? 150 : 110),
			bodyStyle: 'padding:5px;',
	    items: [
				new Ext.form.FormPanel({
		      border: false,
		      frame: false,
		      bodyStyle: 'background-color:transparent;',
		      items: [
		        new Ext.form.NumberField({
		          fieldLabel: 'Anzahl',
		          name: 'itemCount',
		          allowNegative: false,
		          allowDecimals: false,
		          minValue: 1,
		          value: 1
		        }),
		        (options.length ? optionsCombo : { hidden: true })
		      ]
	    	})
			],
	    buttons: [
	      {
	        text: 'abbrechen',
	        handler: function () { this.findParentByType('window').close(); }
	      }, {
	        text: 'kaufen',
	        handler: function () {
	          var form = this.findParentByType('window').items.first().getForm();
	          if (!form.isValid()) return;
	          var valueArr = new Object();
	          for (i=0; field = form.findField(i); i++) if ((wert = field.getValue()) != "") valueArr[field.getName()] = wert.toString().replace(/"/g, "&quot;");
						jQuery.post("index.php?ajaxPhpScriptId=" + cwxShopBasketItemAddAjaxId, {
							id: itemId,
							count: valueArr.itemCount,
							option: valueArr.option ? valueArr.option : 0
						}, function (data) {
							if (data == "OK") {
								cwxShopBasketItemCount += parseInt(valueArr.itemCount);
								showCwxShopBasketItems();
								Ext.Msg.alert('Warenkorb', valueArr.itemCount + " Artikel wurden erfolgreich in den Warenkorb gelegt.");
							} else Ext.Msg.alert('Warenkorb', 'Es ist ein unbekannter Fehler aufgetreten, bitte versuchen Sie es später noch einmal oder benutzen Sie unser Kontaktformular!');
						});
						this.findParentByType('window').close();
	        }
	      }
	    ]
	  }).show();
	  // prevent scrollbars on form item click
	  jQuery(win.getEl().dom).find('.x-form').one('mouseenter', function () { jQuery(this).css('overflow', 'hidden'); });
	}
