var bobdylan = { id: "bd", model: "", size: "" }
var brucespringsteen = { id: "bs", model: "", size: "" }
var fadeSpeed = 200;

$(document).ready(function() {

	function initModel() {
		addModel(bobdylan, 'men');
		addModel(bobdylan, 'women');
		addModel(brucespringsteen, 'men');
		addModel(brucespringsteen, 'women');
		
	}

	function addModel(shirt, model) {
		$('#' + shirt.id + '-' + model).addClass('button');

		if(shirt.id == 'bd') {
			if(model == 'men') {
				$('#' + shirt.id + '-men').click(function() {
					changeModel(bobdylan, 'men');
				});
			} else if(model == 'women') {
				$('#' + shirt.id + '-women').click(function() {
					changeModel(bobdylan, 'women');
				});
			}
		} else if(shirt.id == 'bs') {
			if(model == 'men') {
				$('#' + shirt.id + '-men').click(function() {
					changeModel(brucespringsteen, 'men');
				});
			} else if(model == 'women') {
				$('#' + shirt.id + '-women').click(function() {
					changeModel(brucespringsteen, 'women');
				});
			}
		}
	}
	
	function removeModel(shirt, model) {
		$('#' + shirt.id + '-' + model).removeClass('button');
		$('#' + shirt.id + '-' + model).unbind('click');
	}

	function initSize(shirt) {
		$('#' + shirt.id + '-header-size').fadeTo(fadeSpeed, 1);

		$('#' + shirt.id + '-size-small-inactive').fadeTo(fadeSpeed + 100, 0);
		$('#' + shirt.id + '-size-small-regular').fadeTo(fadeSpeed + 100, 1);

		$('#' + shirt.id + '-size-medium-inactive').fadeTo(fadeSpeed + 200, 0);
		$('#' + shirt.id + '-size-medium-regular').fadeTo(fadeSpeed + 200, 1);

		$('#' + shirt.id + '-size-large-inactive').fadeTo(fadeSpeed + 300, 0);
		$('#' + shirt.id + '-size-large-regular').fadeTo(fadeSpeed + 300, 1);
		
		$('#' + shirt.id + '-size-guide-inactive').fadeTo(fadeSpeed + 400, 0);
		$('#' + shirt.id + '-size-guide-regular').fadeTo(fadeSpeed + 400, 1);
		$('#' + shirt.id + '-size-info').fadeTo(fadeSpeed + 500, 1);
		
		addSize(shirt, 'small');
		addSize(shirt, 'medium');
		addSize(shirt, 'large');
		
		$('#' + shirt.id + '-size-guide').addClass('button');
		if(shirt.id == 'bd') {
			$('#' + shirt.id + '-size-guide').click(function() {
				openSizeGuide(bobdylan);
			})
		} else if(shirt.id == 'bs') {
			$('#' + shirt.id + '-size-guide').click(function() {
				openSizeGuide(brucespringsteen);
			})
		}
	}
	
	function openSizeGuide(shirt) {
		var url;
		if(shirt.model == 'men') {
			url = 'http://www.americanapparel.net/sizing/default.asp?chart=mu.shirts';
		} else if(shirt.model == 'women') {
			url = 'http://www.americanapparel.net/sizing/default.asp?chart=womens.shirts';
		}
		window.open(url);
		return false;
	}
	
	function addSize(shirt, size) {
		$('#' + shirt.id + '-size-' + size).addClass('button');
		if(shirt.id == 'bd') {
			if(size == 'small') {
				$('#' + shirt.id + '-size-' + size).click(function() {
					changeSize(bobdylan, 'small');
				})
			} else if(size == 'medium') {
				$('#' + shirt.id + '-size-' + size).click(function() {
					changeSize(bobdylan, 'medium');
				})
			} else if(size == 'large') {
				$('#' + shirt.id + '-size-' + size).click(function() {
					changeSize(bobdylan, 'large');
				})
			}
		} else if(shirt.id == 'bs') {
			if(size == 'small') {
				$('#' + shirt.id + '-size-' + size).click(function() {
					changeSize(brucespringsteen, 'small');
				})
			} else if(size == 'medium') {
				$('#' + shirt.id + '-size-' + size).click(function() {
					changeSize(brucespringsteen, 'medium');
				})
			} else if(size == 'large') {
				$('#' + shirt.id + '-size-' + size).click(function() {
					changeSize(brucespringsteen, 'large');
				})
			}
		}
	}
	
	function removeSize(shirt, size) {
		$('#' + shirt.id + '-size-' + size).removeClass('button');
		$('#' + shirt.id + '-size-' + size).unbind('click');

	}
	
	function initCheckout(shirt) {
		$('#' + shirt.id + '-header-check-out').fadeTo(fadeSpeed, 1);

		$('#' + shirt.id + '-paypal').fadeTo(fadeSpeed + 100, 1);
		$('#' + shirt.id + '-payson').fadeTo(fadeSpeed + 100, 1);
		$('#' + shirt.id + '-or').fadeTo(fadeSpeed + 100, 1);

		$('#' + shirt.id + '-paypal').addClass('button');
		$('#' + shirt.id + '-payson').addClass('button');
		if(shirt.id == 'bd') {
			$('#' + shirt.id + "-paypal").click(function() {
				buyPaypal(bobdylan);
			});
			$('#' + shirt.id + "-payson").click(function() {
				buyPayson(bobdylan);
			});
		} else if(shirt.id == 'bs') {
			$('#' + shirt.id + "-paypal").click(function() {
				buyPaypal(brucespringsteen);
			});
			$('#' + shirt.id + "-payson").click(function() {
				buyPayson(brucespringsteen);
			});
		}
		
	}
	
	function changeModel(shirt, model) {
		if(shirt.model == '') {
			initSize(shirt);
		} else {
			$('#' + shirt.id + '-' + shirt.model + '-regular').fadeTo(fadeSpeed, 1);
			$('#' + shirt.id + '-' + shirt.model + '-selected').fadeTo(fadeSpeed, 0);
			addModel(shirt, shirt.model);
		}

		$('#' + shirt.id + '-' + model + '-regular').fadeTo(fadeSpeed, 0);
		$('#' + shirt.id + '-' + model + '-selected').fadeTo(fadeSpeed, 1);

		removeModel(shirt, model);
		shirt.model = model;
	}
	
	function changeSize(shirt, size) {
		if(shirt.size == '') {
			initCheckout(shirt);
		} else {
			$('#' + shirt.id + '-size-' + shirt.size + '-selected').fadeTo(fadeSpeed, 0);
			$('#' + shirt.id + '-size-' + shirt.size + '-regular').fadeTo(fadeSpeed, 1);
			addSize(shirt, shirt.size);
		}

		$('#' + shirt.id + '-size-' + size + '-selected').fadeTo(fadeSpeed, 1);
		$('#' + shirt.id + '-size-' + size + '-regular').fadeTo(fadeSpeed, 0);

		removeSize(shirt, size);
		shirt.size = size;
	} 

	function buyPaypal(shirt) {
		var id = {
			bd: {
				men: { small: 'N92E8HMF3XZDJ', medium: '6S9E5AE5SBA58', large: '2PUWUU8UKQH6G' },
				women: { small: 'DAVXYU5YC2KEL', medium: '2VZUET2EU79DU', large: 'N9W3NC39MC2AQ' }
			},
			bs: {
				men: { small: '3XRP9XAKCCZ3W', medium: '24TQA5YQ7KL94', large: 'XNZF4XL9KP3L4' },
				women: { small: 'KF7JHBVFWNJCJ', medium: 'BJNQXCGF9DAXG', large: '6BRCSCR4GGJ8J' }
			}
		};

		// set value and submit form
		$('#paypal_id').attr('value', id[shirt.id][shirt.model][shirt.size]);
		$('#paypal_form').submit();
	}
	
	function buyPayson(shirt) {
		var artist;
		if(shirt.id == 'bd') {
			artist = 'Bob+Dylan';
		} else if(shirt.id == 'bs') {
			artist = 'Bruce+Springsteen';
		}

		var model = convertFirstLetterToUpperCase(shirt.model);
		var size = convertFirstLetterToUpperCase(shirt.size);
		
		var url = 'https://www.payson.se//SendMoney/?De=' + artist + '+T-shirt+For+' + model + '+-+' + size + '+size&Se=order%40artiitii.se&Cost=450%2c00&ShippingAmount=50%2c00&Sp=1';
		window.location = url;
	}
	
	function convertFirstLetterToUpperCase(str) {
		str = str.match(/(^.)(.+)/);
		str = str[1].toUpperCase()  + str[2];
		return str;
	}
	
	function initialState() {
		var invisible = [
			'men-selected',
			'women-selected',
			'size-small-selected',
			'size-small-regular',
			'size-medium-selected',
			'size-medium-regular',
			'size-large-selected',
			'size-large-regular',
			'size-guide-regular'
		];
		for(var i = 0; i < invisible.length; i++) {
			$('#bd-' + invisible[i]).fadeTo(0, 0);
			$('#bs-' + invisible[i]).fadeTo(0, 0);
		}

		var dimmed = [
			'header-size',
			'header-check-out',
			'size-info',
			'paypal',
			'payson',
			'or'
		];
		for(var j = 0; j < dimmed.length; j++) {
			$('#bd-' + dimmed[j]).fadeTo(0, 0.3);
			$('#bs-' + dimmed[j]).fadeTo(0, 0.3);
		}
	}
	
	function initSlideshow() {
		$('#bd-slideshow').cycle({
			next: '#bd-next',
			prev: '#bd-prev',
			timeout: 0,
			ease: 'easinout',
			speed: 500
		});

		$('#bs-slideshow').cycle({
			next: '#bs-next',
			prev: '#bs-prev',
			timeout: 0,
			ease: 'easinout',
			speed: 500
		});
	}
	
	function initFooter() {
		$('#email').hover( function() {
				$('#email-out').fadeTo(fadeSpeed, 0);
				$('#email-over').fadeTo(fadeSpeed, 1);
			}, function() {
				$('#email-over').fadeTo(fadeSpeed, 0);
				$('#email-out').fadeTo(fadeSpeed, 1);
			}
		);

		$('#facebook').hover( function() {
				$('#facebook-greyscale').fadeTo(fadeSpeed, 0);
				$('#facebook-color').fadeTo(fadeSpeed, 1);
			}, function() {
				$('#facebook-color').fadeTo(fadeSpeed, 0);
				$('#facebook-greyscale').fadeTo(fadeSpeed, 1);
			}
		);

		$('#twitter').hover( function() {
				$('#twitter-greyscale').fadeTo(fadeSpeed, 0);
				$('#twitter-color').fadeTo(fadeSpeed, 1);
			}, function() {
				$('#twitter-color').fadeTo(fadeSpeed, 0);
				$('#twitter-greyscale').fadeTo(fadeSpeed, 1);
			}
		);
		
		

		$('#twitter-color').fadeTo(0, 0);
		$('#facebook-color').fadeTo(0, 0);
		$('#email-over').fadeTo(0, 0);
		
		
	}
	
	initialState();
	initModel();
	initSlideshow();
	initFooter();
	
});


