(function($) {

	function setBG() {
		var sw = $(window).width();
		var sh = $(window).height();
		var size = sw / sh;
		var wnum = sw / 1700;
		var hnum = sh / 1050;
		var neww = 1050 * wnum;
		var newh = 1700 * hnum;
		if (sw < 600) {
			$('#bgbox').hide();
		} else {
			$('#bgbox').show();
			$('#bgbox').css("height", sh + "px");
			if (size > 1.61904) {
				$("#bg").css({
					height: newh,
					width: sw + .1
				});
			} else {
				$("#bg").css({
					height: sh,
					left: 0 - ((neww - sw) / 2),
					width: neww
				});
			}
		}
	}

	$(window).load(function() {

		/* Old equal height column fix. Doesn't work with show/hide things in the columns
		var h = 0;
		$('.col-left, .col-main, .col-right').each(function(i, e) {
			h = ($(e).height() > h) ? $(e).height() : h;
		}).css({
			minHeight: h
		});
		*/

		if ($('#news').length > 0) {
			$('#news').mCustomScrollbar('vertical', 200, 'easeOutCirc', 1.25, 'fixed', 'yes', 'no', 0);
		}
	});

	$(function() {

		setBG();
		$(window).resize(function() {
			setBG();
		});

		//Back Button
		$('.back, .back-link a').bind('click', function(e) {
			if (document.referrer) {
				window.open(document.referrer, '_self');
			} else {
				history.go(-1);
			}
			e.preventDefault();
		});

		$('#autocompletesubmit').live('click', function (event) {
			$('#search_mini_form').submit();
			event.preventDefault();
		});

		$('#pdfbutton').click(function (event) {
			$(this).parents('form').attr('target', '_blank').submit();
			event.preventDefault();
		});

		// bit.ly
        $('#bitly').click(function(event) {
            var self = $(this);
            var position = self.position();
            $.ajax('http://api.bit.ly/v3/shorten', {
				cache: false,
				data: {
					apiKey: 'R_e4226fc0104399ea24d4e75467b7f622',
					login: 'prologis',
					uri: window.location.href
				},
				dataType: 'jsonp',
				success: function(data) {
					self.parents('li').find('.tooltip').css({
	                    left: position.left - 170,
	                    top: position.top - 15
	                }).show().find('input').val(data.data.url).end().fadeIn();
				}
            });
            event.preventDefault();
        });


		$('.selectall').live('focus', function () {
			$(this).select();
		});

		$('#closetooltip').live('click', function (event) {
			$('.tooltip').fadeOut();
			event.preventDefault();
		});

		$('.block-wishlist .block-content').first().show().end().last().hide();

		$('.block-wishlist .block-title').click(function(event) {
			$('.block-wishlist .block-content').slideToggle();
			if ($(this).hasClass('expanded')) {
				$(this).removeClass('expanded');
			} else {
				$(this).addClass('expanded');
			}
			event.preventDefault();
		});

		$('#map').click(function (event) {
			$('#map-enlarge').click();
			return false;
		});

		/*$('.block-infobox').hover(function() {
			$(this).show();
		}, function () {

		});*/

		// Tooltips
		$('a[data-title], .block-addtocart .button, .pdf-button').tooltip({
			bodyHandler: function () {
				return $(this).data('title') || '';
			},
			delay: 0,
			fade: 250,
			opacity: 1,
			showURL: false,
			top: -30
		});

		$('.block-downloads a').data('title', 'Click here to download this attachment to your computer.').tooltip({
			bodyHandler: function () {
				return $(this).data('title');
			},
			delay: 0,
			fade: 250,
			opacity: 1,
			showURL: false,
			top: -30
		});

		// Print Page
		$('#print').live('click', function(event) {
			if ($(this).parents('#cboxLoadedContent').length > 0) {
				window.open('/catalog/product_compare/index/');
			} else {
				window.print();
			}
			event.preventDefault();
		});

		if (new RegExp('/catalog/product_compare/index/').test(window.location.pathname)) {
			window.print();
			window.close();
		}

		// Remove from results
		var item = $('.item');
		$('.remove-from-list a').click(function(event) {
			var self = $(this);
			$.post(this.href, function() {
				self.parents('.item').fadeOut(function() {
					self.remove();
					item.removeClass('odd even last');
					decorateList('products-list', 'none-recursive');
				});
			});
			event.preventDefault();
		});

		// Map enlarger
		$('#map-enlarge').colorbox({
			html: '<div id="bigmap"></div>',
			onComplete: function() {
				initialize('bigmap');
			},
			onClosed: function () {
				initialize('map');
			},
			width: 720
		});

		// Product image enlarger
		$('.product-image-zoom a').colorbox({
			rel: 'colorbox'
		});

		$('.more-views a').click(function (event) {
			var index = $(this).index('.more-views a');
			$('.product-image-zoom').hide().filter(':eq(' + index + ')').show();
			event.preventDefault();
		});

		// Send Enquiry button
		$('#send-enquiry').colorbox({
			height: '95%',
			href: '/process.php',
			onComplete: function() {
				var enquiryForm = new VarienForm('enquire');
				if (!Modernizr.input.placeholder){
					$('input[placeholder]').focus(function() {
						var placeholder = $(this).attr('placeholder');
						if ($(this).val() === placeholder) {
							$(this).val('');
						}
					}).blur(function() {
						var placeholder = $(this).attr('placeholder');
						if ($(this).val() === '') {
							$(this).val(placeholder);
						}
					}).each(function(i, e) {
						var placeholder = $(e).attr('placeholder');
						$(e).val(placeholder);
					});
				}
			},
			width: 586
		});

		// Add to Enquiry
		$('#add-to-enquiry').click(function(event) {
			$('#product_addtocart_form').submit();
			event.preventDefault();
		});

		// Send to a friend

		var recipCount = 1;
		var maxRecip;
		var i = 0;

		$('#add-recipient').live('click', function(event) {
			var li_mail = '<li class="fields additional-row"><div class="field"><label for="recipients_name' + i + '" class="required"><em>*</em>Name:</label><div class="input-box"><input name="recipients[name][]" type="text" class="input-text required-entry" id="recipients_name' + i + '" /></div></div><div class="field"><a href="#" title="Remove Email" onclick="remove_recipient(' + i + '); return false" class="btn-remove">Remove Email"</a><label for="recipients_email' + i + '" class="required"><em>*</em>Email Address:</label><div class="input-box"><input name="recipients[email][]" value="" title="Email Address" id="recipients_email' + i + '" type="text" class="input-text required-entry validate-email" /></div></div></li>';
			i++;
			recipCount++;

			if(recipCount >= maxRecip && maxRecip !== 0) {
				$('#add_recipient_button').hide();
				$('#max_recipient_message').show();
			}
			$('#recipients_options').append(li_mail);
			event.preventDefault();
		});

		$('.send-friend .btn-remove').live('click', function(event) {
			$(this).parents('.fields').remove();
			recipCount--;
			if(recipCount < maxRecip && maxRecip !== 0) {
				$('#add_recipient_button').show();
				$('#max_recipient_message').hide();
			}
			event.preventDefault();
		});

		// Log in pop up
		$('a').live('click', function(event) {
			var url = this.href;
			if (new RegExp('/customer/account/login/').test(url)) {
				$.colorbox({
					href: url + ' .col-main',
					width: 586
				});
				event.preventDefault();
			} else if (new RegExp('/catalog/product_compare/index/').test(url)) {
				$.colorbox({
					height: '95%',
					href: url + ' .col-main',
					width: 1002
				});
				event.preventDefault();
			}
		});

		$('.compare-remove a').live('click', function (event) {
			$.colorbox({
				height: '95%',
				width: '900'
			});
			event.preventDefault();
		});

		// Layered navigation
		$('#narrow-by-list dt').click(function(event) {
			$(this).toggleClass('open').next('dd').slideToggle();
			event.preventDefault();
		});

		$('#narrow-by-list').find('dd:first, dt:first').addClass('first').end().find('dd:last, dt:last').addClass('last');

		// My Properties text box
		$('.comment').click(function(event) {
			$(this).parents('.item').find('.comment-box').slideToggle();
			event.preventDefault();
		});

		$('.comment-box textarea').bind('focus blur', function() {
			var self = $(this),
			val = self.val(),
			text = 'Please, enter your comments...';
			if (val === text) {
				self.val('');
			} else if (val === '') {
				self.val(text);
			}
		});

		// Product list description

		$('.read-more').click(function(event) {
			var self = $(this);
			self.prev('.more-info').slideToggle(function() {
				if ($(this).is(':visible')) {
					self.find('a').text('Read less').parents('p').removeClass('read-more').addClass('read-less');
				} else {
					self.find('a').text('Read more').parents('p').removeClass('read-less').addClass('read-more');
				}
			});
			event.preventDefault();
		});

		// Select Store
		$('#select-store').change(function() {
			window.location.assign($(this).val());
		});

		// Back to top
		$('#back-to-top a').click(function(event) {
			$('body, html').animate({
				scrollTop: 0
			});
			event.preventDefault();
		});

	});
}(this.jQuery));
