// Check to see if the function Exists
jQuery.fn.exists = function() {
	return jQuery(this).length > 0;
}

var subtractArray = function(a, b) {
	var result = [], found;
	for (var i = 0; i < a.length; i++) {
		found = false;
		// find a[i] in b
		for (var j = 0; j < b.length; j++) {
			if (a[i] == b[j]) {
				found = true;
				break;
			}
		}
		if (!found) {
			result.push(a[i]);
		}
	}

	return result;
}

$(document).ready(function(){
	set_newsletter_placeholders();
	
	init_placeholder();

	//store items in fancybox...
	init_fancybox($("#thumbView a"));

	init_home();

	init_blog();

	$(window).resize(calc_fancybox_sizes);


	var sticky_stuff = function() {
		// stickRight('#menu_wrap .menu_cart', 35);
		// stickLeft('#banner', 0);	
		// stickLeft('#menu_wrap', 0);	
		stickRight('#footer_wrap .right', 35);	
		stickLeft('#footer_wrap .left', 35);	
	}

	sticky_stuff();

	$(window).resize(sticky_stuff);
	$(window).scroll(sticky_stuff);

	/*
		CART
		Fade the Flash Message In / Out 
	*/
	if ($("div.flash_message").exists()) {
		if($.trim($("div.flash_message").text()) == "") {
			$(".flash_message").remove();
		} else {
			$('div.flash_message').hide().fadeIn("slow");
			//$('div.flash_message').animate({opacity: 1.0}, 5000).fadeOut("slow");
		}
	}
});

var set_newsletter_placeholders = function() {
	$("#StoreSubscriberFirstName").attr('placeholder', 'First Name');
	$("#StoreSubscriberLastName").attr('placeholder', 'Last Name');
	$("#StoreSubscriberEmail").attr('placeholder', 'Email');
}

var stickRight = function(selectors, rightPadding) {
	var width = $(selectors).width();
	var minWidth = parseInt($("body").css("min-width"), 10);

	$(selectors).each(function() {
		if($(window).width() < minWidth) {
			$(this).css({
				left: minWidth - $(window).scrollLeft() - $(this).width() - rightPadding - 1,
				right: "auto"
			});
		} else {
			$(this).css({
				left: "auto",
				right: rightPadding
			});
		}

		if(typeof(width) != 'undefined')
			$(this).width(width);
	});	 // each
}; // stickRight

var stickLeft = function(selectors, leftPadding) {
	var width = $(selectors).width();
	var minWidth = parseFloat($("body").css("min-width"));
	$(selectors).each(function() {
		if($(window).width() < minWidth) {
			$(this).css({
				left: -($(window).scrollLeft() - leftPadding),
				right: "auto"
			});
		} else {
			$(this).css({
				left: leftPadding,
				right: "auto"
			});
		}
			
		if(typeof(width) != 'undefined')
			$(this).width(width);
	});	// each
}; // stickLeft

function init_home() {
	var img = $('.home .container img');

	var calc_image_size = function() {
		var ww = window.innerWidth;
		var wh = window.innerHeight;
	
		if (ww / wh < 1920 / 1440) {
			img.css({
				width: 'auto',
				height: '100%'
			});
		} else {
			img.css({
				width: '100%',
				height: 'auto'
			});
		}
	}

	calc_image_size();

	$(window).resize(calc_image_size);
}

function init_blog() {
	var images = $('#blogList .post img');

	images.css('width', '660px');
	// var hi_images = images.map(function(i, v) {
	// 	var width = parseInt($(v).attr('width'), 10);

	// 	if (width > 360) {
	// 		return v;
	// 	}
	// });

	// hi_images.parent().css({width: '100%'});

	// images = images.toArray();
	// hi_images = hi_images.toArray();

	// lo_images = subtractArray(images, hi_images);

	// $(lo_images).css('width', 'auto !important');
}

function init_placeholder() {
	$('[placeholder]').focus(function() {
		var input = $(this);
		if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		}
	}).blur(function() {
		var input = $(this);
		if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		}
	}).blur();

}

function calc_fancybox_sizes() {
	var margin = 158 + 24 + 20; // header height + footer height + bottom margin
	var overlayHeight = document.documentElement.scrollHeight - margin;
	var innerHeight = window.innerHeight - margin;
	var minHeight = parseInt($("body").css('minHeight'), 10) - margin;
	var minWidth = parseInt($("body").css('minWidth'), 10);
	var overlayWidth = window.innerWidth;

	if (isNaN(minWidth) || minWidth < 0) {
		minWidth = 960;
	}

	if (isNaN(minHeight) || minHeight < 0) {
		minHeight = 320;
	}

	if (overlayWidth < minWidth) {
		overlayWidth = minWidth;
	}

	if (innerHeight < minHeight) {
		innerHeight = minHeight;
	}

	if (typeof document.styleSheets[1].cssRules !== 'undefined' && document.styleSheets[1].cssRules != null) {
		document.styleSheets[1].cssRules[0].style.cssText = 'height: ' + overlayHeight + 'px !important';
		$('#fancybox-overlay').addClass('fancybox-dynamic-overlay');
	} else if (typeof document.styleSheets[1].rules !== 'undefined' && document.styleSheets[1].rules != null) {
		document.styleSheets[1].rules[0].style.cssText = 'height: ' + overlayHeight + 'px !important';
		$('#fancybox-overlay').addClass('fancybox-dynamic-overlay');
	} else {
		$('#fancybox-overlay').height(overlayHeight);
		$('#fancybox-overlay').css('height', overlayHeight + 'px !important');		
	}

	$('#fancybox-overlay').css('width', overlayWidth + 'px !important');

	$('#fancybox-content').height(innerHeight);
	$('#fancybox-content').css('height', innerHeight + 'px !important');

	set_image_width();
}

function init_close_behaviour() {
	$("#body_wrap, #footer, #page_navigation #close").click(function() {
		$.fancybox.close();
	});
}

function init_fancybox(obj) {
	$(obj).not('.no-fancybox').fancybox({ 
		'overlayShow': true,
		'width' : '100%',
		'height': '100%',
		'speedIn'    :600,
   		'speedOut'   :200,
   		'centerOnScroll': false,
		'transitionIn': 'fade',
		'padding': 0,
		'overlayOpacity': 0.95,
		'titleShow': false,
		onStart: function() {
			calc_fancybox_sizes();
		},
		onComplete: function() {
			if (typeof init_cart != 'undefined') {
				init_cart();
			}

			init_close_behaviour();
			set_image_width();

			$('#item_img').cycle({ 
				fx:     'fade', 
				speed:  'fast', 
				timeout: 0, 
				slideResize: 0,
				next:   '#page_navigation #next', 
				prev:   '#page_navigation #prev',
				after: function(oldSlideElement, newSlideElement) {
					calc_fancybox_sizes();

					if ($('body').hasClass('press')) {
						$(newSlideElement).click(function() {
							return false;
						}).css('cursor', 'default');
					} else {
						$(newSlideElement).CloudZoom();
					}
					$(newSlideElement).parent().css('z-index', 9999);
					$(oldSlideElement).parent().css('z-index', 1000);

					new_i = parseInt($('img.item_image', newSlideElement).attr('data-i'), 10);
					old_i = parseInt($('img.item_image', oldSlideElement).attr('data-i'), 10);

					if (!isNaN(old_i)) {
						$('#image_' + old_i).fadeOut('fast');
					}

					if (!isNaN(new_i)) {
						new_description = $('#image_' + new_i).fadeIn('fast');

						init_fancybox($('a', new_description));
					}
				}
			});
		}
	});

	$('#header_wrap').click(function() {
		$.fancybox.close();
	});
}

function set_image_width() {
	var img = $('#fancybox-content #item_img img:visible:first');
	var margin_left = img.width();
	if(margin_left) {
		$('#item_description').css('margin-left', margin_left);
		img.parent().width(img.width());

		//update cloudzoom wrap...
		var wrap = $('.mousetrap');
		wrap.css({width: margin_left, height: img.height()});
	}
}

function on_first_store_image_load(img) {
	$(img).show('slow', function() {
		set_image_width();
		$('#fancybox-content #item_img img').show('slow', function() {
			$('#item_description').fadeIn();
		});
	});
}

function display_img(thumb_src, full_src, target, index) {

	$('.altimg').addClass("altimg").removeClass("active");
	$('#alt_img_' + index).addClass("altimg active");
	
	$('#' + target + ' > a').attr({
		"href" : full_src
	});
	
	$('#' + target + ' > a > img').attr({
		"src" : thumb_src
	});
}


function ucc_submit() {
	if ($('#uccTo').val() != '') {
		$('#ucc_form').submit();
	}
}


function ucc_toggle() {
	$('#uccTo').val('');
	$('#uccToDiv').toggle('fast');
}

