var scripttag = document.getElementsByTagName('script')[1];
wwwbase = scripttag.src.match(/(.*)public\/js.*/)[1];

// fadeToggle function plugin
jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

var Sweboo = {};


jQuery(document).ready(function() {
	
	$('.formsubmit').click(function() {
		$(this).parents('form:first').submit();	
		return false;
	});	
	
	if ($('#footer-ticker li').length > 1) {
		$('#footer-ticker .wrapper').vTicker({
			speed: 500,
			pause: 5000,
			showItems: 1,
			animation: 'fade',
			mousePause: true,
			height: 25,
			direction: 'up'
		});
	}	
	
	if ($('#main-banner .banner img').length > 1) {
		$('#main-banner .banner').nivoSlider({
			effect: 'random',
			slices: 15,
	        boxCols: 8,
	        boxRows: 4,
	        animSpeed: 500,
	        pauseTime: 6000,
	        startSlide: 0,
	        directionNav: true,
	        directionNavHide: false,
	        manualAdvance: false
		});
		
		
		$('.nivo-controlNav').css('left', parseInt((980 - $('.nivo-controlNav a').length * 23) / 2) + 'px');
	}

	$('#products-list .wrapper .tabs').tabs({
		selected : 0,
		select: function(event, ui) { 
			tabs = $('a', $(ui.tab).parents('ul:first'));
			index = tabs.index($(ui.tab));
			tabs.removeClass('shadow');
			if (index != 0) {
				$(tabs[index - 1]).addClass('shadow');
			}
		}
	});
	
	$('#details .tabs').tabs({
		selected : 0
	})
	
	$('a:last', '#brands-list ul li strong em').each(function () {
		$(this).addClass('last');
	});
	
	$('strong', '#brands-list ul li').each(function() {
		$(this).css('left', ((163 - parseInt($(this).width()) + 6) / 2) + 'px');
	});
	
	$('#brands-list ul li').mouseenter(function () {
		$('strong', $(this)).fadeIn(300);
	}).mouseleave(function () {
		$('strong', $(this)).fadeOut(300);
	});
	
	setTimeout(function () {
		$('#faq var').css('height', ($('#faq ol').height() - 22) + 'px');			
	}, 300);
	
	$('#faq ol li.categories a').click(function () {
		$('#faq ol li.questions ul').hide();
		id = $(this).attr('id').toString().replace('cat-', '');
		$('#faq ol li.questions ul#questions-' + id).fadeIn('slow');
		$('dt, dd', '#faq ol li.categories').removeClass('selected');
		$(this).parents('dt:first,dd:first').addClass('selected');
		window.location.hash = 'category-' + id;
		setTimeout(function () {
			$('#faq var').css('height', ($('#faq ol').height() - 22) + 'px');			
		}, 300);
	});
	
	$('#faq ol li.questions a').click(function () {
		$('#faq .answer').hide();
		id = $(this).attr('tabindex').toString().replace('question-', '');
		$('#faq #answer-' + id).fadeIn('slow');
		$('#faq ol li.questions li').removeClass('selected');
		$(this).parents('li:first').addClass('selected');		
		
		window.location.hash = 'faq-' + id;
		setTimeout(function () {
			$('#faq var').css('height', ($('#faq ol').height() - 22) + 'px');			
		}, 300);
	});
	
	if ($('#faq').length) {
		hash = window.location.hash.toString();
		if (hash == "") {
			$('#questions-0').show();
		} else {
			parts = hash.split('-');
			if (parts[0] == '#category') {
				$('#faq ol li.categories a#cat-' + parts[1]).click();
			}
			if (parts[0] == '#faq') {
				elem = $('#faq ol li.questions a[tabindex=' + parts[1] + ']:last');
				tmp = elem.attr('rel').toString().split('-');
				$('#faq ol li.categories a#cat-' + tmp[1]).click();
				elem.click();
			}
		}
	}
	
	$('.bigpad .two_columns #subject').change(function () {
		if ($('.bigpad .two_columns .options ul#questions-' + $(this).val() + ' var').length) {
			$('.bigpad .two_columns .options').hide();
		} else {
			$('.bigpad .two_columns .options').show();
			$('.bigpad .two_columns .options ul, .bigpad .two_columns .options h3').hide();
			$('.bigpad .two_columns .options h3#category-' + $(this).val() + ', .bigpad .two_columns .options ul#questions-' + $(this).val()).fadeIn('slow');
		}
	}).change();
	
	$('.bigpad .two_columns .options ul li').mouseenter(function () {
		$(this).addClass('hover');
	}).mouseleave(function () {
		$(this).removeClass('hover');
	});
	
	// submit catalog filter button
	$('#submit_filter_button').click(function() {
		$('#filter_form').submit();
	});
	
	// reset catalog filter button
	$('#reset_filter_button').click(function() {
		url = window.location.toString();
		if (url.lastIndexOf('?') != -1) {
			url = url.substr(0, url.lastIndexOf('?'));
		}
		window.location = url;
	});
	
	// states
	$('select.country').change(function() {
		if (typeof _states[$(this).val()] == 'undefined') {
			$('select.state', $(this).parents('fieldset:first')).html('<option value="n/a">- State Not Available -</option>');			
		} else {
			options = new Array();
			i = 0;
			for(code in _states[$(this).val()]) {
				options[i++] = '<option value="' + code + '">' + _states[$(this).val()][code] + '</option>';
			}
			$('select.state', $(this).parents('fieldset:first')).html('').html(options.join(''))
		}
	});
	
	// product images display handling
	$('a.preview_images').click(function() {
		image = $(this).children("img:first").get(0);
		$('a.preview_images').parent().removeClass('current');
		$(this).parent().addClass('current');
		$('#main_image').get(0).src = image.src.toString().replace('_67x90_', '_375x500_');
		$('#main_view_larger').get(0).href = image.src.toString().replace('_67x90_', '_525x700_');
	});
	
	$('#view_larger').click(function() {
		$('#main_view_larger').click();
		return false;
	});
	
	$('#main_view_larger').click(function() {
		href = $(this).get(0).href;
		$('a.product_images').fancybox({'cyclic' : 'true'});
		$('a.product_images').each(function(e, i) {
			if ($(this).get(0).href == href) {
				$(this).click();
			}
		});
		return false;
	});
	
	// init fancybox
	$('a.size_guide').fancybox();
	
	// product sizes
	$('a.product_size').click(function() {
		if (!$(this).hasClass('inactive') && !$(this).hasClass('selected')) {
			$('a.product_size').removeClass('selected');
			$(this).addClass('selected');
			parts = $(this).get(0).id.split("_");
			$('#selected_size').html($('a.product_size.selected').html());
			$.ajax({
				url: wwwbase + 'products/xhr',
				data: 'method=get_quantities&id=' + parts[1] + '&size=' + parts[0],
				success: function(response){
					options = '';
					for (i = 1; i <= response; i++) {
						options += '<option value="' + i + '">' + i + '</option>';
					}
					$('select#quantity').html(options);
				}
			});			
		}
	});
	
	// add shipping address
	$('#add_shipping_address').click(function() {
		$('#shipping_address').show('slow');
		$('#add_shipping_address').parents('div:first').hide('slow');
	});
	
	// remove shipping address
	$('#remove_shipping_address').click(function() {
		$('#shipping_address').hide('slow');
		$('#shipping_address input').val('');
		$('#add_shipping_address').parents('div:first').show('slow');
	});
	
	// show flash messages
	$('.flash_messages.system').show('slow');
	
	// submit order
	$('#submit_order').click(function() {
		$('.warning').hide('slow');
		$('#flash_message').hide('slow');
		if ($('#terms_and_policy').get(0).checked == false) {
			$('.warning').show('slow');
			return false;
		}
		error = false;
		$('.not_empty').each(function() {
			if ($(this).attr('name').indexOf('billing_') >= 0 || ($(this).attr('name').indexOf('shipping_') >= 0 && !$('#copy_billing_details').attr('checked'))) {
				if ($(this).val().toString().replace(/^\s+|\s+$/g, '') == '') {
					error = true;
				}
			}
		});
		if (error) {
			$('#flash_message').show('slow');
			return false;
		}
		$(this).parents('form').get(0).submit();
	});
	
	// enter custom billing and shipping details
	$('.change_details').click(function() {
		$('#billing_shipping_details_custom').fadeToggle('slow');
		$('#billing_shipping_details').hide('slow');
		
	});
	
//	$('#paypal_payment').click(function() {
//		$('#credit_card_details').hide('slow');
//	});
//	
//	$('#credit_card_payment').click(function() {
//		$('#credit_card_details').show('slow');
//	});
	
	$('#copy_billing_details').click(function() {
		if ($(this).attr('checked')) {
			$('#shipping_details').hide('slow');
			$('#billing_details select, #billing_details input').each(function () {
				elem = $('[name="' + $(this).attr('name').toString().replace('billing_', 'shipping_') + '"]');
				elem.val($(this).val()).attr('readonly', true);
			});
		} else {
			$('#shipping_details').show('slow');
			$('#billing_details select, #billing_details input').each(function () {
				elem = $('[name="' + $(this).attr('name').toString().replace('billing_', 'shipping_') + '"]');
				elem.val('').attr('readonly', false);
			});
		}
	});
	
	$('.slide-info').click(function () {
		$('#potd .summary').toggle('slow');
		$('.slide-info span').toggle();
	});
	
	$('#breadcrumbs h3:first').css('margin-left', '0px');
	
	$('.table tr:last').addClass('last');

	$(window).resize(function() {
		if (!$('body.home').length) {
   			$('#container').css('minHeight', ($('#outer').height() - 15 - ($('#header').height() + parseInt($('#header').css('paddingTop')) + $('#bottom').height() + parseInt($('#bottom').css('paddingTop')))) + 'px');
		}
	}).trigger('resize');
	
});
