jQuery.fn.reverse = [].reverse;

var currentColor   = null;
var currentSize    = null;
var currentForm    = null;
var currentVariant = null;

var sysImageDivPost = null;

var bIsNewSelectColor = false;

$(function()
{
	// >>> BEGIN COLOR-Click
    $(".cItemColor").live("click", 
		function(){
			if($(this).hasClass("disabled")) return;

			$(".cItemColor").removeClass("highlighted");
			//$(".cItemColor .highlighted").removeClass("highlighted");
			$(this).addClass("highlighted");
			currentColor = $(this).attr("id");
			
			enabledSizes = variants.getWidthByColor(currentColor);
//        formen = variants.getFormByColornumber($(this).attr("id"));
        
			// >>> begin each size-list
			var bIsNewSelectSize = false;
			$(".cItemSize").each(
				function(){
					$(this).removeClass("highlighted");
					$(this).removeClass("disabled");
					$(this).addClass("disabled");
					
					for(var i = 0;i < enabledSizes.length;i++)
					{
						if($(this).text() == enabledSizes[i])
						{
							$(this).removeClass("disabled");
							if(currentSize == null)
							{
								currentSize = $(this).text();
								$(this).addClass("highlighted");
								$(this).click();
								bIsNewSelectSize = true;
							
							}else 
								if($(this).text() == currentSize)
							{
								$(this).addClass("highlighted");
								bIsNewSelectSize = true;
							}
						}
					}
				}
			);

			if(!bIsNewSelectSize){
			    $(".cItemSize").each(
					function(){
						if(
							!bIsNewSelectSize
							&&
							!$(this).hasClass("disabled")
						){
							$(this).click();
							bIsNewSelectSize = true;
						}
					}
				);
			}
			// <<< end each size-list
			setVariantForBasket();
			setPrice();

			//$(".cItemColor").removeClass("colorhighlighted");
			//$(".cItemColor").addClass("colorhighlighted");
		}
	);
	// <<< END COLOR-Click


	// <<< BEGIN SIZE-Click
    $(".cItemSize").live("click", 
		function(){
			if($(this).hasClass("disabled")) return;

			$(".cItemSize").removeClass("highlighted");
			$(this).addClass("highlighted");
			currentSize = $(this).text();

			enabledForms = variants.getFormsByColorAndSize(currentColor, currentSize);
        
			// >>> begin each size-list
			var bIsNewSelectForm = false;
			$(".cItemForm").each(
				function(){
					$(this).removeClass("highlighted");
					$(this).removeClass("disabled");
					$(this).addClass("disabled");
					
					for(var i = 0;i < enabledForms.length;i++)
					{
						if($(this).text() == enabledForms[i])
						{
							$(this).removeClass("disabled");
							if(currentForm == null)
							{
								$(this).addClass("highlighted");
								$(this).click();
								bIsNewSelectForm = true;
							
							}else 
								if($(this).text() == currentForm)
							{
								$(this).addClass("highlighted");
								bIsNewSelectForm = true;
							}
						}
					}
				}
			);

			if(!bIsNewSelectForm){
			    $(".cItemForm").each(
					function(){
						if(
							!bIsNewSelectForm
							&&
							!$(this).hasClass("disabled")
						){
							$(this).click();
							bIsNewSelectForm = true;
						}
					}
				);
			}

			// <<< end each size-list
			setVariantForBasket();
			setPrice();
		}
	);
	// >>> END SIZE-Click


	// <<< BEGIN FORM-Click
    $(".cItemForm").live("click", 
		function(){
			if($(this).hasClass("disabled")) return;
			$(".cItemForm .highlighted").removeClass("highlighted");
			$(this).addClass("highlighted");
			currentForm = $(this).text();
			setVariantForBasket();
			setPrice();
		}
	);
	// >>> END FORM-Click


	var filterColors = (readCookie('sysColor_names')+ "").split("[![!]!]");
	for(i=0; i<filterColors.length&&!bIsNewSelectColor;i++){
	    $(".cItemColor").each(function () {
			if(
				$(this).attr('id') == filterColors[i]
				&&
				!bIsNewSelectColor
				&&
				!$(this).hasClass("disabled")
			){
				$(this).click();
				bIsNewSelectColor = true;
			}
		});
	}

    $(".cItemColor").each(
		function(){
			if(
				!bIsNewSelectColor
				&&
				!$(this).hasClass("disabled")
			){
				$(this).click();
				bIsNewSelectColor = true;
			}
		}
	);
    
	var selSize = readCookie('selSize')+ "";
	document.cookie = 'selSize=null; path=/'; 
	if(
		selSize != null
		&&
		selSize != "null"
		&&
		selSize != ""
	)
    {
		var bIsSelected = false;

		$(".cItemColor").each(function ()
        {
			oCurrColor = $(this);

			if(!bIsSelected)
            {
                oCurrColor.click();

                $(".cItemSize").each(function ()
                {
					oCurrSize = $(this);
					
					if(
						!oCurrSize.hasClass("disabled")
						&&
						oCurrSize.text() == selSize
					)
                    {
						oCurrSize.click();
						bIsSelected = true;
					}
				});
			}
		});

	}

	$("form").live("submit",function(){
		$("input[name='am']").val(
			$("#anzahl").val()
		);
	});
});

function setPrice(){
	if(currentVariant.length>0){
		$("#anzahl").css({'visibility':'visible'});
		fp  = currentVariant[0]._fprice;
		ftp = currentVariant[0]._ftprice;

		isDiscount = (ftp!='')&&fp!=ftp;

		$('#productFPrice').html(fp+' &euro;')

		if(isDiscount){
		    $('#productFPrice').css({ 'color': '#3b3b3b' });
			$('#productFTPrice').html('<del>'+ftp+' &euro;</del>');
		}else{
            $('#productFPrice').css({ 'color': '#3b3b3b' });
			$('#productFTPrice').html('');
		}
	}else{
		$("#anzahl").css({'visibility':'hidden'});
		$('#productFPrice').html('zur Zeit nicht lieferbar')
	}

}

function setVariantForBasket(){
		currentVariant = variants.getVariants(currentColor, currentSize, currentForm);
//		alert(currentVariant);

		if(currentVariant.length>0){
			$("#test_toBasket").css({'visibility':'visible'});
			$("#gesetzAngaben").css({'visibility':'visible'});
			$("#anzahl option").remove();
			
			for(var j = 1;j <= currentVariant[0]._quantity;j++)
			{
				$("#anzahl").append("<option value='" + j + "'>" + j + "</option>");
			}

			$("input[name='aid']").val(currentVariant[0]._id);
			currentVariant[0].setImage();
		}else{
			$("#gesetzAngaben").css({'visibility':'hidden'});
			$("#test_toBasket").css({'visibility':'hidden'});
		}
}

/*
function setAutomaticHighlight()
{
}
// */

/*
function countColor()
{
}
// */

/*
function countWidth()
{
}
// */

/*
function countForm()
{
}
// */

function variantPicture(icon, detail, zoom){
	this._icon   = icon;
	this._detail = detail;
	this._zoom   = zoom;
}

function variant(
	  id
	, size
	, color
	, form
	, quantity
	, description
	, colorValue
	, fprice
	, ftprice
	, pic1
	, pic2
	, pic3
	, pic4
	, pic5
	, pic6
	, pic7
	, pic8
	, pic9
	, pic10
	, pic11
	, pic12
)
{
    this._id          = id;
    this._color       = color;
    this._form        = form;
    this._size        = size;
    this._quantity    = quantity;
    this._description = description;
    this._colorValue  = colorValue;
	this._fprice      = fprice;
	this._ftprice     = ftprice;
	this._pic1        = pic1;
	this._pic2        = pic2;
	this._pic3        = pic3;
	this._pic4        = pic4;
	this._pic5        = pic5;
	this._pic6        = pic6;
	this._pic7        = pic7;
	this._pic8        = pic8;
	this._pic9        = pic9;
	this._pic10       = pic10;
	this._pic11       = pic11;
	this._pic12       = pic12;
	this._bPicsGenerated = false;
	this._picList     = new Array();
	this._tagImage    = null;
	this._isStartZoom = false;
	this._tagImageList = null;
	this.bInitJcarousel = false;

	this.generatePics = function(){
		this._bPicsGenerated = true;
		if(this._pic1  == '' || !this._addNewPic(this._pic1 )) return;
		if(this._pic2  == '' || !this._addNewPic(this._pic2 )) return;
		if(this._pic3  == '' || !this._addNewPic(this._pic3 )) return;
		if(this._pic4  == '' || !this._addNewPic(this._pic4 )) return;
		if(this._pic5  == '' || !this._addNewPic(this._pic5 )) return;
		if(this._pic6  == '' || !this._addNewPic(this._pic6 )) return;
		if(this._pic7  == '' || !this._addNewPic(this._pic7 )) return;
		if(this._pic8  == '' || !this._addNewPic(this._pic8 )) return;
		if(this._pic9  == '' || !this._addNewPic(this._pic9 )) return;
		if(this._pic10 == '' || !this._addNewPic(this._pic10)) return;
		if(this._pic11 == '' || !this._addNewPic(this._pic11)) return;
		if(this._pic12 == '' || !this._addNewPic(this._pic12)) return;
	};

	this._addNewPic = function(pic){
		slPics = pic.split(/:::/);
		if(slPics.length<3)
			return false;
		this._picList[this._picList.length] = new variantPicture(slPics[0], slPics[1], slPics[2]);
		return true;
	}

	this._currentShowingPictureId =0;
	this.changeImage = function(pm){
		this._currentShowingPictureId += pm;

		if(this._currentShowingPictureId>=this._picList.length)
			this._currentShowingPictureId = 0;

		if(this._currentShowingPictureId<0)
			this._currentShowingPictureId = this._picList.length - 1;
		setZoomPicture(this._currentShowingPictureId, this._id , this._picList[this._currentShowingPictureId]._detail, this._picList[this._currentShowingPictureId]._zoom);
	};


	this.setImage = function () {

	    if (!this._bPicsGenerated)
	        this.generatePics();
	    if (this._picList.length < 1) return;


	    if (this._tagImage == null) {
	        this._tagImage =
				$("<a />").attr(
					{
					      href    : this._picList[0]._zoom
						, id      : 'zoom1'
                        , rel     : "adjustX: 5, adjustY:0"
                        , 'class' : "cloud-zoom"
					}
				)
			;

	        $(this._tagImage).html(
				$("<img />").attr(
					{
					      src     : this._picList[0]._detail
						, id      : "product_img"
						, 'class' : "photo"
					}
				)
			);

	    }

	    if (this._tagImage != null) {
	        //$('#sysImageDiv').html('');
	        $('#sysImageDiv').html($(this._tagImage));
	    }

	    





	    if (this._tagImageList == null) {
	        this._tagImageList =
                $("<ul />").attr({
                    id: "ulPDSPL"
                })
            ;

	        for (i = 0; i < this._picList.length; i++) {
	            var aTag =
                    $("<a />").attr({
                          'class' : "cloud-zoom-gallery"
                        , id      : "test_MorePics_" + +this._id
						, rel     : "useZoom: 'zoom1', smallImage: '" + this._picList[i]._detail + "'"
                        , href    : this._picList[i]._zoom
                    })
    			;

	            $("<img />").attr({
	                src: this._picList[i]._icon
	            }).appendTo(aTag);

	            $(this._tagImageList).append($("<li />").append($("<div />").append(aTag)));

	        }

	    }
	    //*
	    $("#ulPDSPL").html(
            this._tagImageList.html()
        );

	    $('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
	    // */

	    


	};
}


function changeZImage(pm){
	currentVariant = variants.getVariants(currentColor, currentSize, currentForm);
	currentVariant[0].changeImage(pm);
}


function setZoomPicture(_i, _id, _detailImage, _zoomImage){
	$('#jqzoom_' + _id).attr('href',_zoomImage);
	$('#jqzoom_' + _id + ' #product_img').attr('src',_detailImage);
	currentVariant = variants.getVariants(currentColor, currentSize, currentForm);
	currentVariant[0]._currentShowingPictureId = _i;
}

function variantList()
{
    this._items = new Array();
    
    this.add = function(
					  id
					, size
					, color
					, form
					, quantity
					, description
					, colorValue
					, fprice
					, ftprice
					, pic1
					, pic2
					, pic3
					, pic4
					, pic5
					, pic6
					, pic7
					, pic8
					, pic9
					, pic10
					, pic11
					, pic12
	)
    {
        this._items[this._items.length] = 
			new variant(
				  id
				, size
				, color
				, form
				, quantity
				, description
				, colorValue
				, fprice
				, ftprice
				, pic1
				, pic2
				, pic3
				, pic4
				, pic5
				, pic6
				, pic7
				, pic8
				, pic9
				, pic10
				, pic11
				, pic12
		);
    }
    
    this.getAll = function()
    {
        return this._items;
    }
    
    this.getWidthByColor = function(colorValue)
    {
        var _sizes = new Array();
        
        for(var i = 0;i < this._items.length;i++)
        {
            if(
				this._items[i]._color == colorValue
				&&
				this._items[i]._quantity>0

			)
            {
                var add = true;
                for(var j = 0;j < _sizes.length;j++)
                {
                    if(this._items[i]._size == _sizes[j])
                    {
                        add = false;
						continue;
                    }
                }
                
                if(add)
                {
                    _sizes[_sizes.length] = this._items[i]._size; 
                }
            }
        }

        return _sizes;
    }

	this.getFormsByColorAndSize = function(colorValue, size){
        var _forms = new Array();

        for(var i = 0;i < this._items.length;i++)
        {
            if(
				this._items[i]._color == colorValue
				&&
				this._items[i]._size == size

			)
            {
                var add = true;
                for(var j = 0;j < _forms.length;j++)
                {
                    if(this._items[i]._form == _forms[j])
                    {
                        add = false;
						continue;
                    }
                }
                
                if(add)
                {
                    _forms[_forms.length] = this._items[i]._form; 
                }
            }
        }

		return _forms;
	}

	this.getVariants = function(colorValue, size, form){
		var _variants = new Array();

        for(var i = 0;i < this._items.length;i++)
        {
            if(
				this._items[i]._color == colorValue
				&&
				this._items[i]._size == size
				&&
				this._items[i]._form == form
			)
                _variants[_variants.length] = this._items[i]; 
        }

		return _variants;
	}
}

function readCookie(name)
{
	name = $.trim(name);
	
	cookies = document.cookie;
	cookies = cookies.split(";");
	
	for(var i = 0;i < cookies.length;i++)
	{
		var zw = cookies[i].split("=");
		zw[0] = $.trim(zw[0]);
		zw[1] = $.trim(zw[1]);
		
		if(zw[0] == name)
		{
			return zw[1];
		}
	}
	return false;
}
            
