/* 
Smiths Optical (version 2.0)
Designed and Created by LaMa Media (www.LaMaMedia.com)
Copyright (c) 2006 - 2011. All rights reserved.
*/
/*global document, setTimeout */

var kDELIM = '|',
	vBrand = { num:0, cur:0, opacity:0, TIME:2000, SPEED:25, DELTA:5 },
	vBrands = [
	'perreira|Barton Perreira|http://bartonperreira.com/',
	'tiffany|Tiffany &amp; Co.|http://www.tiffany.com/',
	'calvin|Calvin Klein|www.calvinklein.com/',
	'coach|Coach|http://www.coach.com/',
	'colors|Colors in Optics|http://www.colorsinoptics.com/',
	'fendi|Fendi|http://www.fendi.com/',
	'ford|Tom Ford|http://www.tomford.com/',
	'armani|Giorgio Armani|http://www.armani.com/',
	'burch|Tory Burch|http://www.toryburch.com/',
	'gucci|Gucci|http://www.gucci.com/',
//	'juicy|Juicy Couture|http://www.juicycouture.com/',
	'kors|Michael Kors|http://www.michaelkors.com/',
//	'dvf|Diane von Furstenberg|http://www.dvf.com/',
//	'lagerfeld|Karl Lagerfeld|http://www.karllagerfeld.com',
	'lafont|Lafont|http://www.lafont.com/',
	'legre|Legre|http://www.legre.com/',
	'lindberg|Lindberg|http://www.lindberg.com/',
//	'minima|Minima/T2|http://www.minima.fr/',
//	'modo|Modo|http://www.modo.com/',
//	'montblanc|Montblanc|http://www.montblanc.com/',
	'peoples|Oliver Peoples|http://www.oliverpeoples.com/',
	'carrera|Porche Design / Carrera|http://www.carreraworld.com/',
	'prada|Prada|http://www.prada.com/',
//	'silhouette|Silhouette|http://www.silhouette.com/',
	'salt|SALT.|http://www.saltoptics.com',
	'smith|Paul Smith|http://www.paulsmithspectacles.com/'];


function fEmailLink(rID, rName, rDomain, rDisplay) {
	var tAddress = rName + String.fromCharCode(64) + rDomain,
		tDisplay = (typeof(rDisplay) === 'undefined') ? tAddress : rDisplay,
		tLink = '<a href="mailto:' + tAddress + '">' + tDisplay + '</a>';
	document.getElementById(rID).innerHTML = tLink;
}

function fGetItem(rStr, rNum, rDelim) {
	var tCount, tPos, tItem;
	for (tCount = 0; tCount <= rNum; tCount++) {
		tPos = rStr.indexOf(rDelim);
		if (tPos < 0) { tPos = rStr.length; }
		tItem = rStr.substring(0, tPos);
		tPos = tItem.length + 1;
		rStr = rStr.substring(tPos, rStr.length);
	}
	return (tItem);
}

function fOpacity(rID, rValue) {
    var tStyle = document.getElementById(rID).style,
		tValue = (rValue / 100);
    tStyle.opacity = tValue; 
    tStyle.MozOpacity = tValue; 
    tStyle.KhtmlOpacity = tValue; 
    tStyle.filter = 'alpha(opacity=' + rValue + ')'; 
} 

function fBrandOpacity(rNum, rValue) {
	fOpacity(('p' + rNum), rValue);
	fOpacity(('l' + rNum), rValue);
}

function fBrandNext() {
	document.getElementById('p' + vBrand.cur).style.display = 'none';
	document.getElementById('l' + vBrand.cur).style.display = 'none';
	++vBrand.cur;
	if (vBrand.cur >= vBrand.num) {
		vBrand.cur = 0;
	}
	fBrandOpacity(vBrand.cur, 100);
	document.getElementById('p' + vBrand.cur).style.display = 'block';
	document.getElementById('l' + vBrand.cur).style.display = 'block';
	fBrandShow();
}

function fBrandHide() {
	var tNext = 0;
	vBrand.opacity -= vBrand.DELTA;
	if (vBrand.opacity <= 0) {
		vBrand.opacity = 0;
		tNext = 1;
	}
	fBrandOpacity(vBrand.cur, vBrand.opacity);
	if (tNext) {
		fBrandNext();
	} else {
		setTimeout(fBrandHide, vBrand.SPEED);
	}
}

function fBrandShow() {
	vBrand.opacity += vBrand.DELTA;
	if (vBrand.opacity >= 100) {
		vBrand.opacity = 100;
		setTimeout(fBrandHide, vBrand.TIME);
	} else {
		setTimeout(fBrandShow, vBrand.SPEED);
	}
	fBrandOpacity(vBrand.cur, vBrand.opacity);
}

function fBrands() {
	var t, tBrand, tFile, tName, tURL, tHalf,
		tPhotos = '', tLeft = '', tRight = '', tLogos = '';
	vBrand.num = vBrands.length;
	vBrand.cur = vBrand.num - 1;
	tHalf = parseInt(((vBrand.num / 2) + .5), 10);
	for (t = 0; t < vBrand.num; t++) {
		tBrand = vBrands[t];
		tFile = fGetItem(tBrand, 0, kDELIM);
		tName = fGetItem(tBrand, 1, kDELIM);
		tURL = fGetItem(tBrand, 2, kDELIM);
		tPhotos += '<div class="photo" id="p' + t + '"><img src="media/photos/' + tFile + '.jpg" width="200" height="150" alt="' + tName + '" /></div>';
		tLogos += '<div class="logo" id="l' + t + '"><img src="media/logos/' + tFile + '.jpg" width="200" height="90" alt="' + tName + '" /></div>';
		if (t) { // DON'T SHOW FIRST BRAND IN LIST
			if (t < tHalf) {
				tLeft += '<div class="brand"><a href="' + tURL + '" target="linkwin">' + tName + '</a></div>';
			} else {
				tRight += '<div class="brand"><a href="' + tURL + '" target="linkwin">' + tName + '</a></div>';
			}
		}
	}
	document.getElementById('photos').innerHTML = tPhotos;
	document.getElementById('left').innerHTML = tLeft;
	document.getElementById('right').innerHTML = tRight;
	document.getElementById('logos').innerHTML = tLogos;
	fBrandNext();
}

