﻿var MagicRecords = {
	preferences: {
		
	},

	start: function() {
		this.tweakLayout();
		this.addFunctionality();
		this.setSliderWidth();
		//this.changeButtons();
	},
	
	
	tweakLayout: function() {
		$('html').addClass('js');
		
	
		var global = this;
		$('input[alt]').each(function() {
			var width = this.alt;
			var s = width.split('.');
			if (s.length == 2) {
				$(this).css({
					'width': s[0] + '%',
					'margin-right': s[1] + '%'
				})
			}
		});
		
		if ($.browser.msie) {
			$('input').each(function() {
				if (this.type == 'text') {
					$(this).addClass('text');
				}
				else if (this.type == 'password') {
					$(this).addClass('password');
				}
			});
		}
		
		// Fix png links in IE6
		if ($.browser.msie) {
			
			// logo
			if (document.getElementById('name')) {
				if ($('#name a').length) {
					var href = $('#name a:first').attr('href');
					$('#top').append('<a href="' + href + '" id="logo-fix"></a>');
				}
			}
			
		}
		
		// Set .image .header background opsacity
		$('.section > .image > .title').each(function() {
			var $this = $(this);
			var content = $this.html();
			$this.before('<span class="title-bg">' + content + '</span>');
		});
		
	},
		
	
	changeButtons: function() {
		$('input.submit').each(function() {
			var $submit = $(this);
			var val = $submit.attr('value');
			$submit.after('<a class="submit">' + val + '</a>');
			$submit.next().click(function() {
				$submit.click();
				return false;
			});
			$submit.hide();
		});
	},
	
	setSliderWidth: function() {
		$('#slider ul').each(function() {
			var $this = $(this);
			var w = 0;
			$this.find('li').each(function() {
				w += $(this).width() + 1;
			});
			$this.width(w);
		});
		setTimeout(function() {
			if ($('#slider').width() > $('#slider > ul').width()) {
				$('#slider ul').each(function() {
					var $this = $(this);
					var w = 0;
					$this.find('li').each(function() {
						w += $(this).width() + 1;
					});
					$this.width(w);
				});
			}
		}, 100);
	},
	
	addFunctionality: function() {
		// Newsletter
		$('#newsletter').find('.field input').focus(function() {
			$this = $(this);
			$this.val('');
			$this.unbind('focus');
		}).end()
		.find('.buttons').append('<a href="#" class="add">Dodaj</a><a href="#" class="substract">Wypisz</a>')
			.find('a.add').click(function() {
				$('#newsletter_add').attr('checked', 'checked');
				$('#newsletter').submit();
				return false;
			}).end()
			.find('a.substract').click(function() {
				$('#newsletter_substract').attr('checked', 'checked');
				$('#newsletter').submit();
				return false;
			});
			
		// Search
		$('#search').find('.field input').focus(function() {
			$this = $(this);
			$this.val('');
			$this.unbind('focus');
		}).end()
		.find('.buttons').append('<a href="#">Szukaj</a>').find('a').click(function() {
			$('#search').submit();
			return false;
		});
		
	}

}

$(function() {
	MagicRecords.start();
})