/**
 * Product Gallery
 * @param String id
 * @return 
 */
function Cgallery(id) {
	this.id = id;

	if (this.id == null) {
		this.id = 0;
		this.display_error = 1;
	}

	this.getType = function() {
		return jQuery("#product_type_" + this.id).html();
	}

	this.showDetail = function() {
		this.getProductData();

		if (this.display_error == 1) {
			this.productOverviewImage = 'fileadmin/res/img/px_carousel/not-found.gif';
		}

		// Switch product image
		var bigImage = this.productBigImage;
		var targetImage = this.productTargetImage;

		if (jQuery("#bigImage img").attr("src") != bigImage) {
			jQuery("#bigImage img").fadeOut("slow", function() {
				jQuery("#bigImage a").attr("href", targetImage);
				jQuery("#bigImage img").attr("src", bigImage);
				jQuery("#bigImage img").fadeIn("slow");
			});
		}

		jQuery("#px_product_selected_index").html(this.id);

		switch (this.getType()) {
 			// Product
			case "0": {
//				jQuery("#bigImage a").removeClass('zoom');

				jQuery("#px_product").attr("style", "display: block;");
				jQuery("#px_product_overview").attr("style", "display: none;");
				this.setProductData();

				if (jQuery("#px_product_request_open").html() == 1)
					jQuery("#px_products_request").attr("style", "display: block");

				break;
			}

			// Overview
			case "1": {
				document.location.hash = '';
//				jQuery("#bigImage a").addClass('zoom');

				jQuery("#px_product_overview img").attr("src", this.productOverviewImage);
				jQuery("#px_product_overview").attr("style", "display: block;");
				jQuery("#px_product").attr("style", "display: none;");
				jQuery("#px_products_request").attr("style", "display: none;");
				break;
			}
		}
	}

	this.getProductData = function() {
		this.productName = jQuery("#product_name_" + this.id).html();
		this.productDescription = jQuery("#product_description_" + this.id).html();
		this.productBigImage = jQuery("#product_big_image_" + this.id).html();
		this.productOverviewImage = jQuery("#product_overview_image_" + this.id).html();
		this.productTargetImage = jQuery("#product_target_image_" + this.id).html();
	}

	this.setProductData = function() {
		jQuery("#product_title").html(this.productName);
		jQuery("#product_description").html(this.productDescription);

		document.location.hash = this.productName;
	}
}

/**
 * Check Email
 * @param String email
 * @return boolean validEmail
 */
function checkMail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email))
		return true;
	return false;
}

/**
 * jCarousel
 * 
 * 
 */
jQuery(document).ready(function() {
	jQuery('#myCarousell').jcarousel({
		scroll: 5
	});

	jQuery("#myCarousell li div").click(function() {
		jQuery("#px_products_successful").attr("style", "display: none;");
		index = jQuery('#myCarousell li div.image').index(this);
		currentImage = jQuery('#myCarousell li div.image').get(index);
		currentClass = jQuery(currentImage).attr("class");

		if (currentClass == "image active") {
			var gallery = new Cgallery(index);
			gallery.showDetail();
		}
	});

	/**
	 * Anfrage-Formular schließen
	 */
	jQuery("#px_products_request .close").click(function() {
		jQuery("#px_products_successful").attr("style", "display: none;");
		jQuery("#px_products_request").slideUp();
		jQuery("#px_product_request_open").html(0);
	});
  
	/**
	 * Anfrage-Knopf
	 */
	jQuery("#request_form").click(function() {
		jQuery("#px_products_successful").attr("style", "display: none;");
		jQuery("#px_product_request_open").html(1);
		jQuery("#px_products_request").slideDown();

		var request_text = jQuery('#request_message').attr('value');
		if (request_text != '')
			request_text += '\n\n\n';
		
		request_text += 'Ich interessiere mich für folgenden Artikel ____________________ auf Seite ' + document.location;
		jQuery('#request_message').attr('value', request_text);
	});

 	jQuery(".jcarousel-skin-tango div.image").mouseover(function() {
		jQuery(this).addClass('active');
	}).mouseout(function() {
		jQuery(this).removeClass('active');
	});

	jQuery(".jcarousel-skin-tango div.image").click(function() {
		jQuery(".jcarousel-skin-tango div.image").removeClass('active_item');
		jQuery(this).addClass('active_item');
	});

	/**
	 * Anfrage verschickt
	 */
	jQuery("#close_request_successful").click(function() {
		jQuery("#px_products_successful").slideUp();
	});

	jQuery("#request_send").click(function() {
		var error=false;
		var emailValue=jQuery("#request_email").attr("value");

		if (!checkMail(emailValue)) {
			jQuery("#request_email").addClass("error");
			jQuery('#request_email').attr("value", "Bitte korrigieren oder ergänzen");
			error = true;
		} else {
			jQuery("#request_email").removeClass();  
		}

		if (!error) {
			index = jQuery("#px_product_selected_index").html();
			var gallery = new Cgallery(index);
			gallery.getProductData();

			jQuery.getJSON("?eID=px_products_ajax", { 
				name:gallery.productName,
				request_name:jQuery("#request_name").attr("value"),
				request_email:jQuery("#request_email").attr("value"),
				request_message:jQuery("#request_message").attr("value")
			});

			jQuery("#px_products_request").attr("style", "display: none");
			jQuery("#px_products_successful").attr("style", "display: block");
			jQuery("#px_product_request_open").html(0);
		}
	});
	
	/**
	 * Window load action. Find slide by hash 
	 */
	var needle = document.location.hash.substring(1);
	var index = null;

	// Find index for product name
	var i = 0;
	do {
		var current = jQuery("#product_name_" + i).html();
		if (current == needle) {
			index = i;
			break;
		}

		i++;
	} while (current != null && needle != '');

	// Goto slide
	if (needle != '') {
		var gallery = new Cgallery(index);
		gallery.showDetail();
	}
	
	var options = {
		zoomWidth: 372,
		zoomHeight: 516,
		xOffset: 40,
		yOffset: 0,
		position: "right",
		title: false
	};
	$('.zoom').jqzoom(options);

});