var fields = new Array();
var bullet = 0;
if (typeof(view)=='undefined') view = '';
if (typeof(page)=='undefined') page = '';
if (typeof(catStyle)=='undefined') catStyle = '';
if (typeof(sortBy)=='undefined') sortBy = '';
if (typeof(search)=='undefined') search = '';
if (typeof(currcat)=='undefined') currcat = '';
function processXMLFiles() { processXMLSetup(); }

function processXMLSetup() {
	$.get('setup.xml', {}, function(xml){
		fields['addtocart'] = $("CART",xml).text();
		fields['viewcart'] = $("VIEWCART",xml).text();
		fields['cuname'] = $("CUNAME",xml).text();
		fields['cuadd1'] = $("CUADD1",xml).text();
		fields['cuadd2'] = $("CUADD2",xml).text();
		fields['cuphone'] = $("CUPHONE",xml).text();
		fields['cuemail'] = $("CUEMAIL",xml).text();
		fields['cuemailform'] = $("CUEMAILFORM",xml).text();
		fields['cufax'] = $("CUFAX",xml).text();
		fields['footer'] = $("FOOTER",xml).text();
		fields['home'] = $("HOME",xml).text();
		fields['logo'] = $("IMGLOGO",xml).text();
		fields['header'] = $("IMGHEADER",xml).text();
		fields['background'] = $("IMGBACKGROUND",xml).text();
		fields['index'] = $("INDEX",xml).text();
		fields['metat'] = $("METAT",xml).text();
		fields['metak'] = $("METAK",xml).text();
		fields['metad'] = $("METAD",xml).text();
		fields['zoomimages'] = $("ZOOMIMAGES",xml).text();
		fields['denom'] = $('DENOM',xml).text();
		fields['headertxt'] = $('HEADERTXT',xml).text();
		fields['slogan'] = $('SLOGAN',xml).text();
		fields['labelcontactus'] = $('LABELCONTACTUS',xml).text();
		fields['labelhome'] = $('LABELHOME',xml).text();
		fields['labelproducts'] = $('LABELPRODUCTS',xml).text();
		fields['labelhotdeals'] = $('LABELHOTDEALS',xml).text();
		fields['link1'] = $('LINK1',xml).text();
		fields['link2'] = $('LINK2',xml).text();
		fields['link3'] = $('LINK3',xml).text();
		fields['link1l'] = $('LINK1L',xml).text();
		fields['link2l'] = $('LINK2L',xml).text();
		fields['link3l'] = $('LINK3L',xml).text();
		fields['link1sif'] = $('LINK1SIF',xml).text();
		fields['link2sif'] = $('LINK2SIF',xml).text();
		fields['link3sif'] = $('LINK3SIF',xml).text();
		if (!fields['link1'].match(/^http:/)) $('#link1').html(fields['link1']);
		if (!fields['link2'].match(/^http:/)) $('#link2').html(fields['link2']);
		if (!fields['link3'].match(/^http:/)) $('#link3').html(fields['link3']);
		if (fields['index']) { $('#personalIndex').html(fields['index']); showModule('personalIndex'); }
		processXMLCat();
	});
}

function processXMLCat() {
	$.get('cat.xml', {}, function(xml){
		fields['categories'] = '';
		$("C", xml).each(function(i) {
			fields['categories'] += '<span style="white-space: nowrap;"><img src="' + getNextBullet() + '" style="margin:0 5px 0 8px"><a class="categoryLinks' + (i == 0 && !fields['index'] ? ' selectedCategory' : '' ) + '" href="productCatalog.php?cat=' + escape($(this).attr('name')) + '" target="module" onclick="showModule(\'module\'); $(\'.selectedCategory\').removeClass(\'selectedCategory\'); $(this).addClass(\'selectedCategory\');">' + toProperCase($(this).attr('name')).replace(/##/,"'") + '</a></span><br>';
		});

		fields['navbar'] = '<div style="margin-left:-25px"><ul class="trilist">';
		if (fields['labelhome']) fields['navbar'] += '<li><a class="mainLink" href="' + fields['home'] + '">'+fields['labelhome']+'</a></li>';
		if (fields['labelhotdeals']) fields['navbar'] += '<li><a class="" href="productCatalog.php?cat=HotDeals" target="module" onclick="showModule(\'module\'); $(\'.selectedCategory\').removeClass(\'selectedCategory\'); $(this).addClass(\'selectedCategory\'); ">'+fields['labelhotdeals']+'</a></li>';
		if (fields['labelproducts']) fields['navbar'] += '<li><a class="mainLink" href="#" onclick="$(\'#categories\').fadeToggle()">'+fields['labelproducts']+'</a></li><li class="nolist" id="categories">' + fields['categories'] + '</li>';
		if (fields['labelcontactus']) fields['navbar'] += '<li><a class="mainLink" href="contactus.php" target="module" onclick="showModule(\'module\'); ">'+fields['labelcontactus']+'</a></li>';
		if (fields['link1']) fields['navbar'] += getExtraLink(1);
		if (fields['link2']) fields['navbar'] += getExtraLink(2);
		if (fields['link3']) fields['navbar'] += getExtraLink(3);
		fields['navbar'] += '<li class="nolist"><br>' + fields['viewcart'] + '</li></ul><br></div>';
		updateLayout();
	});
}

function processSpecificItem(itemId) {
	$.get((currcat == 'HotDeals' ? 'hot.xml' : 'cat.xml'), {}, function(xml){
		$('QS Q', xml).each(function(i) {
			if ($('I', $(this)).text() == itemId) {
				fields['desc'] = $('D', $(this)).text();
				fields['itemId'] = itemId
				fields['productId'] = itemId;
				fields['category'] = $(this).parents('C').attr('name');
				fields['productcode'] = $('CODE', $(this)).text();
				fields['productimage'] = $('IMGL', $(this)).text();
				if (!fields['productimage']) fields['productimage'] = $('IMGS', $(this)).text();
				fields['productimage2'] = $('IMGX2', $(this)).text();
				fields['productimage3'] = $('IMGX3', $(this)).text();
				fields['productimage4'] = $('IMGX4', $(this)).text();
				fields['productimage5'] = $('IMGX5', $(this)).text();
				fields['productimage6'] = $('IMGX6', $(this)).text();
				fields['productimage7'] = $('IMGX7', $(this)).text();
				fields['productimageTT'] = $('TTIMG', $(this)).text();
				fields['productimageTT2'] = $('TTIMG2', $(this)).text();
				fields['productimageTT3'] = $('TTIMG3', $(this)).text();
				fields['productimageTT4'] = $('TTIMG4', $(this)).text();
				fields['productimageTT5'] = $('TTIMG5', $(this)).text();
				fields['productimageTT6'] = $('TTIMG6', $(this)).text();
				fields['productimageTT7'] = $('TTIMG7', $(this)).text();
				fields['productvideo'] = $('VIDEO', $(this)).text();				
				fields['price'] = $('P', $(this)).text();
				fields['msrp'] = $('MSRP', $(this)).text();
				fields['shipping'] = $('SH', $(this)).text();
				fields['productspecs'] = $('S', $(this)).text();
				fields['productsprice'] = $('SPRICE', $(this)).text();
				fields['productscart'] = $('SCART', $(this)).text();
				fields['prevnext'] = ($('I', $(this).prev('Q')).text() ? '<a class="prev" href="productDescription.php?cat='+escape((currcat ? currcat : fields['category']))+'&id='+$('I', $(this).prev('Q')).text()+'&view='+view+'&page='+page+'&style='+catStyle+'&sort='+sortBy+'&search='+search+'"><img src="images/prev.gif" /> Previous</a>&nbsp;&nbsp&nbsp;&nbsp;' : '')
				+ ($('I', $(this).next('Q')).text() ? '<a href="productDescription.php?cat='+escape((currcat ? currcat : fields['category']))+'&id='+$('I', $(this).next('Q')).text()+'&view='+view+'&page='+page+'&style='+catStyle+'&sort='+sortBy+'&search='+search+'" class="next">Next <img src="images/next.gif" /></a>' : '');
				updateProduct();
				return;
			}
		});
	});
}

function tplReplace(preHTML, fieldName, fieldContents) {
	if (fieldContents === undefined) fieldContents = '';
	replaceregex = new RegExp('{'+fieldName+'}', "gi");
	return preHTML.replace(replaceregex, fieldContents);
}

function insertProdDetails(addToCart, productDesc, productId, productPrice, productShipping) {
	productDesc = escape(productDesc); productId = escape(productId); productPrice = escape(productPrice);
	productShipping = escape(productShipping);
	return addToCart.replace(/PRODUCTDESCRIPTION/,productDesc).replace(/PRODUCTID/,productId).replace(/PRODUCTAMOUNT/,productPrice).replace(/PRODUCTSHIPPING/,productShipping);
}

$.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

function updateLayout() {
	preHTML = $('body').html();
	preHTML = tplReplace(preHTML, 'logo', '<center><img src="'
			+ ( fields['logo'].match(/^http/) ? fields['logo'] : 'images/'+fields['logo'] ) +'"/></center>');
	preHTML = tplReplace(preHTML, 'header', 
			(fields['header'] ? '<img src="' + ( fields['header'].match(/^http/) ? fields['header'] : 'images/'+fields['header'] ) +'"/>'
			 : '<div id="headerText">'+fields['headertxt']+'</div><div id="slogan">'+fields['slogan']+'</div>' ) );
	preHTML = tplReplace(preHTML, 'navbar', fields['navbar']);
	preHTML = tplReplace(preHTML, 'footer', fields['footer']);
	preHTML = tplReplace(preHTML, 'viewcart', fields['viewcart']);
	preHTML = tplReplace(preHTML, 'cuname', fields['cuname']);
	preHTML = tplReplace(preHTML, 'cuadd1', fields['cuadd1']);
	preHTML = tplReplace(preHTML, 'cuadd2', fields['cuadd2']);
	preHTML = tplReplace(preHTML, 'cuemail', fields['cuemail']);
	preHTML = tplReplace(preHTML, 'cuemailform', fields['cuemailform']);
	preHTML = tplReplace(preHTML, 'cuphone', fields['cuphone']);
	preHTML = tplReplace(preHTML, 'cufax', fields['cufax']);
	$('body').html(preHTML).fadeIn();
	$('body.prime').attr('background', ( fields['background'].match(/^http/) ? fields['background'] : 'images/'+fields['background'] ) );
}

function updateProduct() {
	preHTML = $('body').html();
	preHTML = tplReplace(preHTML, 'prevnext', fields['prevnext']);
	preHTML = tplReplace(preHTML, 'desc', fields['desc']);
	preHTML = tplReplace(preHTML, 'category', toProperCase(fields['category']));
	preHTML = tplReplace(preHTML, 'extraproductimages', produceGallery(fields['productimage'], fields['productimage2'], fields['productimage3'], fields['productimage4'], fields['productimage5'], fields['productimage6'], fields['productimage7'], fields['productimageTT'], fields['productimageTT2'], fields['productimageTT3'], fields['productimageTT4'], fields['productimageTT5'], fields['productimageTT6'], fields['productimageTT7'], fields['productvideo']));
	if (!fields['productimage']) fields['productimage'] = '';
	preHTML = tplReplace(preHTML, 'productimage', '<a href="productCatalog.php?cat='+escape((currcat ? currcat : fields['category']))+'&view='+view+'&page='+page+'&style='+catStyle+'&sort='+sortBy+'&search='+search
		+'"><img id="productImage" class="'+(fields['zoomimages'] ? 'productImageBig' : 'productImageNormal')+'" src="'
		+ ( fields['productimage'].match(/^http/) ? fields['productimage'] : 'images/'+fields['productimage'] ) 
		+'" onerror="this.src=\'images/nophoto.png\'; $(this).removeClass(\'productImageBig\'); this.border = 0;" /></a><div id="productVideo"></div>');
	preHTML = tplReplace(preHTML, 'productspecs', fields['productspecs']);
	preHTML = tplReplace(preHTML, 'productcode', fields['productcode']);
	preHTML = tplReplace(preHTML, 'price', (fields['msrp'] ? 'MSRP: <b><span style="text-decoration: line-through;">'+fields['denom']+fields['msrp'] + '</span></b><br><br>' : '') + (fields['productsprice'] ? (fields['price'].match(/(\w+\s+)+/gi) ? '' : (fields['msrp'] ? 'Our ' : '') + 'Price: <b>' + fields['denom'])+fields['price']+'</b><br><br>' : '') + (fields['shipping'] ? 'S&H: <b>'+fields['denom']+fields['shipping']+'</b>' : '') );
	preHTML = tplReplace(preHTML, 'addtocart', (fields['productscart'] ? insertProdDetails(fields['addtocart'], fields['desc'], fields['productcode'], fields['price'], fields['shipping']) : ''));
	$('body').html(preHTML);
}

function getNextBullet() {
	return 'images/bullet-arrows' + ((bullet++)%10 + 1) + '.jpg';
}

function toProperCase(s) {
	if (s) return s.toLowerCase().replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });
}

function showModule(module) {
	$('#moduletd div, #moduletd iframe').hide(); $('#'+module).show();
}

function getExtraLink(linkN) {
	return '<li><a class="mainLink" href="#" onclick="' 
		+ (fields['link'+linkN].match(/^http:/) ? 'this.href = fields[\'link'+linkN+'\']; this.target = (fields[\'link'+linkN+'sif\'] ? \'module\' : \'\'); showModule(\'module\')' : 'showModule(\'link'+linkN+'\')' )
		+'">'+fields['link'+linkN+'l']+'</a></li>';
}

function produceGallery(img1, img2, img3, img4, img5, img6, img7, img1tt, img2tt, img3tt, img4tt, img5tt, img6tt, img7tt, video) {
	var string = '';
	if (img1) {
		if (!img1.match(/^http:/)) img1 = 'images/'+img1; 
		string += '<img src="'+img1+'" class="thumbnail" onclick="showImage(this.src)" onerror="$(this).remove()" title="'+img1tt+'">';
	}
	if (img2) { 
		if (!img2.match(/^http:/)) img2 = 'images/'+img2; 
		string += '<img src="'+img2+'" class="thumbnail" onclick="showImage(this.src)" onerror="$(this).remove()" title="'+img2tt+'">'; 
		}
	if (img3) { 
		if (!img3.match(/^http:/)) img3 = 'images/'+img3; 
		string += '<img src="'+img3+'" class="thumbnail" onclick="showImage(this.src)" onerror="$(this).remove()" title="'+img3tt+'">';	
		}
	if (img4) {
		if (!img4.match(/^http:/)) img4 = 'images/'+img4; 
		string += '<img src="'+img4+'" class="thumbnail" onclick="showImage(this.src)" onerror="$(this).remove()" title="'+img4tt+'">'; 
	}
	if (img5) { 
		if (!img5.match(/^http:/)) img5 = 'images/'+img5; 					
		string += '<img src="'+img5+'" class="thumbnail" onclick="showImage(this.src)" onerror="$(this).remove()" title="'+img5tt+'">'; 
	}
	if (img6) {
		if (!img6.match(/^http:/)) img6 = 'images/'+img6; 
		string += '<img src="'+img6+'" class="thumbnail" onclick="showImage(this.src)" onerror="$(this).remove()" title="'+img6tt+'">'; 
	}
	if (img7) { 
		if (!img7.match(/^http:/)) img7 = 'images/'+img7;
		string += '<img src="'+img7+'" class="thumbnail" onclick="showImage(this.src)" onerror="$(this).remove()" title="'+img7tt+'">'; 
	}
	if (video) {
		string += '<img src="images/video.gif" class="thumbnail" onclick="showVideo(\''+video+'\');" onerror="$(this).remove()" title="Watch Video Clip">'; 
	}
	return string;
}

function videoEmbedCode(link) {
	if (link.match(/youtube.com/)) return '<iframe src="'+link+'" width="350" height="266" frameborder="0" border="0"></iframe><br><a href="productCatalog.php?cat='+escape((currcat ? currcat : fields['category']))+'&view='+view+'&page='+page+'&style='+catStyle+'&sort='+sortBy+'&search='+search+'">Close Video</a>';
	else return '<iframe src="videoplayer.php?link='+escape(link)+'" width="400" height="300" frameborder="0" border="0"></iframe><br><a href="productCatalog.php?cat='+escape((currcat ? currcat : fields['category']))+'&view='+view+'&page='+page+'&style='+catStyle+'&sort='+sortBy+'&search='+search+'">Close Video</a>';
} 

function showVideo(video) {
	$('#productImage').hide(); $('#productVideo').html(videoEmbedCode(video)).show();
}

function showImage(image) {
	$('#productVideo').hide().html(''); $('#productImage').attr('src', image).show();
}
