// Create Global Namespace for our properties and methods.
var BLIK = window.BLIK || {};

BLIK.setOverlayHeight = function(h) {
	//many thanks to lightbox2 for this:
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	//arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	//return arrayPageSize;
	$('overlay').style.height = pageHeight+'px';
}

BLIK.refreshSearchResults = function() {
	if ($('search-type-products').checked) {
		$('product-items').show();
	}
	else {
		$('product-items').hide();
	}

	if ($('search-type-posts').checked) {
		$('blog-items').show();
	}
	else {
		$('blog-items').hide();
	}

	if ($('search-type-pages').checked) {
		$('page-items').show();
	}
	else {
		$('page-items').hide();
	}

	if ($('search-type-all-types').checked) {
		$('product-items').show();
		$('blog-items').show();
		$('page-items').show();
	}

	if ($('search-type-all-themes').checked && $('search-type-all-colors').checked) {
		$$('ul#search-items>li.search-item').each(function(el){
			$(el).show();
			$(el).addClassName('visible');
		});
	}
	else {
		// find only the search results that match the checked colors and themes
		var showprods = [];
		var hideprods = [];
		$$('ul#search-items>li').each(function(el){
			$$('input.color-filter-checkbox').each(function(box){
				if ($(box).checked && $(box).value) {
					var attribs = ($(el).readAttribute('filter_color_group') || '').split(/\s+/);
					if (attribs.indexOf($(box).value) != -1) {
						showprods.push($(el));
					}
					else {
						hideprods.push($(el));
					}
				}
			});
			$$('input.theme-filter-checkbox').each(function(box){
				if ($(box).checked && $(box).value) {
					var attribs = ($(el).readAttribute('filter_theme') || '').split(/\s+/);
					if (attribs.indexOf($(box).value) != -1) {
						showprods.push($(el));
					}
					else {
						hideprods.push($(el));
					}
				}
			});
			$(el).hide();
			$(el).removeClassName('visible');
		});
		
		showprods.each(function(el){
			$(el).show();
			$(el).addClassName('visible');
		});
	}
	
	if ('undefined' != typeof BLIK.RM) { BLIK.RM.afterSearch(); }
}

BLIK.updateSearchSummary = function(search_summary) {
	var tmp = (search_summary.product_count*1)+' product';
	if ((search_summary.product_count*1)!=1) tmp += 's';
	tmp += ', ';
	tmp += (search_summary.blog_count*1)+' blog post';
	if ((search_summary.blog_count*1)!=1) tmp += 's';
	tmp += ', and ';
	tmp += (search_summary.page_count*1)+' page';
	if ((search_summary.page_count*1)!=1) tmp += 's';
	tmp += ' were found.';
	$('search-summary').innerHTML = tmp;
}

BLIK.addLoginLinkToError = function() {
	$$('ul.messages li.error-msg ul li').each(function(el) {
		if (el.innerHTML == 'Customer email already exists') {
			el.insert({bottom: '<a class="login-link" href="/shop/customer/account/login/">Log in here</a>'})
		}
	});
}
document.observe('dom:loaded', BLIK.addLoginLinkToError);


/**
 * Blik filters in Explorer area.
 */
BLIK.addFilterFromEl = function(el) {
	var params = $H(location.href.toQueryParams());
	var link = el.down('a[href]');

	// Build current query parameters
	var allLink = '';
	if ('undefined' != typeof params.get('limit')) {
		var limit = '&limit='+params.get('limit');
	} else {
		var limit = '';
	}
	$H(link.href.toQueryParams()).each(function(pair) {
		var oldValue = params.get(pair.key);
		if (pair.value=='' && oldValue!='' && oldValue.match(/^\d*$/)!=null) {
			allLink = '/shop/explore/?'+pair.key+'='+oldValue+limit;
		}
	});

	// Add info to filters list
	$('footer-filters-list').insert({bottom:'<li><a class="bttn-filter"" href="'+allLink+
		'">All '+link.innerHTML.stripTags()+'</a></li>'});

	// Show hidden elements when needed
	if (!$('filter-reset').visible()) {
		$('filter-reset').show();
	}
	if (!$('footer-filters').visible() && $$('#footer-filters-list li').length>1) {
		$('footer-filters').show();
	}
}
document.observe('dom:loaded', function() {
	if ($('filter')!=null && $('footer-filters')!=null) {
		$$('div#filter>ul>li>ul>li.here').each(BLIK.addFilterFromEl);
		$$('div#filter>ul>li.here.binary').each(BLIK.addFilterFromEl);
	}
});

/**
 * Activate the next/prev buttons in the customize section.
 *
 * My silly, silly windows
 */
BLIK.customize = {
	max: 11,
	
	rE: /([^\d]*)([\d]*)\.([a-zA-Z]{3})$/,
	advance: function(oof) {
		var src = $('customize-slideshow').down('img').getAttribute('src');
		var next = parseInt(src.match(BLIK.customize.rE)[2])+oof;
		next = next>BLIK.customize.max ? 1 : next<1 ? BLIK.customize.max : next;
		$('customize-slideshow').down('img').setAttribute('src', src.replace(BLIK.customize.rE, '$1'+next+'.$3'));
	},

	initialize: function() {
		if (!$('customize-slideshow') || location.href.indexOf('/customize')<0) return false;
	
		$('customize-slideshow').down('a.next').observe('click', function(e) {
			e.stop(); BLIK.customize.advance(1);
		});
		$('customize-slideshow').down('a.prev').observe('click', function(e) {
			e.stop(); BLIK.customize.advance(-1);
		});
	}
};
document.observe('dom:loaded', BLIK.customize.initialize);

// Fix for error regarding <object> tags and addthis widget
// http://addthis.com/forum/viewtopic.php?p=74537&sid=5129f82b452b6d8d0a9ca665fd742c6d#p74537
Object.extend(Function.prototype, {
	substr: function() {
		var __method = this;
		return function() { return ""; }
	}
});
