$(document).ready(function(){
	$('select:not([name*=size])').customSelect();
	navigation();
	photoGallery();
	submitHover();
	ieHover('.form-info .btn-add input, .form-info div.row, .form-info div.row1');
	initPopup();
	setupSize();

	// Gift Cert Validation
	if ($('#giftcert-submit').size() > 0){
		$('#giftcert-submit').click(function(){
			var flag = false;
			$('form input, form select, form textarea').each(function(){
				if (($(this).val()=='') && (flag == false)) {
					var field = $(this).attr('name');
					var msg = 'Please enter your '+field;
					if (field == 'message') {msg = 'Please enter a message for your recipient';}
					if (field == 'certificate_amount') {msg = 'Please select an amount for your Gift Certificate';}
					alert(msg);
					flag = true;
				} 
			});
			if (flag==true) {return false;}
		});
	}
	
	// Add to cart product validation
	if ($('form.foxycart').not('body#home form').size() > 0) {
		$('form.foxycart input[name=submit]').click(function(){
			var color = $('select[name=color]').val();
			var quantity = $('select[name*=quantity]').val();
			var size = $('select[name*=size]').val();				
		
			if (color == '') {
				alert('Please Select a Color');
				return false;	
			} else if (quantity == '') {
				alert('Please Select Quantity');
				return false;
			} else if (size == '') {
				alert('Please Select a Size');
				return false;					
			} else {
				return true;
			}
		});	
	}
	
});

/*--- IE6 hover ---*/
function ieHover(h_list, h_class){
	if($.browser.msie && $.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		$(h_list).mouseenter(function(){
			$(this).addClass(h_class);
		}).mouseleave(function(){
			$(this).removeClass(h_class);
		});
	}
}
/*--- submit button hover ---*/
function submitHover(){
	$('input[class*="btn-"]').mouseenter(function(){
		this.src = this.src.replace(".gif", "-hover.gif");
	}).mouseleave(function(){
		this.src = this.src.replace("-hover.gif", ".gif");
	});
}
/*--- navigation ---*/
function navigation(){
	
	var _fakeNav = '<ul id="fakenav">';
	for (var i=0; i < $('#product-images div.list-image > ul > li').length; i++) {
		_fakeNav += '<li><a href="#">'+i+'</a></li>';
	}
	_fakeNav += '</ul>';
	_fakeNav = $(_fakeNav).hide();
	$('#product-images').append(_fakeNav);
	
	$('#product-images').slideShow({		
		slideEl:'div.list-image > ul > li',		
		linkNext:'a.link-next',		
		linkPrev:'a.link-prev',
		numElement:'#fakenav a',
		duration:300
	});
	$('#main').slideShow({		
		slideEl:'div.list-image > ul ul > li',
		numElement:'div.btn-link a',	
		duration:300
	});
	$('#main div.btn-link a').mouseup(function(){
		$('#fakenav a:first').css('border','1px solid red').trigger('click');
	});
}
/*--- photo gallery ---*/
function photoGallery(){
	var stay_time = 5000; //in ms or 'false' if not needed
	var change_speed = 500; //in ms
	$('div.photo-holder > ul').each(function(){
		var _hold = $(this);
		var _list = _hold.children();
		var _a = (_list.index(_list.filter('.active:eq(0)')) != -1)?(_list.index(_list.filter('.active:eq(0)'))):(0);
		var _t, _f = true;
		
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		
		if(_f && stay_time){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, stay_time);
		}
		
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_list.eq(_a).removeClass('active').animate({opacity: 0},{queue:false, duration:change_speed});
				_list.eq(_ind).addClass('active').animate({opacity: 1},{queue:false, duration:change_speed});
				_a = _ind;
			}
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time+change_speed);
			}
		}
	});
}
/*--- popup function ---*/
function initPopup(){
	if($('#fader').length == 0) $('body').append('<div id="fader"></div>');
	var _fader = $('#fader');
	_fader.hide();
	var _popup = -1;
	
	$('a.with-popup').each(function(){
		var _el = this;
		_el._popup = false;
		_el.onclick = function(){
			if(!_el._popup){
				$.ajax({
					url: this.href,
					dataType: "html",
					success: function(_html){
						_el._popup = $(_html);
						_el._popup.css({display:'none'}).appendTo('body');
						
						_el._popup.find('a.btn-close').click(function(){
							if($.browser.msie){
								_popup.hide();
								_fader.fadeOut(200);
								_popup = -1;
							}
							else{
								_popup.fadeOut(400, function(){
									_fader.fadeOut(200);
									_popup = -1;
								});
							}
							return false;
						});
						_popup = _el._popup;
						showPopup();
					}
				});
			}
			else if(_el._popup && _el._popup.length){
				_popup = _el._popup;
				showPopup();
			}
			return false;
		}
	});
	_fader.click(function(){
		if(_popup != -1){
			if($.browser.msie){
				_popup.hide();
				_fader.fadeOut(200);
				_popup = -1;
			}
			else{
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = -1;
				});
			}
		}
		return false;
	});
	
	$(document).keydown(function(e){
		if(!e)evt = window.event;
		if(e.keyCode == 27 && _popup != -1){
			if($.browser.msie){
				_popup.hide();
				_fader.fadeOut(200);
				_popup = -1;
			}
			else{
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = -1;
				});
			}
		}
	});
	/*
	$(window).resize(function(){
		if(_popup != -1){
			_popup.css({
				top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2,
				left: $(window).scrollLeft()+($(window).width()-_popup.outerWidth())/2
			});
			_fader.height(initH());
			if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
		}
	}).scroll(function(){
		if(_popup != -1){
			_popup.css({
				top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2,
				left: $(window).scrollLeft()+($(window).width()-_popup.outerWidth())/2
			});
			_fader.height(initH());
			if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
		}
	}); 
	*/
	function showPopup(){
		_fader.css({
			opacity: 0,
			height: initH(),
			display:'block'
		});
		_popup.css({top: $(window).scrollTop(), left: $(window).scrollLeft()+($(window).width()-_popup.outerWidth())/2});
		_fader.fadeTo(400, 0.5, function(){
			if(_popup != -1){
				if($.browser.msie) _popup.show();
				else _popup.fadeIn(200, function(){
					if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
				});
				_popup.css({
					top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2,
					left: $(window).scrollLeft()+($(window).width()-_popup.outerWidth())/2
				});
				if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
			}
		});
	}
	function initH(){
		var _h = $('#wrapper').outerHeight() + $('#footer').outerHeight();
		if(_h < $(window).height()) _h = $(window).height();
		if(_h < $('body').height()) _h = $('body').height();
		return _h;
	}
}


/*------------------------ custom select's -----------------------------*/
jQuery.fn.customSelect = function(_options) {
var _options = jQuery.extend({
	selectStructure: '<div class="selectArea"><div class="left"></div><div class="center"></div><a href="#" class="selectButton">&nbsp;</a><div class="disabled"></div></div>',
	selectText: '.center',
	selectBtn: '.selectButton',
	selectDisabled: '.disabled',
	optStructure: '<div class="selectOptions"><ul></ul></div>',
	optList: 'ul'
}, _options);
return this.each(function(){
	var select = jQuery(this);
	if((!select.hasClass('outtaHere')) || (select.is('[name*="size"]'))){
		if((select.is(':visible')) || (select.is('[name*="size"]'))) {
			var replaced = jQuery(_options.selectStructure);
			var selectText = replaced.find(_options.selectText);
			var selectBtn = replaced.find(_options.selectBtn);
			var selectDisabled = replaced.find(_options.selectDisabled).hide();
			var optHolder = jQuery(_options.optStructure);
			var optList = optHolder.find(_options.optList);
			if(select.attr('disabled')) selectDisabled.show();
			select.find('option').each(function() {
				var selOpt = $(this);
				var _opt = jQuery('<li><a href="#">' + selOpt.html() + '</a></li>');
				if(selOpt.attr('selected')) {
					selectText.html(selOpt.html());
					_opt.addClass('selected');
				}
				_opt.children('a').click(function(){
					optList.find('li').removeClass('selected');
					select.find('option').removeAttr('selected');
					$(this).parent().addClass('selected');
					selOpt.attr('selected', 'selected');
					selectText.html(selOpt.html());
					select.change();
					if(select.get(0).onchange) select.get(0).onchange();
					replaced.removeClass('opened');
					optHolder.hide();
					return false;
				});
				optList.append(_opt);
			});
			replaced.width(select.outerWidth());
			replaced.insertBefore(select);
			optHolder.css({
				width: select.outerWidth(),
				display: 'none',
				position: 'absolute'
			});
			if(select.attr('class') && select.attr('class') != '') optHolder.addClass('drop-'+select.attr('class'));
			jQuery(document.body).append(optHolder);
			
			var optTimer;
			replaced.hover(function() {
				if(optTimer) clearTimeout(optTimer);
			}, function() {
				optTimer = setTimeout(function() {
					replaced.removeClass('opened');
					optHolder.hide();
				}, 200);
			});
			optHolder.hover(function(){
				if(optTimer) clearTimeout(optTimer);
			}, function() {
				optTimer = setTimeout(function() {
					replaced.removeClass('opened');
					optHolder.hide();
				}, 200);
			});
			selectBtn.click(function(){
				if(optHolder.is(':visible')){
					replaced.removeClass('opened');
					optHolder.hide();
				}
				else{
					optHolder.children('ul').css({height:'auto', overflow:'hidden'});
					optHolder.css({
						top: replaced.offset().top + replaced.outerHeight(),
						left: replaced.offset().left,
						display: 'block'
					});
					replaced.addClass('opened');
					//if(optHolder.children('ul').height() > 100) optHolder.children('ul').css({height:100, overflow:'auto'});
				}
				return false;
			});
			select.addClass('outtaHere');
		}
	}
});
}

/* 
* jQuery slideShow v1.2.2
*/

jQuery.fn.slideShow = function(_options){    
	// defaults options	    
	var _options = jQuery.extend({		
		slideEl:'div.slide',		
		linkNext:'a.next',		
		linkPrev:'a.prev',		
		linkPause:'a.pause',		
		numElement:'div.slideNav li a',		
		duration:500,		
		autoSlideShow:false,		
		switchTime:3000,
		noCicle:false,
		disableClass:'no-active',
		event:'click',		
		currentEl:'span.cur',		
		allEl:'span.all',
		hideBeforeShow: false
	},_options);
    return this.each(function(){
	    var _THIS = jQuery(this),
			_slideEl = jQuery(_options.slideEl, _THIS),
			_linkNext = jQuery(_options.linkNext, _THIS),
			_linkPrev = jQuery(_options.linkPrev, _THIS),
			_linkPause = jQuery(_options.linkPause, _THIS),
			_numElement = jQuery(_options.numElement, _THIS),
			_currentEl = jQuery(_options.currentEl, _THIS),
			_allEl = jQuery(_options.allEl, _THIS),
			_duration = _options.duration,
			_switchTime = _options.switchTime,
			_numElActive, _timer = false, _hover = false, _current = 0, _next = 0, _pause = true;
		
		if (!_slideEl.filter('.active').length) {
			_slideEl.eq(0).addClass('active');
			_current = 0;
		} else {
			_current = _slideEl.index(_slideEl.filter('.active'));
		}
		_slideEl.not(".active").hide();
		
		if (jQuery(_numElement).length && _options.numElement) activeNumEl();
		if (_options.autoSlideShow) {
			_pause = false;
			_timer = setTimeout(function(){nextEl()},_switchTime);
			if (_linkPause.length && _options.linkPause) _linkPause.addClass('play')	
			contentHover();
		}
		if (_options.currentEl && _currentEl.length) {
			_allEl.html(_slideEl.length);
			currentNum();
		}
		if (_linkNext.length && _options.linkNext) {
			_linkNext.click(function(){
				nextEl();
				return false;
			});
		}
		if (_options.noCicle) {
			_linkPrev.addClass('prev-'+_options.disableClass);
		}
		if (_linkPrev.length && _options.linkPrev) {	
			_linkPrev.click(function(){
				_linkNext.removeClass('next-'+_options.disableClass);
				if (!_slideEl.is(':animated') && !jQuery(this).hasClass('prev-'+_options.disableClass)) {
					if (_timer) clearTimeout(_timer);
					_next = _current-1;
					if (_next < 0) _next = _slideEl.length-1;
					if (_options.noCicle && _next-1 < 0) {
						jQuery(this).addClass('prev-'+_options.disableClass);
					}
					fadeElement();
					activeNumEl();
					pauseCode();
				}
				return false;
			});
		}
		if (_numElement.length && _options.numElement) {
			_numElement.bind(_options.event, function(){
				if (!_slideEl.is(':animated')) {
					_next = _numElement.index(jQuery(this));
					_linkNext.removeClass('next-'+_options.disableClass);
					_linkPrev.removeClass('prev-'+_options.disableClass);
					if (_options.noCicle && _next+1 >= _slideEl.length) _linkNext.addClass('next-'+_options.disableClass);
					if (_options.noCicle && _next-1 < 0) _linkPrev.addClass('prev-'+_options.disableClass);
					if (_timer) clearTimeout(_timer);
					if (!_slideEl.eq(_next).hasClass("active")){
						fadeElement();
						activeNumEl();
						pauseCode();
					}
				}
				return false;
			});
		}
		if (_linkPause.length && _options.linkPause) {
			_linkPause.click(function(){
				if (!_pause) {
					jQuery(this).removeClass('play');
					clearTimeout(_timer);
					_pause = true;
				} else {
					jQuery(this).addClass('play');
					_timer = setTimeout(function(){nextEl()},_switchTime);
					_pause = false;
				}
				return false;
			});
		}
		function fadeElement(){
			if (_current != _next) {
				_slideEl.removeClass('active');
				_slideEl.eq(_current).addClass('hide');
				
				if (!_options.hideBeforeShow) {
					_slideEl.eq(_next).fadeIn(_duration, function(){
						_slideEl.filter('.hide').hide().removeClass('hide');
					}).addClass('active');
				} else {
					_slideEl.filter('.hide').fadeOut(_duration, function(){
						_slideEl.eq(_next).fadeIn(_duration);
					}).removeClass('hide');
				}
				_current = _next;
				if (_options.currentEl && _currentEl.length) currentNum();
			}
		};
		function currentNum() {
			_currentEl.html(_current+1);
		}
		function activeNumEl() {
			_numElement.parent().removeClass("active");
			_numElement.eq(_current).parent().addClass("active");
		};
		function nextEl(){
			_linkPrev.removeClass('prev-'+_options.disableClass);
			if (!_slideEl.is(':animated') && !_linkNext.hasClass('next-'+_options.disableClass)) {
				if (_timer) clearTimeout(_timer);
				_next = _current+1;
				if (_next == _slideEl.length) _next = 0;
				if (_options.noCicle && _next+1 >= _slideEl.length) {
					_linkNext.addClass('next-'+_options.disableClass);
				}
				fadeElement();
				activeNumEl();
				pauseCode();
			}
		};
		function pauseCode(){
			if (!_pause) {
				if (_linkPause.length && _options.linkPause) {
					if (_linkPause.hasClass('play')) {
						_timer = setTimeout(function(){nextEl()},_switchTime);
					}					
				} else {
					_timer = setTimeout(function(){nextEl()},_switchTime);
				}
			}
		}
		function contentHover() {
			_hover = true;
			_slideEl.mouseenter(function() {
				if (_timer) clearTimeout(_timer);
			}).mouseleave(function(){
				pauseCode();
			});			
		}
    });
}

function validateShopView() {

}

function setupSize() {

	size_select = $('select[name*="size"]').not('body#home select');
	size_select_div = size_select.parent();
	size_select_div.remove();

	product_name = $('#url_title').attr('class');

	$('select[name*="color"]').change(function(){
	
		if ($(this).val() != '') {
			
			var pretty_select_color = $(this).parent('.row');
			var color = pretty_select_color.find('.selectArea .center').text().toLowerCase();
			$('#url_title').attr('data-color',color);
			if ($('div.row.quantity').size() > 0) {
				var pretty_select_size = $('div.row.quantity');
				pretty_select_size.remove();		
			}
	
					
			pretty_select_color.after('<div class="row quantity"><div id="loading_sizes"><img src="/images/chrome/loading.gif"/> Loading Sizes...</div></div>');
			
			$('div.row.quantity').load('/shop/sizelist/'+product_name+'/'+color, function(data){
				$('#loading_sizes').remove();
				$(this).find('select').customSelect();
			});
		}		
	});
}
