$(document).ready(function() {

	//find all form with class jqtransform and apply the plugin
	$(function() {
		$('form.jqtransform').jqTransform();
	});

	// input logic
	var fillInputWithTitle = function() {
		if ($(this).val() == '' && $(this).attr('title')) {
			$(this).val($(this).attr('title'));
			$(this).addClass('inputDefaultValue');
		} else {
			$(this).removeClass('inputDefaultValue');
		}
	};
	
	var removeTitleFromInput = function() {
		if ($(this).hasClass('inputDefaultValue') && $(this).val() == $(this).attr('title')) {
			$(this).val('');
			$(this).removeClass('inputDefaultValue');
		}
	};

	$('input[type=text]').each(fillInputWithTitle);
	$('input[type=text]').blur(fillInputWithTitle);
	$('input[type=text]').bind('click focus', removeTitleFromInput);
	$('form').bind('submit', function() {
		$('input[type=text]').each(removeTitleFromInput);
	});

	// make the links hovering
	$('.link-default').live('mouseenter', function() {
		$(this).hide().next('.link-active').show();
	});
	$('.link-active').live('mouseleave', function() {
		$(this).hide().prev('.link-default').show();
	});
	
	
	// make all externallinks open up in a new popup
	$('.externallink').each(function() {
		$(this).attr('target', '_new');
	});
	


	// show/hide for news
	$('.news-morelink, .news-lesslink').click(function() {
		$(this).parent().children('.news-lesslink, .news-morelink').toggle();
		$(this).parent().children('.news-details').slideToggle();
		return false;
	});

	// show/hide for workshops
	$('.workshop-morelink, .workshop-lesslink').click(function() {
		$(this).parent().children('.workshop-lesslink, .workshop-morelink').toggle();
		$(this).parent().children('.workshop-body-extended').slideToggle();
		return false;
	});

	// show/hide for artists
	$('.artist-references-morelink, .artist-references-lesslink').click(function() {
		$(this).parent().children('.artist-references-lesslink, .artist-references-morelink').toggle();
		$(this).parent().children('.artist-references-extended').slideToggle();
		return false;
	});

	// show/hide for fce
	$('.fce-morelink, .fce-lesslink').click(function() {
		$(this).parent().children('.fce-lesslink, .fce-morelink').toggle();
		$(this).parent().children('.fce-details').slideToggle();
		return false;
	});

	// show/hide for retailsearch
	$('.retailsearch-morelink, .retailsearch-lesslink').click(function() {
		$(this).parent().children('.retailsearch-lesslink, .retailsearch-morelink').toggle();
		$(this).parent().children('.retailsearch-body-extended').slideToggle();
		return false;
	});
	
	// startpage overview. make the whole div a link
	$('.teaser-text a').each(function() {
		var anchor = $(this).attr('href');
		$(this).parents('.teaser-singleItem').click(function(evt) {
			window.location.href = anchor;
		});
	});
	$('.teaser-singleItem').css('cursor', 'pointer');
	// product overview. make the whole div a link
	$('.product-teaser-text a').each(function() {
		var anchor = $(this).attr('href');
		$(this).parents('.product-teaser-singleItem').click(function(evt) {
			window.location.href = anchor;
		});
	});
	$('.product-teaser-singleItem').css('cursor', 'pointer');
	
	// newsletter
	if ($('#newsletter-register').length) {
		$('#newsletter-on').change(function() {
			if ($(this).attr('checked')) {
				$(this).parent().next('label').addClass('active').removeClass('inactive');
				$('#newsletter-off').parent().next('label').removeClass('active').addClass('inactive');
			} else {
				$(this).parent().next('label').removeClass('active').addClass('inactive');
				$('#newsletter-off').parent().next('label').addClass('active').removeClass('inactive');
			}
		});

		$('#newsletter-register').submit(function() {
			if ($('#email').val().length == 0 || $('#email').val().indexOf('@') == -1) {
				$('#email').parents('.inputDiv').addClass('inputDivError')
					.next().find('.error').show();
			} else {
				$('#email').parents('.inputDiv').removeClass('inputDivError')
					.next().find('.error').hide();
				$.post(
					$(this).attr('action'),
					$(this).serialize()
				);
				$('#newsletter-confirm').show();
			}
			$('input[type=text]').each(fillInputWithTitle);
			return false;
		});

		// check if the submission came from the sidebar
		var email = (window.location.href);
		email = email.substr(email.indexOf('email=')+6);
		if (email) {
			email = decodeURIComponent(email);
			email = email.substr(0, email.indexOf('&'));
			if (email) {
				$('#email').val(email).removeClass('inputDefaultValue');
			}
		}
	}
	





	// zoom functionality
	$('.product-picture').mouseenter(function(evt) {
		if (jQuery.browser.mozilla && navigator.userAgent.indexOf('Macintosh') != -1) {
			$('body, .product-picture').css('cursor', 'pointer');
		} else {
			$('body, .product-picture').css('cursor', "url('/fileadmin/templates/css/img/mauszeiger_plus.cur'), url('../css/img/mauszeiger_plus.cur'), pointer");
		}
	});
	$('.product-picture').mouseleave(function(evt) {
		$('body, .product-picture').css('cursor', 'auto');
	});

	// zoom clicked
	$('.product-detail-image-link, .product-picture').click(function(evt) {
		if (jQuery.browser.mozilla && navigator.userAgent.indexOf('mac') != -1) {
			$('body, .product-detail-image').css('cursor', 'pointer');
		} else {
			$('body, .product-detail-image').css('cursor', "url('/fileadmin/templates/css/img/mauszeiger_minus.cur'), url('../css/img/mauszeiger_minus.cur'), pointer");
		}

		// show the product image as overlay
		$('.product-detail-image').show();
		$('#contentBlock-1').hide();
		evt.preventDefault();
		evt.stopPropagation();
		return false;
	});
	
	$('.product-detail-image').click(function() {
		$(this).hide();
		$('#contentBlock-1').show();
		$('body').css('cursor', 'auto');
		return false;
	});
	
	
	// download portal
	if ($('#portalfilter').length) {

		// called when there was something selected
		var swSelectChange = function(evt) {
			var selName = $(this).parents('.jqTransformSelectWrapper').find('select').attr('name');
			if (selName == 'selector1') {
				var sel2 = $('#portalfilter select[name=selector2]');
				swRemapSelectTransform(sel2);
				sel2.parent().parent().removeClass('portalfilterHideSelect').addClass('portalfilterDummySelect');
				defaultStr = sel2.prev('ul').find('a:eq(0)').html();
				sel2.parent().find('span:eq(0)').html(defaultStr);
			}
			$(this).parents('.portalfilterDummySelect').removeClass('portalfilterDummySelect');
			evt.preventDefault();
		};
		
		
		var swRemapSelectTransform = function(select) {
			var ul = select.prev('ul').empty();
			var wrapper = select.parent();

			$('option', select).each(function(i){
				var oLi = $('<li><a href="#" class="jqTransformOption" index="'+ i +'">'+ $(this).html() +'</a></li>');
				ul.append(oLi);
			});
			ul.find('a').first().addClass('selected');
			ul.find('a')
				.bind('click', swSelectChange)
				.bind('click', function(){
				$('a.selected', wrapper).removeClass('selected');
				$(this).addClass('selected');	
				/* Fire the onchange event */
				if (select[0].selectedIndex != $(this).attr('index') && select[0].onchange) {
					select[0].selectedIndex = $(this).attr('index');
					select[0].onchange();
				}
				select[0].selectedIndex = $(this).attr('index');
				$('span:eq(0)', wrapper).html($(this).html());
				ul.hide();
				$('div:eq(0)', wrapper).removeClass('open');
				return false;
			});
		}
		
	
		$('#portalfilter').jqTransform();
		$('a.jqTransformOption').bind('click', swSelectChange);
		$('#portalfilter select[name=selector2]').hide();
		
		var JSactivate = function(level) {
			var selectorBox = document[this.formname]["selector"+level];
			var entryID = selectorBox.options[selectorBox.selectedIndex].value;
			if (this.entry[entryID])	{
				this.writeOut(this.entry[entryID].child,this.entry[entryID].openID,level+1);
				if (this.entry[this.entry[entryID].parent])	{
					this.entry[this.entry[entryID].parent].openID = entryID;
				}
				// send request to the server
				if (this.entry[entryID].url) {
					url = this.entry[entryID].url;
					url += (url.indexOf('?') != -1 ? '&' : '?') + 'type=14';
					$.get(url, function(data) {
						$('#portalfilter-list').html(data).show();
					}, 'text');
				}
			}
		}
		portalFilter.act = JSactivate;

		$('#formPortalFilterSubmit').click(function() {
			$('#formPortalfilter').submit();
		});


		$('#portalFilterProductForm input[type=image]').click(function() {
			$('#portalFilterProductForm').submit();
		});

		$('#portalFilterProductForm').submit(function() {
			var url = document.location.href;
			url += (url.indexOf('?') != -1 ? '&' : '?') + 'type=14';
			url += '&portalFilterSword=' + $('#portalFilterSword').val();
			$.get(url, function(data) {
				$('#portalfilter-list').html(data).show();
			}, 'text');
			return false;
		});
	}


	// retailersearch
	if ($('#map_form').length && $('#map_form .jqTransformSelectWrapper').length) {
		var wrapper = $('#map_form .jqTransformSelectWrapper');
		if ($('a.selected', wrapper).attr('index') == 0) {
			wrapper.parent().addClass('portalfilterDummySelect');
			$('a.jqTransformOption', wrapper).bind('click', function() {
				wrapper.parent().removeClass('portalfilterDummySelect');
			});
		}

	}

	$('select[name=map_country]').change(function() {
			// don't do the auto-submit on the start page
		if ($(this).parents('.startPageRetailerBox').length > 0) {
			return;
		}
		$(this).parents('form').submit();
	});

	// retailer list
	$('.retailsearch-body a').click(function(evt) {
		evt.stopPropagation();
	});


});

var Store = {

	find: function() {
		var addr = $('#map_address').val();
		var zip = parseInt(addr);
		if (typeof zip == 'number' && zip > 0 && zip == addr) {
			//fetchCityFromZip(zip);
			Store.startGeoCode(addr);
		} else {
			Store.startGeoCode(addr);
		}
	},

	fetchCityFromZip: function(zip) {
		GDownloadUrl('index.php?id=7&type=740&zip=' + zip, function(city, httpCode) {
			if (city.length > 0 && httpCode == 200) {
				$('#map_address').val(zip + ' ' + city);
				Store.startGeoCode(zip + ' ' + city);
			}
		});
	},

	startGeoCode: function(addr) {
		if (addr.length > 0) {
			addr += ' Germany';
			var geocoder = new GClientGeocoder();
			geocoder.getLatLng(addr, Store.centerMapOnPoint);
		}
	},

	centerMapOnPoint: function(latLng) {
		if (latLng) {
			WecMap.get('map').setCenter(latLng, 10);
		} else {
			$('#search_output').html('Die Suche nach <em>' + $('#map_address').value + '</em> ergab keine Treffer');
		}
	}
};
