// -------------------------------------------------------------------
//  site.js
//  $Id$
//  
//  (c) 2007 paperium gmbh, vienna, autria
//  initial author: karl pitrich
// -------------------------------------------------------------------

var timer;
var slideNr = 0;
var slides;
var subPageNr = 0;
var subPages;
var subPageMode = 'manual';

function initSlideshow() {
	// start home-slideshow
	slideNr = 0;
	slides = $$('.slide');
	
	// Create slideshow navigation
	for (i=0; i<slides.length; i++) {
		link = new Element('a');
		if (i==0) {
			link.addClass('active');
		}
		link.setText(i+1);
		link.nr = i;
		link.addEvent('click', function(e) {
			gotoSlide(this.nr);
		});
		link.injectInside('slideNavi');
		
		slides[i].link = link;
	}
	
	timer = window.setTimeout("nextSlide()", 4000);
}

function initSubPageNavigation() {
	// Create JS page navigation
	subPageNr = 0;
	subPages = $$('.subPage');
	
	for (i=0; i<subPages.length; i++) {
		var link = new Element('li');
		if (i==0) {
			link.addClass('active');
		} else {
			subPages[i].setStyle('display','none');
		}
		link.nr = i;
		link.setText(subPages[i].getAttribute('title'));
		link.addEvent('click', function(e) {
			showSubPage(this.nr);
		});
		if (subPageMode=='auto') {
			link.injectInside('subPageNavigation');
		}
		subPages[i].link = link;
	}
	
	// set all "prev"-Links to class "inactive"
	$$('.subPagePrev').each(function(el) {
		el.addClass("inactive");
	});
}

function showSubPage(nr) {
	if (nr != subPageNr) {
		subPages[subPageNr].link.removeClass('active');
		subPages[subPageNr].setStyle('display','none');
		subPageNr = nr;
		if (subPageNr>=subPages.length) {
			subPageNr = 0;
		}
		subPages[subPageNr].link.addClass('active');
		subPages[subPageNr].setStyle('display','block');
		
		// change prev/next Classes
		if (subPageNr == 0) {
			$$('.subPagePrev').each(function(el) {
				el.addClass("inactive");
			});
		} else {
			$$('.subPagePrev').each(function(el) {
				el.removeClass("inactive");
			});
		}
		// change prev/next Classes
		if (subPageNr == (subPages.length-1)) {
			$$('.subPageNext').each(function(el) {
				el.addClass("inactive");
			});
		} else {
			$$('.subPageNext').each(function(el) {
				el.removeClass("inactive");
			});
		}
	}
}

function nextSubPage() {
	nr = subPageNr+1;
	if (nr>=subPages.length) {
		nr = subPages.length-1;
	}
	showSubPage(nr);
}

function prevSubPage() {
	nr = subPageNr-1;
	if (nr<0) {
		nr = 0;
	}
	showSubPage(nr);
}
	
function nextSlide() {
	showSlide(slideNr+1);
	timer = window.setTimeout("nextSlide()", 4000);
}

function gotoSlide(nr) {
	if (nr != slideNr) {
		window.clearTimeout(timer);
		showSlide(nr);
		timer = window.setTimeout("nextSlide()", 10000);
	}
}

function showSlide(nr) {
	if (nr != slideNr) {
		if(slides[slideNr].fx) {slides[slideNr].fx.stop();}
		slides[slideNr].fx = slides[slideNr].effect('opacity', {duration: 1000}).start(0);
		slides[slideNr].link.removeClass('active');
		
		slideNr = nr;
		if (slideNr>=slides.length) {
			slideNr = 0;
		}

		if(slides[slideNr].fx) {slides[slideNr].fx.stop();}
		slides[slideNr].setOpacity(0);
		slides[slideNr].fx = slides[slideNr].effect('opacity', {duration: 1000}).start(1);
		slides[slideNr].link.addClass('active');
	}
}

// -------------------------------------------------------------------

function fade(id, start, end) {	
	$(id).setOpacity(start/100);
	if ($(id).fx) {$(id).fx.stop();}
	$(id).fx = $(id).effect('opacity', {duration: 1000}).start(end/100);
}



/*
 * ZenMagick - Extensions for zen-cart
 * Copyright (C) 2006,2007 ZenMagick
 *
 * Portions Copyright (c) 2003 The zen-cart developers
 * Portions Copyright (c) 2003 osCommerce
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * $Id: common.js,v 1.5 2007/03/30 08:42:16 radebatz Exp $
 */
 
 
 

// focus on particular fomr element
function focus($id) {
    if (document.getElementById) {
        var elem = document.getElementById($id);
        if (elem) elem.focus();
    }
}

// new window (alternative to target="_blank")
function newWin(link) {
  var win = window.open(link.href);
  if (win && win.focus) {
    win.focus();
  }
}

// zen-cart popups
function zcPopupWindow(url, name) {
  var win = window.open(url,name,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=450,height=320,screenX=150,screenY=150,top=150,left=150');
  if (win && win.focus) {
    win.focus();
  }
}
function popupWindow(url) { zcPopupWindow(url, 'popupWindow'); }
function couponpopupWindow(url) { zcPopupWindow(url, 'couponpopupWindow'); }
function submitFunction(gv, total) { if ((gv.value && gv.value >= total) || gv >= total) { submitter = 1; } }


// product image popup
function productPopup(e, parent) {
  if (e.preventDefault) e.preventDefault();
  var win = window.open("","productImageWindow","height=206,width=246");
  if (!win) return;
  win.document.write('<!DOCTYPE html><html>'
    + '<title>' + document.getElementsByTagName('title')[0].innerHTML + '</title>'
    + '<style type="text/css">html,body,p{margin:0;padding:0}body{color:#6b6b6b}p{margin:8px;font:70% sans-serif;text-align:center}a{color:#125991}</style>'
    + '<p><img id="theimg" src="' + parent.href + '" height="160" width="200" alt="">'
    + '<p>[ <a href="#" onclick="javascript:window.close()">Close Window</a> ]</html>');
  win.document.close();
  win.focus();
}

// handle click on empty category (if configured)
function catclick(link) {
    var siblings = link.parentNode.childNodes;
    // find ul sibling
    for (var ii=0; ii < siblings.length; ++ii) {
        if ('UL' == siblings[ii].nodeName) {
            var ul = siblings[ii];
            if ("none" == ul.style.display || ("" == ul.style.display && -1 == ul.className.indexOf('act'))) {
                ul.style.display = "block";
                ul.style.visibility = "visible";
            } else {
                ul.style.display = "none";
                ul.style.visibility = "hidden";
            }
            return false
        }
    }
    return true;
}






/* SCRIPT VON THOMAS */
function showIcon(nr) {
	document.getElementById('icon'+nr).style.display = 'block';
}
function hideIcon(nr) {
	document.getElementById('icon'+nr).style.display = 'none';
}

function showcat(cid) {
	if (cid != actcat) {
		document.getElementById('catlink'+actcat).className = '';
		document.getElementById('catlink'+cid).className = 'act';
		document.getElementById('catlink'+cid).blur();
		fade("cat" + actcat, 100, 0);
		fade("cat" + cid, 0, 100);
		actcat = cid;
	}
}

function addToCart(id) {
	hideLightbox();
	$('sbCart').setHTML('<div class="loading"></div>');
	var myAjax = $('ajaxCartForm'+id).send({method: 'post', update: 'sbCart'});
}


/* Functions for registration process */
function regInputNext(el,nextel) {
	el.value = el.value.toUpperCase();
	maxlen = el.getAttribute('maxlength');
	if (el.value.length == maxlen && nextel!='') {
		document.getElementById(nextel).focus();
	}
}
function checkReg() {
	submitok = true;
	var hexPattern4 = /^[0-9a-f]{4}$/i;
	var snrPattern3 = /^[0-9a-z]{3}$/i;
	var snrPattern2 = /^[0-9a-z]{2}$/i;
	for (i = 1; i < 5; i++) {
		value = document.getElementById('pkey'+i).value;
		ok = false;
		if (value != '') {
			ok = hexPattern4.test(value);
		}
	
		if (ok) {
			$('pkey'+i).removeClass('error');
		}
		else {
			submitok = false;
			$('pkey'+i).addClass('error');
		}
	}
	if (submitok) {
		$('regResult').setHTML('<div class="loading-registration"></div>');
		var myAjax = $('regForm').send({method: 'post', update: 'regResult'});
	} 
	else {
		$('regResult').setHTML('<p>Please check your input values!</p>');
	}
}


/* Dummy function, used at payment screen */
function methodSelect(method) {
}
