//Page init scripts
jQuery(function(){
	initSmoothNav();
	initBGStrech();
	initSlider();
	initAjaxTab();
});

/* initBGStrech */
function initBGStrech(){
	var holder = document.getElementById('bg-scale');
	var images = holder.getElementsByTagName('img');
	for(var i = 0; i < images.length; i++) {
		backgroundStretcher.stretchImage(images[i]);
	};
	backgroundStretcher.setBgHolder(holder.getElementsByTagName('div')[0]);
};

/* popupLink */
function popupLink(){
	var activeClass = 'active';
	jQuery('a.popup-link').each(function(){
		if(jQuery(jQuery(this).attr('href').substr(jQuery(this).attr('href').lastIndexOf('#'))).length){
			jQuery(jQuery(this).attr('href').substr(jQuery(this).attr('href').lastIndexOf('#'))).hide();
		};
		jQuery(this).unbind('click').bind('click', function(){
			if(jQuery('a.popup-link').filter('.' + activeClass + ':eq(0)').length){
				jQuery(jQuery('a.popup-link').filter('.' + activeClass).attr('href').substr(jQuery('a.popup-link').filter('.' + activeClass).attr('href').lastIndexOf('#'))).hide();
				jQuery('a.popup-link').filter('.' + activeClass).removeClass(activeClass);
			};
			jQuery(this).addClass(activeClass);
			jQuery(this.href.substr(this.href.lastIndexOf('#'))).css({ visibility: 'visible' }).show();
			jQuery(this.href.substr(this.href.lastIndexOf('#'))).find('.close').unbind('click').bind('click', function(){
				jQuery(jQuery('a.popup-link').filter('.' + activeClass).attr('href').substr(jQuery('a.popup-link').filter('.' + activeClass).attr('href').lastIndexOf('#'))).hide();
				jQuery('a.popup-link').filter('.' + activeClass).removeClass(activeClass);
				return false;
			});
			return false;
		});
	});
};

/* refreshLink */
function refreshLink(){
	jQuery('.tab-holder a').each(function(){
		if(this.load) return; else this.load = true;
		if(this.rel){
			if(jQuery('.ajax-tab').data('AjaxTab') && jQuery('.ajax-tab').data('AjaxTab').navLink.eq(this.rel)){
				jQuery(this).bind('mouseenter', function(){
					if(jQuery('.main-nav').data('smoothNav')) jQuery('.main-nav').data('smoothNav').moveDown(this.rel);
				}).bind('mouseleave', function(){
					if(jQuery('.main-nav').data('smoothNav')) jQuery('.main-nav').data('smoothNav').moveUp();
				}).bind('click', function(){
					if(jQuery('.ajax-tab').data('AjaxTab')) jQuery('.ajax-tab').data('AjaxTab').navLink.eq(this.rel).trigger('click');
					return false;
				});
			};
		};
	});
};

/* initAjaxTab */
function initAjaxTab(){
	jQuery('.ajax-tab').each(function(){
		if(this.load) return; else this.load = true;
		new AjaxTab(jQuery(this),{
			delay: 650,
			afterAjaxRequest: function(){
				refreshLink();
				popupLink();
			},
			afterClose: function(obj){
				MainSlider.currentElement = obj.currentItem;
				MainSlider.swichFunction(MainSlider.currentElement);
				obj.navLink.parent().removeClass(obj.options.activeClass);
				if(jQuery('#bg-scale').data('ResizeImg')) jQuery('#bg-scale').data('ResizeImg').resizeImage(jQuery('#bg-scale').find('img').eq(obj.currentItem).get(0));
				backgroundStretcher.setBgHolder(MainSlider.set.children().get(0));
			},
			afterOpen: function(obj){
				if(jQuery('#bg-scale').data('ResizeImg')) jQuery('#bg-scale').data('ResizeImg').resizeImage(jQuery('#bg-scale').find('img').eq(obj.currentItem).get(0));
				backgroundStretcher.setBgHolder(MainSlider.set.children().get(0));
				obj.navLink.eq(obj.currentItem).parent().addClass(obj.options.activeClass);
				if(jQuery('.main-nav').data('smoothNav')) jQuery('.main-nav').data('smoothNav').moveUp();
			}
		});
	});
};

/* initAjaxTab module */
(function($){
	AjaxTab = function(){ this.init.apply(this,arguments) };
	AjaxTab.prototype = {
		init: function(context, options){
			this.options = jQuery.extend({
				tabHolder: '>.tab-holder',
				navLink: '.main-nav a',
				activeClass: 'active',
				loadingClass: 'loading',
				autoHeight: true,
				loadingHeight: 500,
				animSpeed: 250,
				startSlide: 0,
				delay: 0,
				beforeInit: function(){},
				afterInit: function(){},
				afterAjaxRequest: function(){},
				beforeChange: function(){},
				afterClose: function(){},
				afterOpen: function(){}
			}, options);
			if(!context || !context.length) return;
			this.set = context;
			if(typeof this.options.beforeInit === 'function') this.options.beforeInit(this);
			this.currentElement = this.options.startSlide;
			this.elementInit();
			this.eventInit();
			this.startPosition(this.currentElement);
			if(typeof this.options.afterInit === 'function') this.options.afterInit(this);
		},
		elementInit: function(){
			this.tabHolder = jQuery(this.options.tabHolder, this.set);
			this.navLink = jQuery(this.options.navLink, this.set);
		},
		eventInit: function(){
			this.navLink.each(jQuery.proxy(function(idx, link){
				link.href += ('#tab'+idx);
				jQuery(link).bind('click', jQuery.proxy(function(){
					if(!jQuery(link).hasClass(this.options.activeClass)){
						this.currentItem = idx;
						this.navLink.removeClass(this.options.activeClass);
						if(jQuery(link).hasClass(this.options.loadingClass)){
							this.currTab = jQuery(link.href.substr(link.href.lastIndexOf('#')));
							this.swichFunction();
							this.navLink.eq(this.currentItem).addClass(this.options.activeClass);
						}
						else {
							this.ajaxRequest(link.href.substr(0, link.href.lastIndexOf('#')));
						};
					};
					return false;
				},this));
			},this));
			
		},
		ajaxRequest: function(currHref){
			jQuery.ajax({
				url: currHref,
				dataType: 'html',
				success: jQuery.proxy(function(data, textStatus, jqXHR){
					this.navLink.eq(this.currentItem).addClass(this.options.loadingClass);
					var t = jQuery('<div id="tab' + this.currentItem + '">' + data + '</div>').css({ width: '100%', overflow: 'hidden', position: 'absolute', bottom: 0, left: 0 });
					this.tabHolder.append(t);
					t.data('height', t.height());
					t.data('width', t.width());
					t.hide();
					this.currTab = jQuery(this.navLink.eq(this.currentItem).attr('href').substr(this.navLink.eq(this.currentItem).attr('href').lastIndexOf('#')));
					this.swichFunction();
					this.navLink.eq(this.currentItem).addClass(this.options.activeClass);
					this.options.afterAjaxRequest(this);
				},this),
				error: function(){
					alert('Error ajax load');
				}
			});
			return false;
		},
		swichFunction: function(){
			this.options.beforeChange(this);
			this.tabHolder.children().filter(':visible').animate({ height: 0 }, { queue: false, duration: this.options.animSpeed, complete: jQuery.proxy(function(){
				this.options.afterClose(this);
				this.tabHolder.animate({ height: this.currTab.data('height') }, { queue: false, duration: 100 });
				clearTimeout(this.delayTime);
				this.delayTime = setTimeout(jQuery.proxy(function(){
					this.currTab
					.css({
						display: 'block',
						bottom: -this.currTab.data('height'),
						left: this.currTab.data('width')/this.navLink.length*this.currentItem
					})
					.animate({
						bottom: 0,
						left: 0,
						height: this.currTab.data('height')
					}, { queue: false, duration: this.options.animSpeed, complete: jQuery.proxy(function(){
							this.options.afterOpen(this);
					},this)});
				},this), this.options.delay);
				this.set.data('AjaxTab', this);
			},this)});
			if(!this.tabHolder.children().filter(':visible').length){
				this.tabHolder.animate({ height: this.currTab.data('height') }, { queue: false, duration: 100 });
				clearTimeout(this.delayTime);
				this.delayTime = setTimeout(jQuery.proxy(function(){
					this.currTab
					.css({
						display: 'block',
						bottom: -this.currTab.data('height'),
						left: this.currTab.data('width')/this.navLink.length*this.currentItem
					})
					.animate({
						bottom: 0,
						left: 0,
						height: this.currTab.data('height')
					}, { queue: false, duration: this.options.animSpeed, complete: jQuery.proxy(function(){
							this.options.afterOpen(this);
					},this)});
				},this), this.options.delay);
				this.set.data('AjaxTab', this);
			}
		},
		startPosition: function(){
			if(this.navLink.parent().filter('.'+ this.options.activeClass).length){
				this.currentItem = this.navLink.parent().filter('.'+ this.options.activeClass + ':eq(0)').index();
				this.ajaxRequest(this.navLink.eq(this.currentItem).attr('href').substr(0, this.navLink.eq(this.currentItem).attr('href').lastIndexOf('#')));
			}
			this.set.data('AjaxTab', this);
		}
	};
})( jQuery );

function initSmoothNav(){
	jQuery('.main-nav').each(function(){
		new SmoothNav(jQuery(this));
	});
};
(function($){
	SmoothNav = function(){ this.init.apply(this,arguments) };
	SmoothNav.prototype = {
		init: function(context, options){
			this.options = jQuery.extend({
				item: '>li',
				animSpeed: 200,
				step: 10,
				activeClass: 'active',
				beforeAnimation: function(){},
				afterAnimation: function(){}
			}, options);
			if(!context || !context.length) return;
			this.set = context;
			if(typeof this.options.beforeInit === 'function') this.options.beforeInit(this);
			this.currentElement = this.options.startSlide;
			this.elementInit();
			this.eventInit();
			this.startPosition(this.currentElement);
			if(typeof this.options.afterInit === 'function') this.options.afterInit(this);
		},
		elementInit: function(){
			this.item = jQuery(this.options.item, this.set);
			this.set.data('smoothNav', this);
		},
		eventInit: function(){
			this.item.each(jQuery.proxy(function(idx, link){
				jQuery(link).bind('mouseenter', jQuery.proxy(function(){
					this.moveDown(idx);
				}, this)).bind('mouseleave', jQuery.proxy(function(){
					this.moveUp();
				}, this));
			},this));
		},
		moveDown: function(curr){
			this.item.eq(curr).find('>a').animate({ marginTop: this.options.step, marginBottom: -this.options.step }, { queue: false, duration: this.options.animSpeed });
		},
		moveUp: function(){
			this.item.each(jQuery.proxy(function(idx, elem){
				if(!jQuery(elem).hasClass(this.options.activeClass)){
					jQuery(elem).find('>a').animate({ marginTop: 0, marginBottom: 0 }, { queue: false, duration: this.options.animSpeed });
				}
				else {
					jQuery(elem).find('>a').animate({ marginTop: this.options.step, marginBottom: -this.options.step }, { queue: false, duration: this.options.animSpeed });
				}
			},this));
			
		},
		startPosition: function(){
			
		}
	};
})( jQuery );

/* initSlider */
function initSlider(){
	MainSlider = new SliderNV(jQuery('#bg-scale'),{
		slider:'>div',
		slides: '>img',
		animSpeed:650,
		effect: false
	});
};
/* slider module */
(function($){
	SliderNV = function(){
		this.init.apply(this,arguments)
	}
	SliderNV.prototype = {
		init: function(context, options){
			/* default options */
			this.options = jQuery.extend({
				sliderHolder: '>div',
				slider:'>ul',
				slides: '>li',
				pagerLinks:'div.pager a',
				generatePagination: false,
				generatePaginationMarkup: '<li><a href="#">&amp;nbsp;</a></li>',
				btnPrev:'a.btn-prev111',
				btnNext:'a.btn-next111',
				activeClass:'active',
				disabledClass:'disabled',
				circleSlide: true,
				effect: true, // true == slide effect & false == fade effect
				pauseClass:'gallery-paused',
				pauseOnHover:true,
				autoRotation:false,
				switchTime:5000,
				animSpeed:650,
				easing:'swing',
				pagerEvent: 'click',
				butttonEvent:'click',
				beforeInit: function(){},
				afterInit:function(){},
				beforeAnimation: function(){},
				afterAnimation: function(){},
				vertical:false,
				reverse: false,
				step: false,
				startElement: 0,
				autoHeight: false
			},options);
			if(!context) return;
			this.set = context;
			if(typeof this.options.beforeInit === 'function') this.options.beforeInit(this);
			this.elementInit();
			this.eventInit();
			this.startPosition(this.currentElement);
			if(typeof this.options.afterInit === 'function') this.options.afterInit(this);
			this.set.data('sliderNV', this);
		},
		elementInit: function(){
			this.sliderHolder = jQuery(this.options.sliderHolder, this.set);
			this.slider = jQuery(this.options.slider, this.set);
			this.slides = jQuery(this.options.slides, this.slider);
			this.reCalc();
			this.currentElement = this.prevElement = this.options.startElement || 0;
			this.pagination();
			this.stepCount = this.slides.length;
			this.autoSlide();
		},
		reCalc: function(){
			this.sumHeight = 0;
			this.sumWidth = 0;
			this.slides.each(jQuery.proxy(function(i, elem){
				this.sumHeight += jQuery(elem).outerHeight(true);
				this.sumWidth += jQuery(elem).outerWidth(true);
			},this));
		},
		eventInit: function(){
			this.set.find(this.options.btnPrev).unbind(this.options.butttonEvent).bind(this.options.butttonEvent, jQuery.proxy(function(){
				this.prev();
				return false;
			}, this));
			this.set.find(this.options.btnNext).unbind(this.options.butttonEvent).bind(this.options.butttonEvent, jQuery.proxy(function(){
				this.next();
				return false;
			}, this));
			if(this.options.pauseOnHover){
				this.set.bind('mouseenter', jQuery.proxy(function(){
					this.set.addClass(this.options.pauseClass);
					clearTimeout(this.autoTimer);
				}, this)).bind('mouseleave',jQuery.proxy(function(){
					this.set.removeClass(this.options.pauseClass);
					this.autoSlide();
				}, this));
			};
		},
		pagination: function(){
			if(this.options.generatePagination) {
				this.set.find(this.options.generatePagination).empty()
				this.slides.each(jQuery.proxy(function(){
					var temp = jQuery(this.options.generatePaginationMarkup);
					this.set.find(this.options.generatePagination).append(temp);
				},this));
			};
			this.pagerLinks = jQuery(this.options.pagerLinks, this.set);
			if(this.pagerLinks){
				this.pagerLinks.each(jQuery.proxy(function(idx, elem){
					jQuery(elem).unbind(this.options.pagerEvent).bind(this.options.pagerEvent, jQuery.proxy(function(){
						if(!jQuery(elem).hasClass(this.options.activeClass)){
							this.prevElement = this.currentElement;
							this.currentElement = idx;
							this.swichFunction(this.currentElement);
							this.set.data('sliderNV', this);
						};
						return false;
					},this));
				},this));
			};
		},
		prev: function(){
			this.prevElement = this.currentElement;
			if(this.currentElement > 0){
				this.currentElement--;
			}
			else if(this.options.circleSlide) {
				this.currentElement = this.stepCount-1;
			};
			this.swichFunction(this.currentElement);
		},
		next: function(){
			this.prevElement = this.currentElement;
			if(this.currentElement < this.stepCount-1){
				this.currentElement++;
			}
			else if(this.options.circleSlide) {
				this.currentElement = 0;
			};
			this.swichFunction(this.currentElement);
		},
		disableButton: function(){
			if(!this.options.circleSlide) {
				this.set.find(this.options.btnPrev).removeClass(this.options.disabledClass);
				this.set.find(this.options.btnNext).removeClass(this.options.disabledClass);
				if(this.currentElement == 0){
					this.set.find(this.options.btnPrev).addClass(this.options.disabledClass);
				}
				else if(this.currentElement == this.stepCount-1){
					this.set.find(this.options.btnNext).addClass(this.options.disabledClass);
				};
			};
		},
		recalcOffsets: function(curr){
			if(this.options.vertical) {
				/* slide vertical */
				if(this.options.step) {
					this.stepHeight = this.slides.eq(curr).outerHeight(true)*parseInt(this.options.step);
					this.stepCount = Math.ceil((this.sumHeight-this.sliderHolder.height())/this.stepHeight)+1;
					this.offset = -this.stepHeight*curr;
				} else {
					this.stepHeight = this.sliderHolder.height();
					this.stepCount = Math.ceil(this.sumHeight/this.stepHeight);
					this.offset = -this.stepHeight*curr;
					if(this.offset < this.stepHeight-this.sumHeight) this.offset = this.stepHeight-this.sumHeight;
				}
			}
			else {
				/* slide horizontal */
				if(this.options.step) {
					this.stepWidth = this.slides.eq(curr).outerWidth(true)*parseInt(this.options.step);
					this.stepCount = Math.ceil((this.sumWidth-this.sliderHolder.width())/this.stepWidth)+1;
					this.offset = -this.stepWidth*curr;
					if(this.offset < this.sliderHolder.width()-this.sumWidth) this.offset = this.sliderHolder.width()-this.sumWidth;
				} else {
					this.stepWidth = this.sliderHolder.width();
					this.stepCount = Math.ceil(this.sumWidth/this.stepWidth);
					this.offset = -this.stepWidth*curr;
					if(this.offset < this.stepWidth-this.sumWidth) this.offset = this.stepWidth-this.sumWidth;
				}
			};
		},
		swichFunction: function(curr){
			if(this.prevElement != curr){
				this.prevElement == curr;
				if(!this.options.effect){
					/* fade effect */
					if(typeof this.options.beforeAnimation ==='function') this.options.beforeAnimation(this);
					this.slides.filter(':visible').stop(false, true).fadeOut(this.options.animSpeed);
					this.slides.eq(curr).stop(false, true).fadeIn(this.options.animSpeed, jQuery.proxy(function(){
						if(typeof this.options.afterAnimation ==='function') this.options.afterAnimation(this);
						this.autoSlide();
						if(this.options.autoHeight) this.slider.css({ position: 'relative', height: this.slides.eq(this.currentElement).outerHeight(true) });
						this.set.data('sliderNV', this);
					},this));
				}
				else {
					/* slide effect */
					var marginType = {};
					this.recalcOffsets(curr);
					marginType[this.options.vertical ? 'marginTop' : 'marginLeft'] = this.offset;
					if(typeof this.options.beforeAnimation ==='function') this.options.beforeAnimation(this);
					this.slider.animate(
						marginType,
						{
							queue: false,
							duration: this.options.animSpeed,
							complete: jQuery.proxy(function(){
								if(typeof this.options.afterAnimation ==='function') this.options.afterAnimation(this);
								this.autoSlide();
								this.set.data('sliderNV', this);
							},this)
						}
					);
				};
				this.pagerLinks.removeClass(this.options.activeClass).eq(this.currentElement).addClass(this.options.activeClass);
				if(!this.options.circleSlide) this.disableButton();
				this.set.data('sliderNV', this);
			};
		},
		startPosition: function(curr){
			if(this.options.effect) {
				var marginType = {};
				this.recalcOffsets(curr);
				marginType[this.options.vertical ? 'marginTop' : 'marginLeft'] = this.offset;
				this.slider.css(marginType);
			}
			else {
				this.slides.css({ position: 'absolute', top: 0, left: 0 });
				this.slider.css({ position: 'relative' });
				if(this.options.autoHeight) this.slider.css({ position: 'relative', height: this.slides.eq(this.currentElement).outerHeight(true) });
				this.slides.hide().eq(this.currentElement).show();
			};
			this.disableButton();
			this.pagerLinks.removeClass(this.options.activeClass).eq(this.currentElement).addClass(this.options.activeClass);
		},
		autoSlide: function(){
			if(this.options.autoRotation && !this.set.hasClass(this.options.pauseClass)){
				if(this.autoTimer) clearTimeout(this.autoTimer);
				this.autoTimer = setTimeout(jQuery.proxy(function(){
					if(this.options.reverse) {
						this.prev();
					}
					else {
						this.next();
					}
				},this), this.options.switchTime + this.options.animSpeed);
			}
		}
	};
})( jQuery );


// mobile browsers detect
browserPlatform = {
	platforms: [
		{ uaString:['symbian','midp'], cssFile:'symbian.css' }, // Symbian phones
		{ uaString:['opera','mobi'], cssFile:'opera.css' }, // Opera Mobile
		{ uaString:['msie','ppc'], cssFile:'ieppc.css' }, // IE Mobile <6
		{ uaString:'iemobile', cssFile:'iemobile.css' }, // IE Mobile 6+
		{ uaString:'webos', cssFile:'webos.css' }, // Palm WebOS
		{ uaString:'Android', cssFile:'android.css' }, // Android
		{ uaString:['BlackBerry','/6.0','mobi'], cssFile:'blackberry6.css' },	// Blackberry 6
		{ uaString:['BlackBerry','/7.0','mobi'], cssFile:'blackberry7.css' },	// Blackberry 7+
		{ uaString:'ipad', cssFile:'ipad.css', miscHead:'<meta name="viewport" content="width=device-width" />' }, // iPad
		{ uaString:['safari','mobi'], cssFile:'safari.css', miscHead:'<meta name="viewport" content="width=device-width" />' } // iPhone and other webkit browsers
	],
	options: {
		cssPath:'css/',
		mobileCSS:'allmobile.css'
	},
	init:function(){
		this.checkMobile();
		this.parsePlatforms();
		return this;
	},
	checkMobile: function() {
		if(this.uaMatch('mobi') || this.uaMatch('midp') || this.uaMatch('ppc') || this.uaMatch('webos')) {
			this.attachStyles({cssFile:this.options.mobileCSS});
		}
	},
	parsePlatforms: function() {
		for(var i = 0; i < this.platforms.length; i++) {
			if(typeof this.platforms[i].uaString === 'string') {
				if(this.uaMatch(this.platforms[i].uaString)) {
					this.attachStyles(this.platforms[i]);
					break;
				}
			} else {
				for(var j = 0, allMatch = true; j < this.platforms[i].uaString.length; j++) {
					if(!this.uaMatch(this.platforms[i].uaString[j])) {
						allMatch = false;
					}
				}
				if(allMatch) {
					this.attachStyles(this.platforms[i]);
					break;
				}
			}
		}
	},
	attachStyles: function(platform) {
		var head = document.getElementsByTagName('head')[0], fragment;
		var cssText = '<link rel="stylesheet" href="' + this.options.cssPath + platform.cssFile + '" type="text/css"/>';
		var miscText = platform.miscHead;
		if(platform.cssFile) {
			if(document.body) {
				fragment = document.createElement('div');
				fragment.innerHTML = cssText;
				head.appendChild(fragment.childNodes[0]);
			} else {
				document.write(cssText);
			}
		}
		if(platform.miscHead) {
			if(document.body) {
				fragment = document.createElement('div');
				fragment.innerHTML = miscText;
				head.appendChild(fragment.childNodes[0]);
			} else {
				document.write(miscText);
			}
		}
	},
	uaMatch:function(str) {
		if(!this.ua) {
			this.ua = navigator.userAgent.toLowerCase();
		}
		return this.ua.indexOf(str.toLowerCase()) != -1;
	}
}.init();

// image stretch module
backgroundStretcher = {
	images: [],
	holders: [],
	viewWidth: 0,
	viewHeight: 0,
	stretchByWindow: false, // or entire page
	init: function(){
		this.addHandlers();
		this.resizeAll();
		return this;
	},
	stretchImage: function(obj) {
		var img = new Image();
		img.onload = this.bind(function(){
			obj.iRatio = img.width / img.height;
			obj.iWidth = img.width;
			obj.iHeight = img.height;
			obj.style.msInterpolationMode = 'bicubic'; // IE7 fix
			this.resizeImage(obj);
		});
		img.src = obj.src;
		this.images.push(obj);
	},
	setBgHolder: function(obj) {
		this.holders.push(obj);
		this.resizeAll();
	},
	resizeImage: function(obj) {
		if(obj.iRatio) {
			var slideWidth = this.viewWidth;
			var slideHeight = slideWidth / obj.iRatio;
			if(slideHeight < this.viewHeight) {
				slideHeight = this.viewHeight;
				slideWidth = slideHeight * obj.iRatio;
			}
			obj.style.width = slideWidth+'px';
			obj.style.height = slideHeight+'px';
			obj.style.top = (this.viewHeight - slideHeight)/2+'px';
			obj.style.left = (this.viewWidth - slideWidth)/2+'px';
		}
	},
	resizeHolder: function(obj) {
		obj.style.width = this.viewWidth+'px';
		obj.style.height = this.viewHeight+'px';
	},
	getWindowWidth: function() {
		return typeof window.innerWidth === 'number' ? window.innerWidth : document.documentElement.clientWidth;
	},
	getWindowHeight: function() {
		return typeof window.innerHeight === 'number' ? window.innerHeight : document.documentElement.clientHeight;
	},
	getPageWidth: function() {
		if(!document.body) return 0;
		return Math.max(
			document.getElementById('bg-scale').offsetWidth
		);
	},
	getPageHeight: function() {
		if(!document.body) return 0;
		return Math.max(
			document.getElementById('bg-scale').offsetHeight
		);
	},
	resizeAll: function() {
		// crop holder width by window size
		for(var i = 0; i < this.holders.length; i++) {
			this.holders[i].style.width = '100%'; 
		}
		
		// delay required for IE to handle resize
		clearTimeout(this.resizeTimer);
		this.resizeTimer = setTimeout(this.bind(function(){
			// hide background holders
			for(var i = 0; i < this.holders.length; i++) {
				this.holders[i].style.display = 'none';
			}
			
			// calculate real page dimensions with hidden background blocks
			this.viewWidth = this[this.stretchByWindow ? 'getWindowWidth' : 'getPageWidth']();
			this.viewHeight = this[this.stretchByWindow ? 'getWindowHeight' : 'getPageHeight']();
			
			// show and resize all background holders
			for(i = 0; i < this.holders.length; i++) {
				this.holders[i].style.display = 'block';
				this.resizeHolder(this.holders[i]);
			}
			for(i = 0; i < this.images.length; i++) {
				this.resizeImage(this.images[i]);
			}
		}),10);
	},
	addHandlers: function() {
		if (window.addEventListener) {
			window.addEventListener('resize', this.bind(this.resizeAll), false);
			window.addEventListener('orientationchange', this.bind(this.resizeAll), false);
		} else if (window.attachEvent) {
			window.attachEvent('onresize', this.bind(this.resizeAll));
		}
	},
	bind: function(fn, scope, args) {
		var newScope = scope || this;
		return function() {
			return fn.apply(newScope, args || arguments);
		}
	} 
}.init();
