
(function($){

	$(document).ready(function(){

		// style switcher.
		$('#primary ul.tools li.change-btn a')
			.click(function(){
				change_style_sheet($(this).attr('rel'));
			})
			// store button images.
			.find('img')
				.each(function(){
					var img = new Image();
					img.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_on$2');
					$(this).data('images', {
						default_src: this.src,
						active_src: img.src
					});
				})
			.end()
		;

		// set global nav drop down.
		$('#gnav > ul > li')
			// show or hide menus.
			.hover(
				function() {
					$('ul', this).stop(false, true).slideDown('fast');
				},
				function() {
					$('ul', this).stop(false, true).slideUp('fast');
				}
			)
			// hide menus at default.
			/*.find('ul')
				.hide()
			.end()*/
			
		;

		// set products sliding nav.
		if ($('#aside > div.products').length > 0) {
			var $nav = $('#aside > div.products');
			var current_category = $nav.attr('id');

			// for men's page (currently freshlight).
			if (location.hash === '#men') {
				current_category = 'men_'+ current_category;
			}

			// set toggle nav.
			$('h3', $nav)
				// store images for toggle.
				.find('img')
					.each(function(){
						var img = new Image();
						img.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_on$2');
						$(this).data('images', {
							default_src: this.src,
							active_src: img.src
						});
					})
				.end()

				// add toggle button.
				.append('<span></span>')
				.find('span')
					.toggle(
						// hide nav.
						function() {
							var $heading = $(this).closest('h3');

							// switch image.
							var $img = $('img', $heading);
							$img.attr('src', $img.data('images').active_src);
	
							$heading.next().stop(false, true).slideUp('fast');
						},
						// show nav.
						function() {
							var $heading = $(this).closest('h3');

							// switch image.
							var $img = $('img', $heading);
							$img.attr('src', $img.data('images').default_src);
	
							$heading.next().stop(false, true).slideDown('fast');
						}
					)
				.end()
			;

			// set default nav statement.
			$('div.group', $nav)
				// exclude current page.
				.filter(function(){
					return $(this).hasClass(current_category) !== true;
				})
				// hide nav trigger.
				.prev().find('span').click()
			;
		}

		// external link.
		$('#main-image area.external, #content area.external, a[rel=external]').click(function(){
			window.open(this.href, '_blank');
			return false;
		});

		// set active css.
		var title = readCookie('style') || 'small';
		change_style_sheet(title);
	});

	// change css by title.
	function change_style_sheet(title)
	{
		setActiveStyleSheet(title);

		// switch button status.
		$('#primary ul.tools li.change-btn a').each(function(){
			var $img = $('img', this);
			if (title === $(this).attr('rel')) {
				$img.attr('src', $img.data('images').active_src);
			}
			else {
				$img.attr('src', $img.data('images').default_src);
			}
		});

		// save current css title.
		var current_title = getActiveStyleSheet() || 'small';
		createCookie("style", current_title, 365);
	}

})(jQuery);
