$(document).ready(function() {
	$("body").addClass("jsOn");
	$("body").css('backgroundImage', 'url(/img/dassler_background.jpg)');
	$("a.copy-switcher").click(function() {
		$("div.copy").hide();
		$("a.copy-switcher").removeClass('active');
		$(this).addClass('active');
		$("#copy-" + $(this).parent().attr('class')).show();
		return false;
	});

	$('.slideshow').innerfade({ animationtype: 'fade',  speed: 'slow', timeout: 4000, type: 'sequence', containerheight: 'auto' }); 

	//	History Timeline Functions
		$("#historyTimeline h2").hover(
		function(){
			$(this).addClass("over");
		},
		function(){
			$(this).removeClass("over");
		}
	);
	$("#historyTimeline h2").historyTimeline();

	$("#historyTimeline h2").each(function(){
		var thisID = $(this).attr("id");
		var text = $(this).text();
		$(this).next(".item").prepend('<h3 class="' + thisID + '">' + text + '</h3>');
	});

	$("#historyTimeline p.prevContent a").historyTimelinePrev();
	$("#historyTimeline p.nextContent a").historyTimelineNext();

//	Product Filters
	$("#productFilters input").attr("checked", "checked");
	$("#productFilters input").productFilters();

	$('#retailers-country select').change(function() {
		$.ajax({type: "GET", url: "/retailers/city/", data: {format: "xml", country: this.value}, dataType: "xml", success: function(xml) {
			var cities = $(xml).find('city');
			var state_province = $(cities[0]).attr('state_province');
			var cities_buffer = "<option>Please select...</option>";

			var cur_state_province = '';
			var first = 1;
			$(xml).find('city').each(function() {
				var new_state_province = $(this).attr('state_province');

				if(cur_state_province != new_state_province) {
					if(!first) {
						cities_buffer += "</optgroup><optgroup label='" + $(this).attr('state_province') + "'>";
					} else {
						cities_buffer += "<optgroup label='" + $(this).attr('state_province') + "'>";
						first = 0;
					}
					cur_state_province = new_state_province;
				}
				
				cities_buffer += "<option value='" + $(this).attr('name') + "'>" + $(this).attr('name') + "</option>";
			});
			cities_buffer += '</optgroup>';
		
			$('#retailers-city select').html(cities_buffer);
			
		}});

		$("#selectcities").html("Please select...");
	});

	$('#retailers-city select').change(function() {
		$.ajax({type: "GET", url: "/retailers/store/", data: {format: "xml", country: $('#retailers-country select').val(), city: this.value}, dataType: "xml", success: function(xml) {
			var store_buffer = "";

			$(xml).find('store').each(function() {
				var id = $(this).attr("id");
				var name = $(this).attr('name');
				var address = $(this).attr('address1');
				var address2 = $(this).attr('address2');
				var city = $(this).attr('city');
				var state_province = $(this).attr('state_province');
				var phone_number = $(this).attr('phone_number');

				if(address2 != "") {
					address2 += "<br />";
				}

				if(state_province != "") {
					state_province = ", " + state_province;
				}

				store_buffer += "<div id='store-" + id + "' class='store' style='display: none;'><span class='title'>" + name + "</span><br />" + address + "<br />" + address2 + city + state_province + "<br />" + phone_number + "</div>";
			});

			$('#store-results').html(store_buffer);

			$(".store:eq(0)").show();
			$(".store:eq(0)").addClass('no-border');
			$(".store:eq(1)").show();
		}});
	});

	$("#show-previous-retailers").click(function() {
		var n = $('.store').length;
		var index = $('.store').index($(".store.no-border"));

		if(index != 0) {
			$(".store:eq(" + index + ")").removeClass('no-border');

			index = index+1;
			$(".store:eq(" + index + ")").hide();

			index = index-2;
			$(".store:eq(" + index + ")").addClass('no-border');
			$(".store:eq(" + index + ")").show();
		}
		return false;
	});

	$("#show-next-retailers").click(function() {
		var n = $('.store').length;
		var index = $('.store').index($(".store.no-border"));

		if(index != (n - 1)) {
			$(".store:eq(" + index + ")").hide();
			$(".store:eq(" + index + ")").removeClass('no-border');
			index = index+1;
			$(".store:eq(" + index + ")").addClass('no-border');
			index = index+1;
			$(".store:eq(" + index + ")").show();
		}

		return false;
	});

	$("#play").click(function() { swfobject.embedSWF("swf/video-player.swf", "video-fla", "526", "397", "9.0.0", null, {video:"Pumaneko_Final_sm.flv", xpad:"15", ypad:"15"}, {menu:"false", wmode:"transparent"}); return false; });
});



$.fn.productFilters = function() {

	$(this).click(function(){

		var obj = $(this);
		var child = obj.parent("li").children("label").text();
		var thisClass = child.toLowerCase();
		var status = obj.attr("checked");
		var parent = obj.parent().parent().attr("class");

		$("#products li").each(function(){
			if( status == true ){
				if ( $(this).hasClass(thisClass) ) {
					if (parent == "gender"){
						$(this).removeClass("genderHidden");
					} else if (parent == "type") {
						$(this).removeClass("typeHidden");
					}
				};
			} else if ( status == false ){
				if ( $(this).hasClass(thisClass) ){
					if (parent == "gender"){
						$(this).addClass("genderHidden");
					} else if (parent == "type"){
						$(this).addClass("typeHidden");
					}
				}
			}
		});

	});

};

$.fn.historyTimeline = function() {
	$(this).click(function(){
		var obj = $(this);
		$("#historyTimeline .item, #historyTimeline h2").removeClass("active");
		obj.addClass("active");
		obj.next(".item").addClass("active");
		return false;
	});
};

$.fn.historyTimelinePrev = function() {
	var obj = $(this);
	obj.click(function(){
		var currActiveH2 = $("#historyTimeline h2.active");
		var prevActiveH2 = currActiveH2.prev().prev("h2");
		var currActiveItem = $("#historyTimeline div.active");
		var prevActiveItem = currActiveItem.prev().prev(".item");
		var prevItemText = $("#historyTimeline .item.active").prev("h2").text();
		if ( prevItemText > 0 ){
			prevActiveH2.addClass("active");
			currActiveH2.removeClass("active");
			prevActiveItem.addClass("active");
			currActiveItem.removeClass("active");
		}
		return false;
	})
}

$.fn.historyTimelineNext = function() {
	var obj = $(this);
	obj.click(function(){
		var currActiveH2 = $("#historyTimeline h2.active");
		var nextActiveH2 = currActiveH2.next().next("h2");
		var currActiveItem = $("#historyTimeline div.active");
		var nextActiveItem = currActiveItem.next().next(".item");
		var nextItemText = $("#historyTimeline .item.active").next("h2").text();
		if ( nextItemText > 0 ){
			nextActiveH2.addClass("active");
			currActiveH2.removeClass("active");
			nextActiveItem.addClass("active");
			currActiveItem.removeClass("active");
		}
		return false;
	})
}
