

var iPad;
iPad = (navigator.userAgent.indexOf("iPad") > -1)? true : false;



var activeBlock = null;

var offset;
var myScroll;

var currPage;
var numPages;
var pageW = 974;


var isMac = (navigator.appVersion.indexOf("Mac") != -1)? true : false;

///////////////////////
//////// INIT /////////

function init() {
	
	/// BLOCK TEXT POSITIONS 
	$('.tPos4, .tPos5, .tPos6').css('top', function() {
		var myH = $(this).height();
		var parentH = $(this).parent().height();
		var pos = parentH / 2 - myH / 2;
		var cssVal = pos + "px";
		return cssVal;
	});
	$('.tPos2, .tPos5, .tPos8').css('left', function() {
		var myW = $(this).width();
		var parentW = $(this).parent().width();
		var pos = parentW / 2 - myW / 2;
		var cssVal = pos + "px";
		return cssVal;
	});
	
	/// ASSIGN ROW data() TO BLOCKS (NEEDED FOR INSERTING CONTENT AFTER ROW) 
	$('.block').each(function() {
		$(this).data("row", $(this).parents('.row'));
	});
	
	if (iPad) {
		$('#contentInner').wrap('<div id="scroller"/>');
		$('#scroller').wrap('<div id="wrapper"/>');
	}
	$('#contentInner').bind('load', setupScroller);
	
	$(window).resize(function() {
		offset = $('#scrollPane').offset();
	});
	
	$.address.strict(false);
	$.address.change(function(event) {  
        var hs = event.value;
		if (anchors[hs] != null) {
			var id = anchors[hs];
			blockClick(id);
		}
		else if (hs == '') {
			contentContract(false, true);
		}
    });  
    $('a').not('.external').click(function() {
		if ($.address.value() == $(this).attr('title')) {
			var id = anchors[$.address.value()];
			blockClick(id);
		}
        else $.address.value($(this).attr('title'));
		return false;
    });  
	
}

/////////////////////// 
///// BLOCK CLICK ///// 
function blockClick(id) {
	contentContract(id);
	if (activeBlock != null) {
		hiLite(activeBlock, false);
	}
	activeBlock = $('#item'+id);
	hiLite(activeBlock, true);
}


///////////////////////////////////////////////
///// SHOP RESIZE ON INTERNAL NAVIGATION //////
function resizeShop(win) {
	return;
}

function shopOnLoad() {
	var h = $('#shopFrame').contents().find('#super_main_table').height() + 20;
	//if (isMac) alert("height: " + h);
	
	h = 656;
	
	$('#shopFrame').height(h);
	contentExpand(h);
}

////////////////////////////////////
///// GET JSON AND MAKE CONTENT ////
function getContent(_id) {
	
	$('#contentInner').empty();
	$('#contentInner').width(pageW);
	
	$.getJSON("getContentItem.php", {id:_id}, function(item) {
		var instance = item.instance;
		var html = "";
		var openContent = true;
		numPages = 1;
		
		
		switch (item.typeName) {
			/// SHOP LINK
			case "webshop page link":
				html += '<iframe id="shopFrame" src="' + instance.url + '" frameborder="0"></iframe>'; // scrolling="no" 
				html += '<div class="navBtn closeShop"><a href="javascript:contentContract(false)"><img src="grfx/btnClose-black.gif"/></a></div>';
				$('#contentInner').append(html);
				$('#shopFrame').load(function() {
					if (isMac) setTimeout(shopOnLoad, 500);
					else shopOnLoad();
				});
				openContent = false;
			break;
			
			/// TEXT SECTION 
			case "text section":
				var pages = instance.pages;
				numPages = pages.length;
				$('#contentInner').width(pageW * numPages);
				for (var i = 1; i <= pages.length ; i++) {
					var page = pages[i-1];
					html += '<div class="page" id="page'+i+'">';
					html += '<div class="pageContent">';
					html += '<h1>' + instance['name_'+lang] + '.</h1>';
					html += page['html_'+lang];
					html += '</div>';	/// END CONTENT 
					if (i == 1) html += '<div id="textLogo"></div>';
					html += addNavigation(i, numPages);
					html += '</div>';	/// END PAGE 
				}
				$('#contentInner').append(html);
			break;
			
			/// PRESS SECTION 
			case "press":
				var items = instance.items;
				numPages = Math.ceil(items.length / 12);
				var pageNum = 0;
				$('#contentInner').width(pageW * numPages);
				for (var i = 0; i < items.length ; i++) {
					var pItem = items[i];
					if (i % 12 == 0) {	/// ONE PAGE
						if (pageNum > 0) {
							html += '</ul>'; /// END THUMBS 
							html += '</div>';	/// END PAGE
						}
						pageNum++;
						html += '<div class="page white blackBg" id="page'+pageNum+'">';
						html += addNavigation(pageNum, numPages);
						if (i == 0) {
							html += '<div class="pageContent" style="bottom:100px;width:220px;">';
							html += '<h1>press.</h1>';
							html += '<h2>' + instance['title_'+lang] + '.</h2>';
							html += instance['text_'+lang];
							html += '</div>';	/// END CONTENT 
						}
						html += '<ul class="pressThumbsList">';	/// START THUMBS 
					}
					html += '<li class="pressItem">';
					html += '<div style="position:absolute;bottom:0">';
					html += '<a href="press/'+ item.id + "/" + pItem.file +'" target="_blank"><img class="pressThumb" src="press/'+ item.id + "/" + pItem.thumb +'"/></a><br />';
					html += '<a href="press/'+ item.id + "/" + pItem.file +'" target="_blank">'+ pItem.caption +'</a>';
					html += '</div>';
					html += '</li>';
				}
				$('#contentInner').append(html);
			break;
			
			/// VIDEO
			case "video":
				$('#contentInner').width(pageW);
				html += '<div class="page blackBg white" id="page'+i+'">';
				html += '<div class="pageContent" style="top:195px;">';
				html += '<h1>video.</h1>';
				html += '<h2>' + instance['title_'+lang].split("\r\n").join("<br />") + '</h2>';
				html += '<div style="width:150px">' + instance['text_'+lang] + '</div>';
				html += '</div>';	/// END CONTENT 
				html += '<div class="videoBlock">';
				html += '<a href="javascript:showVideo()"><img id="still" src="videos/'+item.id+'/still.jpg"/></a>';
				html += '<a href="javascript:showVideo()"><img id="btnPlay" src="grfx/btnPlay.gif"/></a>';
				html += '<div id="video"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="590" height="367">';
				html += '<param name="SRC" value="videoPlayer.swf?src=videos/'+item.id+'/movie.flv"/><param name="allowFullScreen" value="true">';
				html += '<embed src="videoPlayer.swf?src=videos/'+item.id+'/movie.flv" width="590" height="367" allowFullScreen="true"/></object></div>';
				html += '</div>';
				html += addNavigation(i, numPages);
				html += '</div>';	/// END PAGE 
				$('#contentInner').append(html);
			break;
			
			/// COLLECTION
			case "collection":
				var images = instance.images;
				var templ = item.instance.template_id;
				var pages = [];
				var skipOne = false;
				for (var i = 0; i < images.length ; i++) {
					if (skipOne) {
						skipOne = false;
						continue;
					}
					html = "";
					var img = images[i];
					html += '<div class="page';
					if (i == 0) html += ' blackBg';
					html += '" id="page'+(pages.length+1)+'">';
					var format = (img.vertical == 1)? "V" : "H";
					html += '<img src="collections/'+item.id+'/'+img.filename+'" class="img1Pos'+templ+' img'+format+'size'+templ+'"/>';
					if (format == "V") {
						var img2 = images[i+1];
						html += '<img src="collections/'+item.id+'/'+img2.filename+'" class="img2Pos'+templ+' img'+format+'size'+templ+'"/>';
						skipOne = true;
					}
					if (i == 0) {
						html += '<div class="pageContent white">';
						html += '<h1>collection.</h1>';
						html += '<h2>' + instance['title_'+lang].split("\r\n").join("<br />") + '</h2>';
						html += '<div style="width:200px">' + instance['text_'+lang] + '</div>';
						html += '</div>';	/// END CONTENT 
					}
					//// INSERT CAPTIONS! /////
					if (img.code != "" || img['caption_'+lang] != "") {
						html += '<div class="caption cap1"><div class="prodCode">'+img.code+'</div><div class="capText">' + img['caption_'+lang] + '</div></div>';
						if (format == "V") {
							html += '<div class="caption cap2"><div class="prodCode">'+img2.code+'</div><div class="capText">' + img2['caption_'+lang] + '</div></div>';
						}
					}
					html += '</div>';	/// END PAGE 
					html = $(html);
					pages.push(html);
				}
				numPages = pages.length;
				$('#contentInner').width(pageW * numPages);
				
				for (i = 0; i < pages.length ; i++) {
					var page = pages[i];
					page.append(addNavigation(i+1, numPages));
					$('#contentInner').append(page);
				}
			break;
		}
		
		currPage = 1;
		if (openContent) contentExpand();
		
	});
}

function addNavigation(pageN, numPages) {
	var str = "";
	if (pageN > 1) {
		str += '<div class="navBtn prevPage"><a href="javascript:prevPage()"><img src="grfx/btnPrev-black.gif"/></a></div>';
	}
	if (pageN < numPages) {
		str += '<div class="navBtn nextPage"><a href="javascript:nextPage()"><img src="grfx/btnNext-black.gif"/></a></div>';
	}
	str += '<div class="navBtn closeContent"><a href="javascript:contentContract(false)"><img src="grfx/btnClose-black.gif"/></a></div>';
	return str;
}

function prevPage() {
	if (currPage > 1) {
		var p = currPage - 1;
		$('#content').scrollTo('#page'+p, 600);
		currPage = p;
	}
}
function nextPage() {
	if (currPage < numPages) {
		var p = currPage + 1;
		$('#content').scrollTo('#page'+p, 600);
		currPage = p;
	}
}





////////////////////////////
////// OPEN CONTENT ////////
function contentExpand(hgt) {
	var h = (hgt)? hgt : 656;
	var scroll = true;
	$('#content').show();
	$('#content').animate({height: h}, 300, function() {
		$('html,body').animate({scrollTop:$('#content').offset().top - 10}, 650);
	});
	if (scroll) setTimeout(setupScroller, 1000);
}

function showVideo() {
	$('#video').show();
	$('#still').hide();
	$('#btnPlay').hide();
}

///////////////////////////
////// CLOSE CONTENT //////
function contentContract(id, root) {
	$('#content').animate({
		height: 0
	}, 600, function () {
		if (id !== false) {
			var clicked = $('#item'+id);
			$('#content').insertAfter(clicked.data('row'));
			getContent(id);
		}
		else {
			$('#content').hide();
			if (root) $('html body').scrollTo(0, 600);
			else if (activeBlock != null) $('html body').scrollTo(activeBlock.offset().top - 10, 600);
		}
	});
}

//////////////////////////////
//// HIGHLIGHT ACTIVE BL /////
function hiLite(elm, on) {
	/*
	if (on) {
		elm.css("opacity", 0.5);
	}
	else {
		elm.css("opacity", 1);
	}
	*/
	return;
	var shift = 10;
	if (on) {
		elm.css("border", shift+"px solid #ccc");
		elm.css("top", elm.position().top - shift);
		elm.css("left", elm.position().left - shift);
	}
	else {
		elm.css("border", "none");
		elm.css("top", elm.position().top + shift);
		elm.css("left", elm.position().left + shift);
	}
}

/////////////////////////
////////// IPAD /////////
function setupScroller() {
	if (iPad) {
		setTimeout(function(){ myScroll = new iScroll('scroller',{bounceLock:true});}, 500);
		document.getElementById("scroller").addEventListener('touchstart', function(e){ e.preventDefault(); }, false);
	}
}


function iScrollStart() {
	
}


function showScrollIndication(show) {
	
}

function stopIPadAutoScroll() {
	return; /// THIS CALL FROM iscroll.js, BECAUSE OF MAIN PAGES' AUTO SCROLL
}

$(init);

