MobileSite = function(){
	mImageIndex = 0;
	mScrollBy = 320;
	mScrollPlus = 0;
	this.gallery = null
	swipeInit = false;
	mPassedIndex = this.getUrlVars()["item"];
	mSwapStartPage = false;
	mImageArray = [];
	this.div = $(this.getDiv());
	if(DetectIphoneOrIpod())
		this.div.find('#bottom').hide()
	this.landscapeFooter = $('<div class="landscapeBottom"></div>')
	$('.holder').append(this.div);
	this.initSet();
	this.getMeta();
	window.addEventListener('load', setOrientation, false);  
	window.addEventListener('orientationchange', setOrientation, false);
}

MobileSite.prototype = {
	showLandscape: function(){
		$('img').attr({
  			width: '160',
		});
		this.div.find('#outer').css('width', '480px');
		this.div.find('.swipe').css('width', '480px');
		this.div.find('.mobile').css('width', '480px');
		this.div.find('#navigation').css('width', '480px');
		this.div.find('.navLogo').css('width', '306px');
		this.div.find('#bottom').css('top', '230px');
		this.div.find('#bottom').css('left', '80px');
		this.div.find('#contactInfo').css('padding-top', '45px');
		$('.mobile').append(this.landscapeFooter)
		mScrollBy = 160;
		mScrollPlus = 1;
		scrollTop()
		updateImageIndex()

	},
	showPortrait: function(){
		this.showLandscape();
		$('img').attr({
			width: '320',
		});
		this.div.find('#outer').css('width', '320px');
		this.div.find('.swipe').css('width', '320px');
		this.div.find('.mobile').css('width', '320px');
		this.div.find('#navigation').css('width', '320px');
		this.div.find('.navLogo').css('width', '146px');
		this.landscapeFooter.remove();
		this.div.find('#bottom').css('left', '0px');
		this.div.find('#bottom').css('top', '416px');
		this.div.find('#contactInfo').css('padding-top', '70px');
		mScrollBy = 320;
		mScrollPlus = 0;
		scrollTop()
		updateImageIndex()
	},
	
	getDiv: function(){
		return '<div class="mobile"><div id="navigation"><div class="navLogo"></div><div class="navGallery"></div><div class="navContact"></div></div><div class="swipe"><div id="outer" style="position:relative; left:0px; height:382px; overflow:hidden;"><div id="images"></div><div id="contactInfo"><conText>NEIL KELLERHOUSE<br/><orange>KELLERHOUSE, INC.</orange><br/><a href="mailto:neil@kellerhouse.com">NEIL@KELLERHOUSE.COM</a><br/><a href="tel:13104552684">310.455.2684</a></conText></div></div></div><div id="bottom" class"bottom"><div class="navRight"></div><div class="navLeft"></div></div></div>'
	},
	getMeta: function(){
		$.ajax({
			type: "GET",
			url: "meta/iPhoneData.xml",
			dataType: "xml",
			success: function(xml) {
				var count = 0;
				$(xml).find('sections').each(function(){
					var imageNumber = 1;
					$(this).find('group').each(function(){
						var routUrl = $(this).find('rootUrl').text();
						var id = $(this).find('name').text();
						var title = $(this).find('name').text();
						if(title == "WORK")
						{
							mMobileSite.galleryDiv = $('<div class="items" id="link_'+id+'"></div>').html("").appendTo('#images');
							$(this).find('item').each(function()
							{
								var url = $(this).find('url').text();
								var imageID = $(this).find('url').text();
								$('<div class="long" id='+imageID+'></div>').html('<img src='+routUrl+url+'?cacheKey=112810 width=320>').appendTo('#images');
								mImageArray.push(imageID);
								count ++ ;
							});
						};
					});
				});
			$("#images").css("width", count*320 );
			}
		})
	},
	getUrlVars: function(){
		var vars = {};
		var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, 
		function(m,key,value) {
			vars[key] = value;
		});
		return vars;
	},
	initSet: function(){
		if(mPassedIndex != undefined)
		{
			if(isNaN(mPassedIndex))
			{
				if(mPassedIndex == "ABOUT")
				{
					mSwapStartPage = true;
				}
			}
			else
			{
				mImageIndex = mPassedIndex * -1;
			}
		}
		if(!mSwapStartPage){
			$("#contactInfo").hide();
			setTimeout(updateImageIndex, 100);
		}else{
			$("#images").hide();
			$("#bottom").hide();
		}
	}
}

//DOM

function setOrientation() {  
 	var orient = Math.abs(window.orientation) === 90 ? mMobileSite.showLandscape() : mMobileSite.showPortrait(); 
//	var orient = Math.abs(window.orientation) === 90 ? mMobileSite.showLandscape() : mMobileSite.showLandscape(); 
}
function showGallery(e){
		mMobileSite.div.find("#contactInfo").hide('slow');
		mMobileSite.div.find("#images").fadeIn('slow');
		if(!DetectIphoneOrIpod())
				mMobileSite.div.find("#bottom").fadeIn('slow');

}
function showContact(e){
	mMobileSite.div.find("#images").fadeOut('slow');
	mMobileSite.div.find("#contactInfo").show('slow');
	if(!DetectIphoneOrIpod())
		mMobileSite.div.find("#bottom").fadeOut('slow');
}

$(document).ready(function(){
	setTimeout(scrollTop, 2000)
	window.scrollTo(0, 0);
	$(".navGallery").click(showGallery);
	$(".navContact").click(showContact);
	$(".navRight").click(
		function(event){
			right()
		}
	);
	$(".navLeft").click(
		function(event){
			left()
		}
	);
	$('.swipe').swipe({
		swipeLeft: function(){
			left()
		},
		swipeRight: function(){
			right()
		},
	});
});

function right(){
	mImageIndex ++;
			mImageIndex = mImageIndex >= 0 ? 0 : mImageIndex;
			var pos = 0;
			if((mImageIndex + mScrollPlus)* mScrollBy > 0)
				pos = 0;
			else
				pos = (mImageIndex + mScrollPlus)* mScrollBy;
			animate(pos);
			scrollTop()
}
function left(){
	mImageIndex --;
			mImageIndex = mImageIndex <= (mImageArray.length-1)*-1 ? (mImageArray.length-1)*-1 : mImageIndex;
			var pos = 0;
			if((mImageIndex + mScrollPlus)* mScrollBy > 0)
				pos = 0;
			else
				pos = (mImageIndex + mScrollPlus)* mScrollBy;

			if(mImageIndex == -1 && pos==0 && mScrollBy==160)
			{
				pos = -160
				mImageIndex = -2
			}
				

			animate(pos);
			scrollTop()
}
function scrollTop(){
	window.scrollTo(0, 0);
}
function updateImageIndex()
{	
	var pos = 0;
	if((mImageIndex + mScrollPlus)* mScrollBy > 0)
		pos = 0;
	else
		pos = (mImageIndex + mScrollPlus)* mScrollBy;
	
	$('#images').animate({ left: pos }, { duration: 0, complete: function() {} });
}
function animate  (pNumber){
	$('#images').animate({ left: pNumber }, { duration: 500, complete: function() {} });
}

