var textOk = 'OK';
var textCancel = 'Abbrechen';

jQuery(document).ready(function(){
	
	/* === slider home === */
	var $slider = jQuery('#highlights .slidecont');
	var slide_nr = 0;
	var slide_w = 656;
	var slide_max = Math.ceil($slider.children().length / 4) - 1;
	
	jQuery('#highlights .slideleft a').click(function(event) {
		event.preventDefault();
		slide_nr--;
		if (slide_nr < 0) {slide_nr = 0;}
		$slider.stop().animate({'left': slide_nr * slide_w * -1}, 'fast');
	});
	
	jQuery('#highlights .slideright a').click(function(event) {
		event.preventDefault();
		slide_nr++;
		if (slide_nr > slide_max) {slide_nr = slide_max;}
		$slider.stop().animate({'left': slide_nr * slide_w * -1}, 'fast');
	});
	
	/* === slider detail === */
	var $slider2 = jQuery('#artikel .slidecont');
	var slide_nr2 = 0;
	var slide_w2 = 456;
	var slide_max2 = Math.ceil($slider2.children().length / 4) - 1;
	
	jQuery('#artikel .slideleft a').click(function(event) {
		event.preventDefault();
		slide_nr2--;
		if (slide_nr2 < 0) {slide_nr2 = 0;}
		$slider2.stop().animate({'left': slide_nr2 * slide_w2 * -1}, 'fast');
	});
	
	jQuery('#artikel .slideright a').click(function(event) {
		event.preventDefault();
		slide_nr2++;
		if (slide_nr2 > slide_max2) {slide_nr2 = slide_max2;}
		$slider2.stop().animate({'left': slide_nr2 * slide_w2 * -1}, 'fast');
	});
	
	/* === image zoom === */
	jQuery('.esimgzoom').jqzoom({
		zoomType:		'innerzoom',
		preloadText:	'',
		title:			false
	});
	
	//slideToggle
	jQuery('.collapsible_head').live('click', function(event) {
		event.preventDefault();
		jQuery(this).next('.collapsible_info').slideToggle();
		if (jQuery(this).hasClass('collapsible_hide')) {
			jQuery(this).slideUp();
		}
	});
	
	// continue shopping
	jQuery('.collapsible_back').live('click', function() {
		var $tmp = jQuery(this).closest('.collapsible_info');
		$tmp.prev('.collapsible_head').slideDown();
		$tmp.slideToggle();
	});
	
	//boxy
	jQuery('#naviBox4 .cartlink').live('click', function() {
		var bxconfig = {};
//		try {bxconfig = jQuery.parseJSON(jQuery(this).attr("boxify"));}
//			catch(e) {bxconfig = {}};
		if (! bxconfig['width'])	{bxconfig['width'] = 800;}
		if (! bxconfig['height'])	{bxconfig['height'] = 500;}
		if (! bxconfig['url'])		{bxconfig['url'] = this.getAttribute('href');}
		if (! bxconfig['title'])	{bxconfig['title'] = this.title;}
			if (! bxconfig['title']){bxconfig['title'] = '&nbsp;';}
		var	active = Boxy.linkedTo(this),
			href = bxconfig['url'],
			localOptions = {
				actuator: this,
				title: bxconfig['title'],
				closeable: true,
				closeText: 'Weiter im Shop X',
				draggable: false,
				modal: true,
				cache: false,
				filter: '#content .ajaxcont',
				unloadOnHide: true,
				afterShow: function() {
					this.resize(bxconfig['width'],bxconfig['height']);
				}
			};

		if (active) {
			active.show();
		} else {
			Boxy.load(href, localOptions);
		}

		return false;
	});
	
	/* CART FUNCTIONS */
	
	//ajax when already in boxy
	jQuery('.boxy-content form[ajaxme]').live('submit', function(event){
		event.preventDefault();
//		var tmpData = jQuery(this).serialize();
		//simulate normal click submit => encode submit-button too
		var tmpData = jQuery(this).serializeArray();
		jQuery(this).find('input[ajaxClicked=true]').each(function(){
			if (jQuery(this).attr('name')) {
				tmpData.push({
					name: jQuery(this).attr('name'),
					value: jQuery(this).attr('value')
				});
			}
		});
		tmpData = jQuery.param(tmpData);
		var tmpUrl = jQuery(this).attr('action');
		var tmpMethod = jQuery(this).attr('method');
		var $tmpBoxyContent = jQuery(this).closest('.boxy-content');
		var $tmpBoxy = $tmpBoxyContent.closest('.boxy-wrapper');
		$tmpBoxyContent.append('<div class="loading"></div>');
		$tmpBoxyContent.find('.loading').css({
			position: 'fixed',
			left: $tmpBoxy.css('left'),
			top: $tmpBoxy.css('top'),
			width: $tmpBoxy.css('width'),
			height: $tmpBoxy.css('height')
		});
		jQuery.ajax({
			async:		true,
			cache:		false,
			type:		tmpMethod,
			url:		tmpUrl,
			data:		tmpData,
			context:	this,
			success:	function(data) {
				$tmpBoxyContent.empty();
				jQuery(data).find('#content .ajaxcont').children().appendTo($tmpBoxyContent);
			},
			error:	function() {
				$tmpBoxyContent.empty();
			}
		});
	});

	//ajax when in content
	jQuery('#content form[ajaxme]').live('submit', function(event){
		event.preventDefault();
		var tmpData = jQuery(this).serialize();
		var tmpUrl = jQuery(this).attr('action');
		var tmpMethod = jQuery(this).attr('method');
		
		var tmpUrl2 = jQuery(this).attr('ajaxme');
		if (tmpUrl2 != 'ajaxme' && tmpUrl2.length > 2) {
			tmpUrl = tmpUrl2;
		}
		
		var	localOptions = {
			actuator: this,
			title: '&nbsp;',
			closeable: true,
			closeText: 'Weiter im Shop X',
			draggable: false,
			modal: true,
			type:	tmpMethod,
			cache: false,
			filter: '#content .ajaxcont',
			data: tmpData,
			unloadOnHide: true,
			afterShow: function() {
				this.resize(800,500);
			}
		};

		Boxy.load(tmpUrl, localOptions);
		this.reset();
		
	});
	
	//submit clicked submit button too () only needed in lightbox form
	jQuery('.boxy-content form[ajaxme] input[type=submit]').live('click', function(event){
		jQuery(this).closest('form[ajaxme]').find('input[type=submit]').removeAttr('ajaxClicked');
//		jQuery(this).attr('ajaxClicked', new Date().getTime());
		jQuery(this).attr('ajaxClicked', 'true');
	});

	// remove button
	jQuery('form input.button_remove').live('click', function(event) {
		jQuery(this).closest('form').find('input[name="es_shop[update_quantity]"]').val('0');
		jQuery(this).closest('form').submit();
	});
	
	//scrollbars
	jQuery('ul.boxmenu_ul').jScrollPane();
	
	jQuery('form[ask_confirm]:not([ajaxme])').live('submit', function(event){
		event.preventDefault();
		var askStr = jQuery(this).attr('ask_confirm');
		var askTitle = jQuery(this).find('input[type=submit]').val();
		Boxy.ask(askStr, [textOk, textCancel], function(r, o){
			if (r == textOk) {
				this.submit();
			}
		},{
			title: askTitle,
			actuator: this
		});
	});

});
