/* 6packer.js
 * 
 * (c) 2006-2009 Boland Werbung
 * 
 * Exchange slices of the start page banner with changing transitions 
 *
 * Version: 1.1
 *
 * 14.09.2009 - added Preloader (sixLoader())
 * 16.03.2009 - hyperlink broken
 * 23.11.2006 - added multilanguage and hyperlink support
 */

var packer_slice = new Array();
var packer_anchor = new Array();
var packer_lang = 'en';
var packer_link;
var packer_pack_active;
var packer_pack_next;
var packer_string;
var packer_transition = 0;
var packer_is_ie = document.all;
var packer_timelap_slide = 7000;
var packer_timelap_slice = 200;
var packer_timelap_fade = 50;
var sliceno;
//var packer_output;

function sixLoad() {
	// Function: Preloading of 6-pack tiles
	
	var img = new Array();
	var imgName = ''; // e.g. cbn
	var imgFullName = ''; // e.g. cbn_r1-c3.jpg
	var imgPath = '/images/frontbanner/'; // image folder
	var imgSuffix = '.jpg'; // the suffix
	var row = 1; // initialise row variable
	var col = 1; // initialise the column variable
	var num = 0;
	
	
	for (var i = 0; i < packer_pack[packer_lang].length; i++) {		
		// Fetch Image name form 6packerlist
		imgName = packer_pack[packer_lang][i]['img'];
		
		// for-Loops for image-part-names
		// 1st loop in row
		for (var r = 1; r <= 2; r++) { 
			// 2nd loop for column
			for (var c = 1; c <= 3; c++) {
				imgFullName = imgName + '_r' + r + '_c' + c + imgSuffix;
				
				// Create new Image-Object (that will be load automaticly)
				img[num] = new Image();
				img[num].src = imgPath + imgFullName;
				num++;
			}
		}
	}
	// Testing
	/*var dump = '';
	for (var t=0; t < img.length; t++) {
		dump = dump + img[t].src + "\n";
	}
	alert(dump);
	*/
}


function BW_6packer_init(lang) {
		
	packer_pack_active = 0;
	packer_link = FIND("6packlink")
	packer_lang = lang;
	
	for (var i=0;i<6;i++) {
		packer_slice[i] = FIND("slice" + i);
		packer_anchor[i] = FIND("anchor" + i);
	}
	
	//packer_output=window.open("","hello","");
	
	if (!packer_is_ie) {
		for (var i=6;i<12;i++) {
			packer_slice[i] = FIND("slice" + i);
			// packer_slice[i].style.backgroundImage = packer_slice[i-6].src;
			packer_slice[i].style.backgroundImage = 'url('+packer_slice[i-6].src+')';
			// alert(packer_slice[i-6]);
		}
	}
	
	// Testing
		/*
		var dump = '';
		for (var t = 0; t < packer_slice.length; t++) {
			dump = dump + packer_slice[i].src + "\n";
		}
		alert(dump);
		*/
	
	setTimeout('BW_6packer_handler()', packer_timelap_slide); // 7000
}



function BW_6packer_handler() {
	if (!packer_is_ie)	// copy background to front, which will later fade to the new images
		for (var i=0; i<6; i++) {
			// packer_slice[i].src = packer_slice[i+6].style.backgroundImage; /////.match(/http.*jpg/);
			packer_slice[i].src = packer_slice[i+6].style.backgroundImage.match(/\/http.*jpg/);
		}
	packer_pack_next = packer_pack_active+1;
	packer_transition++; 
	if (packer_transition >= 7)
		packer_transition = 0;
	//packer_output.document.write("Transition " + packer_transition + ": ");
		
	if (packer_pack_next >= packer_pack[packer_lang].length)
		packer_pack_next=0;
	
	if (!packer_is_ie)
		for (var i=0;i<6;i++) {
			packer_slice[(i+6)].style.backgroundImage = 'url('+packer_path + packer_pack[packer_lang][packer_pack_active]['img'] + "_r" + (Math.floor(i / 3)+1) + "_c" + ((i % 3)+1) + '.jpg)';
		}
  		//for (i=0;i<6;i++) packer_slice[i+6].background=packer_path + packer_pack[packer_lang][packer_pack_next]['img'] + + "_r" + (Math.floor(i / 3)+1) + "_c" + ((i % 3)+1) + ".jpg";

	setTimeout('BW_6packer_transition(0)', packer_timelap_slice);
}

function BW_6packer_transition(phase) {
	
	switch(packer_transition) {
		case 0:  // counter-clockwise
		    sliceno = (phase>2)?(phase-3):(5-phase);
			break;
		case 1:  // left-to-right line-by-line
		    sliceno = phase;
			break;
		case 2:  // tile
		    sliceno = (phase<3)?(phase*2):(phase-3)*2+1;
			break;
		case 3: // right-to-left line-by-line
		    sliceno = 5-phase;
			break;
		case 4:  // left-to-right col-by-col
			sliceno = (phase % 2 ==0)?(Math.floor(phase/2)):(Math.floor(phase/2)+3)
			break;
		case 5:  // tile2
			sliceno = (phase<3)?(phase*2+1):(phase-3)*2;
			break;
		case 6: // right-to-left col-by-col
			sliceno = (phase % 2 ==0)?(Math.floor(phase/2)+3):(Math.floor(phase/2));
			break;
	}
	//packer_output.document.write(sliceno + " ");

	if (packer_is_ie) {
		packer_slice[sliceno].filters.blendTrans.Apply();
		packer_slice[sliceno].src = packer_path + packer_pack[packer_lang][packer_pack_next]['img'] + "_r" + (Math.floor(sliceno / 3)+1) + "_c" + ((sliceno % 3)+1) + ".jpg";
		packer_anchor[sliceno].href = packer_pack[packer_lang][packer_pack_next]['href'];
		//packer_link.href=packer_pack[packer_lang][packer_pack_next]['href'];
	} else {
		packer_slice[sliceno+6].style.backgroundImage = 'url(' + packer_path + packer_pack[packer_lang][packer_pack_next]['img'] + "_r" + (Math.floor(sliceno / 3)+1) + "_c" + ((sliceno % 3)+1) + '.jpg)';
		packer_slice[sliceno].src = packer_path + packer_pack[packer_lang][packer_pack_active]['img'] + "_r" + (Math.floor(sliceno / 3)+1) + "_c" + ((sliceno % 3)+1) + '.jpg';
		packer_anchor[sliceno].href = packer_pack[packer_lang][packer_pack_next]['href'];
	}

	
	if (packer_is_ie)
		packer_slice[sliceno].filters.blendTrans.Play();
	else
		setTimeout('BW_6packer_mozblend('+sliceno+',0.95)', packer_timelap_fade); 

	if (phase<5)
		setTimeout('BW_6packer_transition('+(phase+1)+')', packer_timelap_slice);
	else
	{
		//packer_output.document.write("<br />");
		
		packer_pack_active = packer_pack_next;
		setTimeout('BW_6packer_handler()', packer_timelap_slide);
	}
}

function BW_6packer_mozblend(img, state) {
	// document.getElementById('sixpackOpacity').value = (Math.round(state*100)/100); /* d */ /* testing */
	state = (Math.round(state*100)/100);
	packer_slice[img].style.opacity = state;
	if (state>0)
		setTimeout('BW_6packer_mozblend('+img+','+(state-0.05)+')', packer_timelap_fade);
	else {
		packer_slice[img].src = packer_path + packer_pack[packer_lang][packer_pack_next]['img'] + "_r" + (Math.floor(img / 3)+1) + "_c" + ((img % 3)+1) + ".jpg";
	//	packer_slice[img].src = packer_slice[img+6].style.backgroundImage.match(/\/http.*jpg/);
		packer_slice[img].style.opacity = '1';
	}
}
	
function FIND(item) {
	if (document.all) return(document.all[item]);
	if (document.getElementById) return(document.getElementById(item));
	return(false);
}
