var onView = 0;

function addShadow() {
	var shadow = document.createElement('div'),
		shadows = [
		document.createElement('div'),
		document.createElement('div'),
		document.createElement('div'),
		document.createElement('div')
	],
		dirs = ['n', 'e', 's', 'w'];
	for (var i = 0, ilen = shadows.length; i < ilen; i++) {
		shadows[i].id = dirs[i] + '-shadow';
		shadow.appendChild(shadows[i]);
	}
	shadow.id = 'collection-shadow';
	document.getElementById('canvas').appendChild(shadow);
}

function addGloss() {
	var n = [],
		x = document.createElement('div'),
		d = [],
		a = [];
	x.id = 'frames';
	x.style.width = $('#newsstand').width() + 'px';
	$('#newsstand a').each(function (i, e) {
		var img = e.getElementsByTagName('img')[0];
		$(e).width(img.width);
		if (!$.browser.msie) {
			//$(img).hide();
			n[i] = Raphael(e, img.width, 210);
			//n[i].image(img.src, 0, 0, img.width, img.height).attr({'stroke-width': 0});
			n[i].image(img.src, 0, 150, img.width, img.height).scale(1, -1).attr({opacity: .5, 'stroke-width': 0});
			n[i].rect(0, 150, img.width, 60).attr({gradient: "90-#FFF-#FFF", opacity: .5, 'stroke-width': 0});
		}
		d[i] = document.createElement('div');
		d[i].style.width = img.width + 'px';
		d[i].style.height = '210px';
		a[i] = document.createElement('a');
		a[i].style.width = img.width + 'px';
		a[i].style.height = '150px';
		a[i].href = e.href;
		a[i].setAttribute('popwidth', e.getAttribute('popwidth'));
		a[i].setAttribute('publication', img.title);
		a[i].setAttribute('date', img.alt);
		a[i].target = e.target;
		d[i].appendChild(a[i]);
		x.appendChild(d[i]);
	});
	document.getElementById('main').appendChild(x);
	$('#frames a').hover(
		function () {
			$(this).css({
				outline: '1px solid #000',
				opacity: .01
			});
			$(this).animate(
				{
					outlineWidth: 10,
					opacity: .6
				},
				{
					queue: false,
					duration: 250
				}
			);
		},
		function () {
			$(this).animate(
				{
					outlineWidth: 1,
					opacity: .01
				},
				{
					queue: false,
					duration: 250
				}
			);
		}
	);
	$('#frames a').click(function () {
		var s = $(this).attr('href');
		if (s.indexOf('hunterdixon') != -1) {
			var popup = window.open('press.php?src=' + s.
				replace('http://beta.hunterdixon.com/images/press/','').
				replace('.jpg','') + '&pub=' + this.getAttribute('publication') + '&date=' + this.getAttribute('date'), '', 'height=600, width=' + (parseInt(this.getAttribute('popwidth')) + 300));
			if (window.focus) popup.focus();
			return false;
		}
	});
	$('#newsstand img').css({
		position:'absolute',
		top: 0,
		left: 0
	});
	var previous = document.createElement('img');
	previous.id = 'previous';
	previous.src = 'images/previous.png';
	previous.onclick = morePrevious;
	document.getElementById('canvas').appendChild(previous);
	var next = document.createElement('img');
	next.id = 'next';
	next.src = 'images/next.png';
	next.onclick = moreNext;
	document.getElementById('canvas').appendChild(next);
	$('#newsstand, #frames').animate({
		left: 125
	}, {
		duration: 1000, 
		easing: 'swing',
		queue: false
	});
}

function setupCarousel() {
	var previous = document.createElement('img');
	previous.id = 'previous';
	previous.src = 'images/prevl.png';
	previous.onclick = toPrevious;
	document.getElementById('canvas').appendChild(previous);
	var next = document.createElement('img');
	next.id = 'next';
	next.src = 'images/next.png';
	next.onclick = toNext;
	document.getElementById('canvas').appendChild(next);
	/**/$('#previous').hide();
}

function centerMe(arg) {
	arg = arg || document.getElementById('0');
	var that = jQuery(arg),
		position = that.position();
	onView = that.attr('id');
	position.left -= ((1000/*800*/ - that.width()) / 2);
	$('#desc').animate({
		top: 480
	}, 'slow', 'swing');
	$('#cartouche').animate({
		left: position.left * -1
	}, {
		duration: 1250,
		easing: 'swing',
		complete: function () {
			$('#desc h3:first').text(that.attr('title'));
			$('#desc p:first').text(that.attr('alt'));
			Cufon.refresh($('#desc'));
			$('#desc').animate({
				top: 420
			}, 'slow', 'swing');
		}
	});
}

function dispatch() {
	centerMe(this);
}

function morePrevious() {
	$('#newsstand, #frames').animate({
		left: '+=667'
	}, {
		duration: 1500, 
		easing: 'swing',
		queue: false
	});
}

function moreNext() {
	$('#newsstand, #frames').animate({
		left: '-=667'
	}, {
		duration: 1500, 
		easing: 'swing',
		queue: false
	});
}

function toPrevious() {
	onView = onView - 0;
	var newId = onView - 1,
		testId = onView - 2;
	if (document.getElementById(newId)) {
		centerMe(document.getElementById(newId));
		/**/$('#next').show();
		document.getElementById('next').src = 'images/next.png';
		if(!document.getElementById(testId)) {
			//document.getElementById('previous').src = 'images/prevl.png';
			/**/$('#previous').hide();
		}
	} else {
		document.location = 'http://hunterdixon.com/springcollection2010/';
	}
}

function toNext() {
	onView = onView - 0;
	var newId = onView + 1,
		testId = onView + 2;
	if (document.getElementById(newId)) {
		centerMe(document.getElementById(newId));
		/**/$('#previous').show();
		document.getElementById('previous').src = 'images/previous.png';
		if(!document.getElementById(testId)) {
			document.getElementById('next').src = 'images/prevr.png';
			/**/$('#next').hide();
		}
	} else {
		document.location = 'http://hunterdixon.com/springcollection2010/';
	}
}

function ajaxify() {
	//APPEND OR REPLACE URI WITH #listing 
	var uri = document.location.toString();
	if (uri.indexOf('#') == -1) {
		document.location = document.location + '#listing';
	} else if (uri.indexOf('listing') == -1) {
		document.location = uri.replace('table', 'listing');
	}
	//ADD CLICK EVENTS
	$('#menu a').click(function () {
		if ($(this).attr('id') != 'switcher') {
			$('#menu a').removeClass();
			$(this).addClass('active');
			Cufon.refresh($('#menu a'));
			$('#locations').html('');
			var uri = $(this).attr('href'),
				switcher = document.location.toString();
			if (switcher.indexOf('listing') != -1) {
				uri = uri + '&ajax=1';
			}
			$.ajax({
				type: 'GET',
				url: uri,
				success: function (html) {
					if (switcher.indexOf('listing') == -1) {
						$('#locations').html(html);
						$('#locations').css({width: 780});
						$('#locations table tr:odd').css({backgroundColor:'rgba(255,255,255,.35)'});
					} else {
						var ajax = eval(html),
							length = ajax.length,
							i,
							s = '<ul>';
						for (i = 0; i < length; i++) {
							s += '<li><dl>';
							s += '<dt>' + (ajax[i].name || '--') + '</dt>';
							s += '<dd>' + (ajax[i].street || '--') + '</dd>';
							s += '<dd>' + (ajax[i].city || '--') + ', ' + (ajax[i].state || ajax[i].country) + ' ' + ajax[i].zip + '</dd>';
							s += '<dd>' + (ajax[i].phone || '--') + '</dd></dl></li>';
						}
						s += '</ul>';
						if (length > 8) {
							$('#locations').css({width:780});
						} else {
							$('#locations').css({width:520});
						}
						$('#locations').html(s);
					}
					Cufon.refresh($('#locations'));
				}
			});
			return false;
		} else {
			var text = $(this).text() == 'Table View' ? 'Listing View' : 'Table View';
			$(this).text(text);
			Cufon.refresh($('#switcher'));
			var uri = document.location.toString();
			if (uri.indexOf('listing') != -1) {
				document.location = uri.replace('listing', 'table');
			} else {
				document.location = uri.replace('table', 'listing');
			}
			$('.active').click();
			return false;
		}
	});
}

var formObj;
var inputObj;
var emptyText;
function SearchForm(formId, inputId, emptyDefaultText){
	formObj = jQuery(formId);
	inputObj = jQuery(inputId);
	emptyText = emptyDefaultText;
	formObj.bind('sumbit', 
		function(event){
	        if (inputObj.attr('value') == emptyText || inputObj.attr('value') == ''){
	            event.stop();
	        }
	    }
	);
	
	inputObj.bind('focus', searchFormFocus);
	inputObj.bind('blur', searchFormBlur);
	inputObj.blur();
}

var searchFormFocus = function(){
	if(inputObj.attr('value') == emptyText){
		inputObj.attr('value', '');
	}
}

var searchFormBlur = function(){
	if(inputObj.attr('value') == ''){
		inputObj.attr('value', emptyText);
	}
}