﻿var oBg_resize = {
	init: function() {
		this.eBg = $('#background')[0];
		this.eLayout = $('#layout')[0];

		if(RegExp('MSIE 6.0').test(navigator.userAgent)) {
			this.bIE6 = true;
			this.stretch();
			//this.width_fix();
			//this.height_fix();

			window.onresize = function() {
				oBg_resize.stretch();
			}
		}

	},

	stretch: function() {
		this.eBg.style.width = document.body.clientWidth > 1300 ? '100%' : '1300px';
		//this.width_fix();
		//this.height_fix();
	}// ,
	// 
	// 	width_fix: function() {
	// 		this.eLayout.style.width = document.body.clientWidth > 1000 ? '100%' : '1000px';
	// 	},
	// 
	// 	height_fix: function() {
	// 		this.eLayout.style.height = document.body.clientHeight > 1000 ? '100%' : '1000px';
	// 	}
};

var oMap = {
	init: function() {
		this.eList = $('#map dl');
		this.eMarks = $('#map dl dt a');
		this.aeMarks = $.makeArray(this.eMarks);
		this.aePlaces = $.makeArray($('#map dl dd'));

		var oThis = this;

		this.eMarks.mouseover(function() {return false;}).click(
			function() {
				oMap.toggle_places(this);
				return false;
		});
	},

	toggle_places: function(oThis) {
		var eCurrent = this.eList.find('dt.hidden');
		var eCurrent_new = $(oThis).parent('dt');

		eCurrent.removeClass('hidden').next().fadeOut('slow').addClass('hidden');

		eCurrent_new.addClass('hidden').next().fadeIn('slow').removeClass('hidden');
	}
};

var oMenu = {
	init: function() {
		// if(RegExp('MSIE').test(navigator.userAgent)) {
		// 			this.aeLinks = $('#main_navigation li a');
		// 			this.attach_event();
		// 		}
	},
	attach_event: function() {
		this.aeLinks.hover(
			function() {
				var eParent = $(this).parent('li');
				var eSpan = $(this).find('span');

				if (eParent.hasClass('selected')) {
					switch(eParent.attr('class')) {
						case 'brand selected':
							eSpan[0].style.top = '-490px';
							break;
						case 'stores selected':
							eSpan[0].style.top = '-160px';
							break;
						case 'instructions selected':
							eSpan[0].style.top = '-200px';
							break;
					}
				}
				else {
					switch(eParent.attr('class')) {
						case 'brand':
							eSpan[0].style.top = '-120px';
							break;
						case 'stores':
							eSpan[0].style.top = '-160px';
							break;
						case 'instructions':
							eSpan[0].style.top = '-200px';
							break;
					}
				}
			},
			function() {
				$(this).find('span')[0].style.top = '';
			}
		);
	}
};

var oSwitch_page = {
	init: function() {
		var clone;
		this.content = $('#content');
		var aePages = $.makeArray($('#content .page'));
		this.Images = $.makeArray($('#bottom_stuff div'));
		this.eStripe = $('#page_container .stripe')[0];

		if(RegExp('MSIE 6.0').test(navigator.userAgent)) {
			this.bIE6 = true;
		}

		this.get_direction();
	},

	get_direction: function() {
		var me = this;
		this.direction;
		this.boo = true;
			$('#content .page_switcher .pseudo_link').click(function() {
				var parent = $(this).parent();
				var deda = parent.parent();
				var pageId = deda.attr('class').split(' ')[0];
				var page = $('#' + pageId);
				var targetClass = $(this).attr('class').split(' ')[1];
				var targetLink = deda.siblings("div[class*="+targetClass+"]").find('div');
				var newPageId = $(this).attr('class').split(' ')[1];
				var newPage = $('#' + newPageId);
				if(me.boo){
					if(parent.is('.left')) {
						me.direction = 'prev';
					}
					else if(parent.is('.right')) {
						me.direction = 'next';
					}
					me.switch_pages(page, deda, targetLink, newPage, me);
				}
				
			
			});
	},

	switch_pages: function(page, deda, targetLink, newPage,me) {
		var oThis = this;
		this.eCurrent_content = page;
		this.eDeda = deda.find('div');
		this.eTargetLink = targetLink;
		this.newPage = newPage;
		

		// switch (this.direction) {
		// 	case 'next':
		// 		this.new_page = page.next('.page');
		// 		break
		// 	case 'prev':
		// 		this.new_page = page.prev('.page');
		// 		break
		// 	default:
		// 		return false;
		// }

		$('#bottom_stuff div').each(function() {
			if (!$(this).hasClass(oThis.newPage[0].id)) {
				$(this).fadeOut('slow');
			}
			else {
				$(this).fadeIn('slow');
			}
		});

		this.fader(me);
	},

	fader: function(me){
		var that = this;
		this.me = me;
		this.iStep = 0;
		this.iSteps = 20;
		this.iHalf_step = this.iSteps / 2;
		this.animation(me);
	},

	animation: function(me){
		this.iStep++;
		this.boo = false;
		if(this.iStep <= this.iSteps){
			if(this.iStep < this.iHalf_step){
				var iOpacity = (this.iHalf_step - this.iStep) / this.iHalf_step;
				this.eCurrent_content[0].style.opacity = iOpacity;
				this.eDeda.css('opacity', iOpacity - 0.1);
				var ix = 'alpha(opacity=' + (iOpacity - 0.1) * 99 + ')';
				this.eCurrent_content[0].style.filter = ix;
				this.eDeda.css('filter', ix);
			}
			if(this.iStep == this.iHalf_step){
				this.newPage[0].style.opacity = 0;
				this.eTargetLink.css('opacity', 0);
				this.newPage[0].style.filter = 'alpha(opacity=0)';
				this.eTargetLink.css('filter', 'alpha(opacity=0)');
				this.newPage[0].style.visibility = 'visible';
				this.eTargetLink.css('display', 'block');
				if(!this.eStripe.style.left) {
					this.eStripe.style.left = 0;
				}
				var start_left = parseInt(this.eStripe.style.left);
			
				switch (this.direction) {
					case 'next':
						this.eStripe.style.left = start_left - 100 + '%';
						break
					case 'prev':
						this.eStripe.style.left = start_left + 100 + '%';
						break
					default:
						return true;
				}
			}
			if(this.iStep >= this.iHalf_step){
				var iOpacity = (this.iStep - this.iHalf_step) / this.iHalf_step;
				this.newPage[0].style.opacity = iOpacity-0.01;
				this.eTargetLink.css('opacity', iOpacity);
				var iy = 'alpha(opacity=' + iOpacity * 100 + ')';
				this.newPage[0].style.filter = iy;
				this.eTargetLink.css('filter', iy);
			}
			var oThis = this;
			setTimeout(function(){oThis.animation();}, 0);
		}
		else {
			this.eDeda.css('display','none');
		}
		if(this.iStep == this.iSteps + 1){
			this.boo = true;
			this.newPage[0].style.filter = '';
			this.eTargetLink.css('filter', '');
		}
	}
};


var oStairs = {
	init: function() {
		$('#content .columns .steps .explanation').append('<div style="float: left; margin-right: -100%; visibility: hidden;" id="line_reduser">A</div>');
		
		window.onresize = function() {
			//oStairs.recalculate();
		};
	},

	recalculate: function() {
		//console.log($('#content .columns .steps .explanation')[0].offsetHeight / $('#line_reduser')[0].offsetHeight);
	}
};

var oWashing = {
	init: function() {
		//this.signs_container = $('#washing_signs');
		this.signs = $('#washing_signs dt');

		if(oBg_resize.bIE6) {
			this.hover_icons();
		}

		this.toggle_info();
		this.shoes_clothes();
	},

	hover_icons: function() {
		this.signs.hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		);
	},

	toggle_info: function() {
		this.signs.click( function() {
			this.signs_container = $('#washing_signs');
			$(this.signs_container).find('.selected').removeClass('selected');
			
			$(this).addClass('selected');
			var current_sign = $(this).next('dd')[0];
			$(current_sign).addClass('selected');
		});
	},

	shoes_clothes: function() {
		var oThis = this;
		var eStripe = $('#content div.content div.container div.stripe')[0];

		var clothes = $('#clothes');
		var shoes = $('#shoes');
		var clothes_link = $('#content div.content h2.clothes_link span');
		var shoes_link = $('#content div.content h2.shoes_link span');
		
		$('#content div.content h2 span').hover(
			function(){
				if($(this).is('.pseudo_link')){
					$(this).addClass('hover');
				}
			},
			function(){
				$(this).removeClass('hover');
			}
		);

		clothes_link.click( function() {
			$(this).removeClass('pseudo_link hover');
			shoes_link.addClass('pseudo_link');
			shoes.animate({opacity: 0}, 700, 'linear', function() {
				eStripe.style.left = 0;
				clothes.animate({opacity: 0.99}, 1000, function() {
					clothes[0].style.filter = '';
					//clothes[0].style.opacity = '';
				});
			});
		});

		shoes_link.click( function() {
			$(this).removeClass('pseudo_link hover');
			clothes_link.addClass('pseudo_link');
			clothes.animate({opacity: 0}, 700, 'linear', function() {
				eStripe.style.left = '-100%';
				shoes.animate({opacity: 0.99}, 1000, function() {
					shoes[0].style.filter = '';
					//shoes[0].style.opacity = '';
				});
			});
		});
	}
};

$(function(){
	oBg_resize.init();
	if($('#map dl').length){
		oMap.init();
	}
	if($('#main_navigation li a').length){
		oMenu.init();
	}
	// if($('#content .columns .steps .explanation').length){
	// 	oStairs.init();
	// }
	if($('#page_container .stripe').length){
		oSwitch_page.init();		
	}
	if($('#washing_signs').length){
		oWashing.init();
	}
});