
// global variables for product-config
var zone3_productconfig_text = '';
var zone3_productconfig_font = '';
var zone3_productconfig_color = '';
var zone3_productconfig_ownfont_typed = 0;


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}
function in_array (needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}
function explode (delimiter, string, limit) {
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||
        typeof arguments[1] == 'undefined' ) {
        return null;
    }
 
    if ( delimiter === '' ||
        delimiter === false ||
        delimiter === null ) {
        return false;
    }
 
    if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' ) {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}



//zoombilder fuer

function zone3_swapImages(mainpic,zoompic,element) {
	
	if (element.getAttribute('id')=='MorePics_1') {
		jQuery('#zone3livepreview').css('display','block');
	} else {
		jQuery('#zone3livepreview').css('display','none');
	}
	
	
	
	var img = jQuery('#product_img');
	img.attr('src',mainpic);
	var a = img.parent();
	
	
	
		
	
	if (zoompic!="") {
		a.attr('href',zoompic);
		a.addClass('top_up');
		jQuery('.zoomlink').css('display','block');
		
		jQuery(".zoomlink a").addClass('top_up');
		jQuery(".zoomlink a").attr('href', zoompic);
		
	} else {
		a.attr('href','javascript:;');
		a.removeClass('top_up');
		jQuery('.zoomlink').css('display','none');
	}
}


var oldonclick = jQuery('#toBasket').attr('onclick');
function zone3_sanitycheck(type) {
	if (type=='standard') {
		
	
		jQuery('#toBasket').css('opacity','1');
		jQuery('#toBasket').removeAttr('disabled');
				
	} else {
		//custom slogan
		if (zone3_productconfig_text!='' && zone3_productconfig_font!='' && zone3_productconfig_color!='') {
			jQuery('#toBasket').css('opacity','1');
			jQuery('#toBasket').removeAttr('disabled');		
		} else {
			//deactivate warenkorb button
			jQuery('#toBasket').css('opacity','0.5');
			jQuery('#toBasket').attr('disabled','disabled');
		}
	}
}

jQuery(document).ready(function(){

		
	// Klassen fuer vertikale Linien - Metallicbox Startseite
	jQuery(".startpage .action .product:nth-child(1)").addClass('vert-line-1'); 
	jQuery(".startpage .action .product:nth-child(2)").addClass('vert-line-2'); 
	jQuery(".startpage .action .product:nth-child(3)").addClass('vert-line-3'); 


	// Aktiv Klasse fuer Seitenzahl (Kategorie Artikeluebersicht)
	if (jQuery('a#PageNrTop_1').size()>0){
		var activesite = jQuery("#listXofY_Top").html().split(' '); 
		jQuery('a#PageNrTop_'+activesite[1]).addClass('active');
		jQuery('a#PageNrBottom_'+activesite[1]).addClass('active');
	}


	// Funktion Dropdown
	
	if (jQuery('div.dropdown-wrapper').size()>0){
		
		
		// Dropdown CSS-Declarations
		
		jQuery('div#variant-container').css({ 
				'position' : 'relative',
				'min-height' : '85px',
				'z-index' : '99'
		});
		
		jQuery('div#defaultslogan-container').css({ 
				'position' : 'relative',
				'min-height' : '70px',
				'z-index' : '98'
		});
		
		jQuery('div#fontcolor-container').css({ 
				'position' : 'relative',
				'min-height' : '70px',
				'z-index' : '97'
		});
		
		jQuery('div#fontfamily-container').css({ 
				'position' : 'relative',
				'min-height' : '70px',
				'z-index' : '96'
		});		
		
		jQuery('div.dropdown-wrapper').css({ 
				'position' : 'absolute',
				'z-index' : '99'
		});		
		
		//##############
		
		jQuery("div.dropdown-wrapper div.variant-list").hide();
		
		jQuery("div.dropdown-wrapper .active-variant").append('<a class="show-variants" href="#" title="Zeige Varianten">Zeige Varianten</a>');
		
		
		jQuery('div.dropdown-wrapper .active-variant .show-variants').click(function(){
			
			if (jQuery(this).parent().parent().find('div.variant-list').is(":hidden")) {
				jQuery('div.dropdown-wrapper div.dropdown-body div.variant-list').slideUp(500);
				jQuery(this).parent().parent().find('div.variant-list').slideDown(500);
				
				jQuery('div.dropdown-wrapper .active-variant .show-variants').removeClass('active');
				jQuery(this).addClass('active');
				jQuery('div.dropdown-wrapper .active-variant .show-variants').attr('title','Schließe Varianten');
			}
			
			else {
				jQuery('div.dropdown-wrapper div.dropdown-body div.variant-list').slideUp(500);
				jQuery(this).removeClass('active');
				jQuery('div.dropdown-wrapper .active-variant .show-variants').attr('title','Zeige Varianten');
			}
			jQuery(this).trigger('blur');
			
	      	return false;
		});
	}
	

	// Dropdown Liste MouseOver
	
		jQuery(".variant-list .product").hover(
		function () {
			jQuery(this).css({ 
				// 'background-color' : '#5a5a5a'
				'background' : '#5a5a5a url(/out/zone3/src/bg/arrow_13_white_left.gif) 230px center no-repeat'
			});
		}, function () {
			jQuery(this).css({ 
				// 'background-color' : '#505050'
				'background' : 'none'
			});
	});
	
	jQuery(".variant-list .variant").hover(
		function () {
			jQuery(this).addClass('varianthover');
		}, function () {
			jQuery(this).removeClass('varianthover');
	});

	// Dropdown Liste - Ganze flaeche verlinkt
	
	jQuery(".variant-list .product").click(function(){
		
			var variantlink = jQuery(this).find('p a').attr('href');
	
			//jQuery('.product-details .pics p.picture').prepend('<div class="variantloader"></div>');
	
			/*jQuery('.product-details .variantloader').css({ 
				'opacity' : '0.3'
			});*/
			
			/*
			setTimeout(function(){
				window.location = variantlink;
			},2000);
			*/			
			jQuery(this).trigger('blur');
			return true;
			
			return false;
	});

	
	// Hide Config options
	jQuery('#fontstyle-wrapper').hide();
		
	// Produktkonfigurator "wahle einen spruch"
	
	jQuery('.slogan-addition').css('display','none');
	
	var defaulttext = jQuery('#defaultslogan-container .active-variant p:eq(0)').html();
	jQuery('#defaultslogan-container .variant-list .slogan').click(function(){
		jQuery('#zone3livepreview').remove();
		
		var variantbox = jQuery(this).parent().parent();
	
		var box = jQuery('.persparam input:eq(0)');
		
		if (jQuery(this).find('p.no-slogan').size()>0){
			var sloganvalue = '';
			var newhtml = defaulttext;
			
			jQuery('.slogan-addition').hide();

			
		} else {
			var sloganvalue = jQuery(this).find('img').attr('rel');
			
			// live preview
			if (typeof(previews)!='undefined') {
			var image = str_replace('_','-',sloganvalue);
			image = str_replace('.gif','',image);
			var bits = explode('-',image);
			var preview = 'prsnlz_'+currentOXID+'_'+bits[1]+'_'+bits[3]+'.png';
			if (in_array(preview,previews)){
				jQuery('#zone3livepreview').remove();
				jQuery('#MorePics_1').trigger('click');
				
				jQuery('#product_img').after('<div id="zone3livepreview" style="position:absolute;top:0;left:0;display:block;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/out/pictures/zone3custom/_previews/'+preview+'\');"><img style="filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" src="/out/pictures/zone3custom/_previews/'+preview+'" border="0" alt=""></div>');
				
			
				
			} else {
				jQuery('#zone3livepreview').remove();
			}
			}
			
			var newhtml = jQuery(this).html();
			jQuery('#fontstyle-wrapper').slideUp('slow');
			
			zone3_productconfig_text = '';

		}
		
		
		
		box.attr('value',sloganvalue);		
		jQuery('div.dropdown-wrapper div.dropdown-body div.variant-list').slideUp(500);
		jQuery('div.dropdown-wrapper .active-variant .show-variants').removeClass('active');
		jQuery('div.dropdown-wrapper .active-variant .show-variants').attr('title','Zeige Varianten');
		
		// selectbox auf 1 setzen
		
		if (jQuery(this).find('p.no-slogan').size()>0){
			jQuery('.artvariants select:eq(0)').attr('value',0);
		} else {
			jQuery('.artvariants select:eq(0)').attr('value',1);
			jQuery('.slogan-addition').show();
			jQuery('#personalSlogan').attr('value','');
		}		
		
		variantbox.find('.active-variant p:eq(0)').html(newhtml);
		
	//	jQuery('#fontcolor-container .active-variant p:eq(0)').html(defaultcolor);
	//	jQuery('#fontfamily-container .active-variant p:eq(0)').html(defaultfont);
		
		zone3_sanitycheck('standard');
	});
	
	
	// textslogan
	jQuery('#personalSlogan').click(function(){
		if (jQuery('#fontstyle-wrapper').css('opacity')==0) {
			zone3_sanitycheck('standard');
		} else {
			if (!jQuery(this).hasClass('cleared')) {
				jQuery(this).attr('value','');
				jQuery(this).trigger('keyup');
				jQuery(this).addClass('cleared');
			}
			
		}
		
		
		
		
	});
	
	jQuery('#personalSlogan').keyup(function(){
		jQuery('#defaultslogan-container p.no-slogan').parent().trigger('click');
		var box = jQuery('.persparam input:eq(0)');

		var currentvalue = jQuery(this).attr('value');
		zone3_productconfig_text = currentvalue;
		
		if (trim(currentvalue)!="") {
			newvalue = trim(zone3_productconfig_text + ' ' + zone3_productconfig_color + ' ' + zone3_productconfig_font);
			
			jQuery('#fontstyle-wrapper').slideDown('slow');
			jQuery('.slogan-addition').show();
			
			zone3_sanitycheck('custom');
		} else {
			newvalue = '';
			jQuery('#fontstyle-wrapper').slideUp('slow');
			jQuery('.slogan-addition').fadeOut('slow');
			zone3_sanitycheck('standard');
		}
				
		if (trim(newvalue)!="") {
			jQuery('.artvariants select:eq(0)').attr('value',1);
			box.attr('value',newvalue);
		}
		
	});
	
	//textcolor
	var defaultcolor = jQuery('#fontcolor-container .active-variant p:eq(0)').html();
	jQuery('#fontcolor-container .variant-list p').click(function(){
		//jQuery('#defaultslogan-container p.no-slogan').parent().trigger('click');
		var variantbox = jQuery(this).parent().parent().parent();
		var box = jQuery('.persparam input:eq(0)');

		var colorvalue = jQuery(this).html();		
		jQuery('div.dropdown-wrapper div.dropdown-body div.variant-list').slideUp(500);
		jQuery('div.dropdown-wrapper .active-variant .show-variants').removeClass('active');
		jQuery('div.dropdown-wrapper .active-variant .show-variants').attr('title','Zeige Varianten');	
		
		zone3_productconfig_color= colorvalue;
		
		newvalue = trim(zone3_productconfig_text + ' ' + zone3_productconfig_color + ' ' + zone3_productconfig_font);
		
		box.attr('value',newvalue);
		variantbox.find('.active-variant p:eq(0)').html(jQuery(this).html());
		zone3_sanitycheck('custom');
		
		jQuery(this).parent().parent().find(':hidden').css('display','block');
		jQuery(this).parent().css('display','none');
	});
	
	
	//textfont
	var defaultfont = jQuery('#fontcolor-container .active-variant p:eq(0)').html();
	jQuery('#fontfamily-container .variant-list p').click(function(){
		//jQuery('#defaultslogan-container p.no-slogan').parent().trigger('click');
		var variantbox = jQuery(this).parent().parent().parent();
		var box = jQuery('.persparam input:eq(0)');

		var fontvalue = jQuery(this).html();	
		jQuery('div.dropdown-wrapper div.dropdown-body div.variant-list').slideUp(500);
		jQuery('div.dropdown-wrapper .active-variant .show-variants').removeClass('active');
		jQuery('div.dropdown-wrapper .active-variant .show-variants').attr('title','Zeige Varianten');
				
		zone3_productconfig_font= fontvalue;
		
		newvalue = trim(zone3_productconfig_text + ' ' + zone3_productconfig_color + ' ' + zone3_productconfig_font);
		
		box.attr('value',newvalue);
		variantbox.find('.active-variant p:eq(0)').html(jQuery(this).html());
		zone3_sanitycheck('custom');
		
		
		jQuery(this).parent().parent().find(':hidden').css('display','block');
		jQuery(this).parent().css('display','none');
	});
	jQuery('#fontfamily-container input[name="specialfont"]').click(function(){
		zone3_productconfig_font = '';
		setTimeout(function(){
			zone3_productconfig_font = '';	
			zone3_productconfig_ownfont_typed = 0;						
			jQuery('input[name="specialfont"]:eq(0)').keydown(function(){
				if (zone3_productconfig_ownfont_typed==0) {
					zone3_productconfig_ownfont_typed = 1;
					jQuery(this).attr('value','');
				}
			});
			
			jQuery('input[name="specialfont"]:eq(0)').trigger('focus').removeAttr('readonly').keyup(function(){
				zone3_productconfig_font= jQuery(this).attr('value');
				var box = jQuery('.persparam input:eq(0)');
				newvalue = trim(zone3_productconfig_text + ' ' + zone3_productconfig_color + ' ' + zone3_productconfig_font);
				box.attr('value',newvalue);
				zone3_sanitycheck('custom');
			}).click(function(){
				zone3_productconfig_ownfont_typed = 0;
				zone3_productconfig_font= '';
				jQuery(this).attr('value','');
			});
			zone3_sanitycheck('custom');
		},100);
	});
	
	
	
	// verlinkung zoombutton
	
		var thiszoompic = jQuery('p.picture a.top_up').attr('href');
		jQuery(".zoomlink a").addClass('top_up');
		jQuery(".zoomlink a").attr('href', thiszoompic);
	
	
	// Produktbild - entferne verlinkung falls keine großansicht vorhanden ist
	
	jQuery('div.morepics a').click(function(){

		if (jQuery('p.picture a').attr('href')=='javascript:;'){
			
			jQuery('p.picture a').removeAttr('title');
			jQuery('p.picture a').css("cursor","default");
			
		}else{
			jQuery('p.picture a').attr('title','Anzeige vergößern');
			jQuery('p.picture a').css("cursor","pointer");
		}	
	
	});
	
	
	// Aktivzustand fuer Infonavi und siteinfo
	
	var activeUrl = document.URL;
	
	jQuery('.infonav ul li a').each(function(){
		
		linkUrl = jQuery(this).attr('href');
	
		if (linkUrl == activeUrl){
			jQuery(this).parent().addClass('active');
		}
	
	});
	
	
	jQuery('#siteinfo ul li a').each(function(){
		
		linkUrl = jQuery(this).attr('href');
	
		if (linkUrl == activeUrl){
			jQuery(this).parent().addClass('active');
		}
	
	});	
		

	//startseite gallery
	jQuery('#startpage-img').nivoSlider({
		effect:'random',
		slices:15,
		animSpeed:1000,
		pauseTime:5000,
		directionNav:false, //Next & Prev
		directionNavHide:false, //Only show on hover
		controlNav:false, //1,2,3...
		pauseOnHover:false, //Stop animation while hovering
		beforeChange: function(){},
		afterChange: function(){}
	});




});
