var thebody = false;
var dts = false;
var dds = false;
var lastf = false;

var theloc = false;

var isSafari = ((parseInt(navigator.productSub)>=20020000) && (navigator.vendor.indexOf('Apple Computer')!=-1));
var DEF_VAL = 'enter ZIP code';

function externalLinks() {
	var links = thebody.getElementsByTagName('a');
	var linknum = links.length;
	for(var i = 0; i <linknum; i++) {
		var thelink = links[i];
		if(thelink.rel && thelink.rel == 'nofollow') {
			thelink.target = '_blank';
		}
	}
}

function addEvent(obj, evType, fn, useCapture) {
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		//alert("Handler could not be attached");
	}
}

function showMenu() {
	var dls = document.getElementsByTagName('dl');
	if(dls.length > 0) {
		var thedl = dls[0];
		dts = thedl.getElementsByTagName('dt');
		dds = thedl.getElementsByTagName('dd');
		for(x = 0; x < dds.length; x++) {
			var thedt = thedl.getElementsByTagName('dt')[x];
			var thedd = thedl.getElementsByTagName('dd')[x];
			var theul = thedd.getElementsByTagName('ul')[1];
			var result = '';
			var lis = theul.getElementsByTagName('li');
			for(i = 0; i < lis.length; i++) {
				var kind = lis[i].getElementsByTagName('span')[0].firstChild.data;
				var hasit = lis[i].getElementsByTagName('span')[1].firstChild.data;
				result += (result != '' ? ' ' : '') + hasit.substr(2) + i;
				if(hasit == ': yes') lis[i].className = 'a';
			}
			thedt.className = result;
			thedd.className = result;
		}
		var fas = document.getElementById('f').getElementsByTagName('a');
		for(i = 0; i < fas.length; i++) {
			if(i < 1) {
				fas[i].rel = ' ';
				fas[i].className = 's';
				lastf = fas[i];
			}else fas[i].rel = 'yes' + (i - 1);
			addEvent(fas[i], 'click', switchTab, false);
		}
	}
}

var canstuff = false;

function switchTab() {
	if(this.firstChild && this.firstChild.data) {
		canstuff = true;
		document.title = this.firstChild.data.replace('all', '') + theloc.replace('Apple', '');
		var h1t = thebody.getElementsByTagName('h1')[0].getElementsByTagName('a')[0];
		h1t.replaceChild(document.createTextNode(document.title), h1t.firstChild);
	}
	lastf.className = '';
	this.className = 's';
	filterStores(this.rel);
	lastf = this;
	piwikTracker.trackLink('#' + this.firstChild.data, 'link');
}

function filterStores(what) {
	var dtlen = dts.length;
	var haslen = dtlen;
	for(i=0; i < dtlen; i++) {
		var oldclass = dts[i].className;
		if(oldclass.indexOf(what) >= 0) {
			if(canstuff) {
				dts[i].style.opacity = '1';
				dts[i].style.height = 'auto';
				dts[i].style.margin = '0 0 -40px 0';
				dts[i].style.padding = '10px';
				dds[i].style.opacity = '1';
				dds[i].style.height = '90px';
				dds[i].style.padding = '40px 10px 10px 10px';
				dds[i].style.margin = '0 0 40px 0';
				dds[i].style.border = '1px solid #e7e7e7';
			}else{
				dts[i].style.display = 'block';
				dds[i].style.display = 'block';
			}
		}else{
			if(canstuff) {
				dts[i].style.height = '0px';
				dts[i].style.opacity = '0';
				dts[i].style.margin = '0';
				dts[i].style.padding = '0';
				dds[i].style.height = '0px';
				dds[i].style.opacity = '0';
				dds[i].style.padding = '0';
				dds[i].style.margin = '0';
				dds[i].style.border = '0';
			}else{
				dts[i].style.display = 'none';
				dds[i].style.display = 'none';
			}
			haslen--;
		}
	}
	if(haslen < 1) {
		if(nostores = document.getElementById('nostore')) {
			var nostorestr = document.createTextNode('No ' + document.title + ' found');
			nostores.replaceChild(nostorestr, nostores.firstChild);
			nostores.className = 'ns';
		}else{
			var nostores = document.createElement('p');
			nostores.className = 'ns';
			nostores.id = 'nostore';
			var nostorestr = document.createTextNode('No ' + document.title + ' found');
			nostores.appendChild(nostorestr);
			thebody.appendChild(nostores);
		}
	}else{
		if(nostores = document.getElementById('nostore')) {
			nostores.className = 'nsh';
		}
	}
}

function replaceSearchField()
{
	if (!document.getElementById)
		return;
	var searchForm = document.getElementsByTagName('form')[0];
	var searchField = searchForm.getElementsByTagName('input')[0];
	var searchButton = searchForm.getElementsByTagName('input')[1];
	if (isSafari) {
		searchField.setAttribute('type', 'search');
		searchField.setAttribute('autosave', 'in.apple-store');
		searchField.setAttribute('results', '5');
		searchField.setAttribute('placeholder', DEF_VAL);
		searchField.setAttribute('class', 'saf');
		searchButton.style.display = 'none';
	}else{
		addEvent(searchField, 'focus', focusSearch, false);
		addEvent(searchField, 'blur', blurSearch, false);
		if (searchField.value=='') searchField.value = DEF_VAL;
	}
	if(searchForm.className == 's') searchField.focus();
}

function focusSearch() {
	if (this.value==DEF_VAL) {
		this.value = '';
		this.setAttribute('class', 'focus');
	}
}

function blurSearch() {
	if (this.value=='') {
		this.value = DEF_VAL;
		this.removeAttribute('class', 'focus');
	}
}

function socialize() {
	var soclinks = [];
	soclinks[0] = ['http://www.facebook.com/share.php?u=', 'http://bookmarkcraze.com/images/bookmarkcraze_Facebook.png', 'Facebook'];
	soclinks[1] = ['http://twitthis.com/twit?url=', 'http://bookmarkcraze.com/images/bookmarkcraze_Twitter.png', 'Twitter'];
	soclinks[2] = ['http://del.icio.us/post?url=', 'http://bookmarkcraze.com/images/bookmarkcraze_del.icio.us.png', 'del.icio.us'];
	soclinks[3] = ['http://reddit.com/submit?url=', 'http://bookmarkcraze.com/images/bookmarkcraze_Reddit.png', 'Reddit'];
	soclinks[4] = ['http://www.stumbleupon.com/submit?url=', 'http://bookmarkcraze.com/images/bookmarkcraze_Stumbleupon.png', 'Stumbleupon'];
	soclinks[5] = ['https://www.google.com/bookmarks/mark?op=edit&bkmk=', 'http://bookmarkcraze.com/images/bookmarkcraze_Google.png', 'Google'];
	soclinks[6] = ['http://digg.com/submit?phase=2&url=', 'http://bookmarkcraze.com/images/bookmarkcraze_Digg_it.png', 'Digg it'];

	var socp = document.createElement('p');
	for(i in soclinks) {
		var soclink = document.createElement('a');
		soclink.href = soclinks[i][0];
		soclink.rel = soclinks[i][0];
		soclink.setAttribute('style', 'background: url(' + soclinks[i][1] + ') no-repeat;');
		soclink.title = soclinks[i][2];
		soclink.target = '_blank';
		soclink.appendChild(document.createTextNode(soclinks[i][2]));
		soclink.onmouseover = function() {
			this.href = this.rel + escape(self.document.location) + '&title=' + escape(Utf8.encode(self.document.title));
		}
		socp.appendChild(soclink);
	}
	socp.id = 'socp';
	var thel = document.getElementById('l');
	thebody.insertBefore(socp, thel);
}

var Utf8 = {

	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

function adspecial() {
	var sp = [];
	sp[0] = ['http://gan.doubleclick.net/gan_click?lid=41000613802001458&pubid=21000000000285025', '$100 Price Drop - Apple iMac 27" 3.2GHz Core i3, 4GB RAM, 1TB HD - Now Only $1299 Delivered'];
	var spp = document.createElement('p');
	for(i in sp) {
		var splink = document.createElement('a');
		splink.appendChild(document.createTextNode(sp[i][1]));
		splink.href = sp[i][0];
		splink.target = '_blank';
		spp.appendChild(splink);
	}
	spp.id = 'spp';
	var thel = document.getElementById('l');
	thebody.insertBefore(spp, thel);
}

function init() {
	thebody = document.getElementsByTagName('body')[0];
	theloc = thebody.getElementsByTagName('h1')[0].getElementsByTagName('a')[0].firstChild.data;
	showMenu();
	replaceSearchField();
	externalLinks();
	if(!document.getElementById('af')) {
		socialize();
	//	adspecial();
	}
}

addEvent(window, 'load', init, false);
