var structure, items, cached, txt, media, imageview;

var nfcnfg = {
	// DEVELLOPER
	debug: false,
	cnsl: false,
	log: true
	
}


$.expr[':'].mailto = function(obj){
    return obj.href.match(/^mailto\:/)
};


if($.cookie('parallaxOn')){
	 var parallaxOn = ( $.cookie('parallaxOn')=='true' )? true: false;
}
else{
	var parallaxOn = true;
	$.cookie('parallaxOn', 'true', { path: '/' });
}
var settings = {

	layertotal: 15,
	layeroffset: 7,
	parallax: parallaxOn,
	tunetop: 00,
	tune_item_top: 220,
	tuneleft: 0,
	tunesize: 0,
	contentpath: '/_images/content/',
	titlepath: '/_images/titles/'
	
}
$(document).ready(function(){

	// time.set.load()

	
	$('body').append(

	);

	$('#mail').click(function(){
		// alert('mail');
		window.location.href = 'mailto:hello@mrkmln.nl';
	});
	// $.each(locations)
	// 'MRKMLN/photoshoots/ltm/issue3'
	var url = (edit)? '/JSON.php?editor=1': '/JSON.php?'+version;
	$.getJSON(url, function(json){

		///////////////////////////
		// ORDER RETURNED JSON DATA
		structure = json.structure;
		items = json.items;
		cached = json.cached;
		text = json.text;

		/////////////////
		// BUILD WEBSITE
		mrk.init({});
		$('a:mailto').each(function(){
			$(this).text( $(this).text().replace(/&zwnj;/g,'') )
			$(this).attr( 'href', $(this).attr('href').replace(/&zwnj;/g,'') )
		})
	});
	setTimeout(function(){
		if(!structure)alert('JSON Error.\nPlease contact Developer');
		},4000);
});
                                                                                                                                                                         

var lctn = {

	read: function(){
		lctn.url = window.location.toString();
		lctn.url = lctn.url.replace(/http:\/\/www.mrkmln/, 'mrkmln');
		lctn.url = lctn.url.replace(/http:\/\/mrkmln/, 'mrkmln');
		lctn.root = (lctn.url.match('.nl'))? 'mrkmln.nl': 'mrkmln'; // live and local version
		lctn.url = lctn.url.replace(/.nl/, '');
		lctn.path = (lctn.url.length<10)? lctn.root + lctn.url.replace(/mrkmln\//,''): lctn.root + lctn.url.replace(/mrkmln/,'');
		// quick fix live!
		lctn.path = (lctn.path[lctn.path.length-1]=='/')? lctn.path.substr(0,lctn.path.length-1): lctn.path;
		lctn.suburl = lctn.url.split('/');

		// IF TRAILING SLASH, REMOVE LAST SUBURL
		if(!lctn.suburl[lctn.suburl.length-1]){
			lctn.suburl.pop();
			lctn.url = lctn.url.substr(0,lctn.url.length-1);
		}

		// IF LAST SUBURL IS NUMBER: IMAGEVIEW
		// alert(lctn.suburl[lctn.suburl.length-1]*1)
		if( (lctn.suburl[lctn.suburl.length-1] * 1) ){
			imageview = true;
		}
	}
}
lctn.read()

var ttl = {

	totext: function(title){
		title = title.replace(/_/g, ' ').replace(/;/g,'').replace(/:/g,' ');
		return title;
	},

	tolink: function(title){
		title = title.toLowerCase().replace(/ /g, '_').replace(/;/g,'').replace(/:/g,'_');
		return title;
	},

	tourl: function(title){
		str_replace(['::',':: ',';;','_', '&nbsp;'],['','','','',' ',' '],title)
		title = title.toLowerCase().replace(/;/g,'').replace(/:/g,'').replace(/ /g, '_');
		return title;
	}
}

// TOGGLE PARALLAX
function togglePreview () {
	if($.cookie('parallax')=='true'){
		alert('parallax!')
	}
	else{
		
	}

}


// PREVENT CLICK WHEN DRAGGING AN ITEM
function preventItemClick(){
	$(this).unbind('click', goToImage);
}
function restoreItemClick(){
	$(this).bind('click', goToImage);
}
function goToImage () {
	var sqlid = $(this).data('data').sqlid;
	var pth = location.pathname.split('/')
	if(!pth[pth.length-1]) pth.pop(); // REMOVE TRAILING SLASH IF ANY
	location.pathname = pth.join('/')+'/'+ sqlid+'/';
}
function goFromImage () {
	var pth = location.pathname.split('/')
	if(!pth[pth.length-1]) pth.pop(); // REMOVE TRAILING SLASH IF ANY
	pth.pop(); // REMOVE SQLID
	location.pathname = pth.join('/')+'/';
}


var mrk = {

	init: function (options) {

		/////////
		// SETUP 
		mrk.parallax = $('#parallax');
		mrk.matchCount = 0; // FOR ADDING A HEADER FOR EACH SUBURL
		mrk.scrollportTimer = '';
		mrk.scrollport = $('#scrollport');
		mrk.mouseport = $('#mouseport');
		///////////
		// PREPARE
		mrk.helpers.setsize();
		///////////////
		// PRLX OPTIONS
		mrk.prlx_options = {
			triggerExposesEdges: true,
			triggerResponse: true,
			
			// takeoverFactor: 0.98, //0-1 0.68
			// takeoverThresh: 0.001, //0-1 0.002
			// xtravel: 0.8,
			// ytravel: 0.8,
			// yorigin: 0.4,
			// yorigin: 'top',
			frameDuration: 35,
			// cssViewport: '#mouseport',
			// mouseActiveOutside: true,
			mouseport: mrk.mouseport
		}
		mrk.prlx_lr1_options = {
			xparallax: false,
			yparallax: false
		}

		////////////
		// BUILD UP
		mrk.add.layers();
		mrk.layers = $('.layer');

		mrk.add.structure(structure);

		/////////////////////
		// ACTIVATE PARALLAX
		(settings.parallax)? mrk.parallax.jparallax(mrk.prlx_options, mrk.prlx_lr1_options): null;
		// mrk.parallax.jparallax(mrk.prlx_options);
		// var ref = $('#init').attr('href');
		mrk.parallax.trigger("jparallax", ['#init']);

		// ENABLE DRAGGABLILITY OF LIVEUSER ITEMS
		if (edit){
			liveEdit.enable();
		}
		else{
			$.each($('.item:not(.solo)'), function(i,l){
				$(l).draggable({
					delay: 300,
					start: preventItemClick,
					stop: restoreItemClick
				})
			});

			$.each($('.frameBody'), function(i,t){
				$(t).draggable({delay: 300});
			});
		}

		// WRAP A SHADOW CONTAINER WHEN CSS SHADOW NOT SUPPORTED

		if( !$('body').css('-webkit-box-shadow') && !$('body').css('-moz-box-shadow') ){
			$('.titlepart').each(function(i,tp){
				$(tp).wrap(
					$('<div/>').addClass('shadow').css({ 
						height: $(tp).height()+16,
						width: $(tp).width()+10,
						zIndex: 100-i
					})
				).css({ 
					top: 0,
					left: 0
				}).after(
					$('<div/>').addClass('fix').hover(function(){
						$(this).parent().next().find('.titlepart').trigger('mouseenter')
					},function(){
						$(this).parent().next().find('.titlepart').trigger('mouseleave')
					}).click(function(){
						window.location = $(this).parent().next().find('.titlepart').attr('href');
					})
				)
			})
		}
		// time.get.ready()
	},

	add: {

		layers: function(){

			var i = 0;

			while (i < settings.layertotal){

				var w = mrk.winw + (i*settings.layeroffset);
				var h = mrk.winh + (i*settings.layeroffset);

				var toplayerid = (i==settings.layertotal-1&&edit)? 'toplayer':'';

				var layer = 	$('<div/>').addClass('layer').attr('id', 'layer_'+i).css({
									width: i*settings.layeroffset,
									height: i*settings.layeroffset
								});

				// INIT IS THE POINT WHICH THE VIEWPORT WILL TRY TO CENTER
				var init = null; // ONLY APPEND TO TOP LAYER
				if(i==settings.layertotal-1) init=$('<div/>').attr({id: 'init'}).css({ left: mrk.winw/2, top:100 }).append($('<a/>').attr({ href: '', name: 'init' }));

				$(mrk.parallax).append(
					layer.append(
						$('<div/>').addClass('define_tl').css({ top:0, left: - mrk.winw/2 }), init,
						$('<div/>').addClass('define_br').css({ top:h, right: - (mrk.winw/2 +i*settings.layeroffset) }),
						$('<div/>').addClass('innerlayer layer'+i).attr('id', toplayerid).css({ 
								// right: - (mrk.winw/2 - i*settings.layeroffset),
								right: -1006/2 + i*settings.layeroffset/2,
								top:  (i*settings.layeroffset)/2,
								// left: - mrk.winw/2 + (i*settings.layeroffset),
								left: - 1006/2 + i*settings.layeroffset/2,
								height: h+100, 
								marginTop: settings.tunetop,
								marginLeft: settings.tuneleft
						}).append(
							$('<div/>').addClass('headlines').css({ top:50 })
						)
					)
				)
				i++;
			}
			// $('.layer:first').next().next().next().next().next().find('.innerlayer').append($('<a/>').attr({ right: - (mrk.winw/2 - i*settings.layeroffset), href: 'sucks', name: 'init', id: 'init' }));
			
		},

		structure: function(branch){

			$.each(branch.children, function(i, child){
				
				if(lctn.suburl[mrk.matchCount]){
					// alert(lctn.url)
					// IF CHILD TITLE IS SAME AS CURRENT SUBURL
					// alert(child.title_url_l1+' == '+lctn.suburl[mrk.matchCount])
					if( child.title_url_l1 == lctn.suburl[mrk.matchCount] ){

						// ADD THE HEADER
						mrk.add.content.headers(child);

						//cnsl.log('build', 'add.structure: '+child.title_l1);

						// IF LAST SUBURL INSERT TEXT BLOCKS
						if(mrk.matchCount == (lctn.suburl.length-1)){
							$.each(text, function(i, txt){
								// alert(child.sqlid+' == '+txt.item_id+'||'+txt.structure_id)
								if((child.media&&child.sqlid == txt.item_id)||(!child.media&&child.sqlid == txt.structure_id)){
									var layer = (txt.layer==99)? Math.floor(Math.random() * (mrk.layers.length-1))+1: txt.layer;

									$(mrk.layers[layer]).find('.innerlayer').append(
										$('<div/>').addClass('frameBody').data('data',txt).css({ zIndex: 30, width: txt.w, left: txt.l, top: txt.t+ settings.tune_item_top }).html(txt.content)
									)
								}
							});
						}
// alert(mrk.matchCount+'_'+(lctn.suburl.length-1)+':'+lctn.suburl[0]+'/'+lctn.suburl[1]+'  '+!(child.children))
// alert(child.children)
						// KEEP LOOKING FOR FOLLOW UP MATCHES UNTILL MATCHES EXCEED SUBURL-LENGTH
						if(mrk.matchCount < (lctn.suburl.length-1) && child.children){

							// INCREASE MATCHCOUNT
							mrk.matchCount++;
							// TRY ADDING ITEMS IN CASE THERE ARE PROJECTS AMONG THE SUBCATS
							mrk.add.content.items(child);
							// CONTINUE LOOKING FOR Matches
							mrk.add.structure(child);
							// STOP THE LOOP
							if (imageview){
								
								$.each(items, function(i,itm){
									$.each(itm.media, function(j,m){
										// console.log((lctn.suburl[lctn.suburl.length-1]*1))
										if( m.sqlid == (lctn.suburl[lctn.suburl.length-1]*1) ){
											
											var t = i*settings.layeroffset/2;
											var l = -mrk.winw/2+(mrk.layers.length-i)*settings.layeroffset*2;
											//142 it is. 753 - 1122/2
											// if(m.layer==99) alert(m.layer)
											var layer = (m.layer==99)? Math.floor(Math.random() * (mrk.layers.length-1))+1: m.layer;
											// cnsl.log(lyr)
											$(mrk.layers[layer]).find('.innerlayer').append(
												$('<div/>').css({ 
													left: 0, 
													top: settings.tune_item_top+60, 
													width: m.source_w, 
													height: m.source_h,
													zIndex: 30
												}).addClass('item solo').data('data', m).css({position:'relative',margin:'0 auto'}).append(
													$('<img/>').attr({ src: settings.contentpath+m.static_filename+'_source.png' }),
													$('<a/>').addClass('mediatitle').attr({'href': '#' }).html(m.title)
												).bind('click', goFromImage)
											)
										imageview = false;
										}
									})
								})
							}
							return false;
						}
						// STOP LOOKING FOR FOLLOW UP MATCHES WHEN SUBURLS RAN OUT
						else{

							mrk.helpers.setLayerHeight(child);
							// INCREASE MATCHCOUNT
							mrk.matchCount++;
							//cnsl.log(child.title_url_l1);
							mrk.add.content.items(child);
							mrk.add.content.structure(structure, child.nr);

							// STOP THE LOOP
							return false;
						}
					}
					else{
						//cnsl.log(ttl.tourl(child.title_l1)+'!='+ttl.tourl(lctn.suburl[mrk.matchCount]))
					}
					
				}
			});
		},

		content: {

			structure: function(branch, nr){
				// cnsl.log('>>'+branch.title_l1)
// alert('content.structure: '+branch.children.length)
				$.each(branch.children, function(i, chld){
					// alert(i)
						mrk.matchCount++

						// DEFINE PARENTNR
						var l = chld.nr.indexOf('_00');
						var r = chld.nr.substr(0,l-2)+'00_00_00_00_00_00';
						var parent = r.substr(0,17);

						// IF MATCH IS FOUND
						if(nr == parent){
							// var title = ttl.tourl(chld.title_l1);
							// cnsl.log('build', 'mrk.add.content.structure: '+title);
							// alert('content.structure')
							
							mrk.add.content.noname(chld)
						}

						if(chld.children) {
							// alert('(branch:'+branch.title_l1+'/child:'+chld.title_l1+'/'+nr+'/'+branch.children.length+') '+'from mrk.add.content.structure')
							mrk.add.content.structure(chld, nr);
						}
						else{
							// alert('child:'+chld.title_l1+'no children')
						}
				})
			},

			items: function(child){

				// LOOK FOR DIRECT CHILDREN IN JSON_ITEMS
				$.each(items, function(i, json_item){

					// if(lctn.suburl[mrk.matchCount]){
						// if(json_item.title.toLowerCase().replace(/;/g,'').replace(/:/g,' ') == lctn.suburl[mrk.matchCount].toLowerCase()){

							// IF MATCH IS FOUND
							if(child.nr == json_item.container && !json_item.hidden){

								//cnsl.log('build', 'mrk.add.content.items: '+json_item.title_l1);
								mrk.add.content.noname(json_item);
							}
						// }
					// }
				});
				// LOOK FOR DIRECT CHILDREN IN JSON_STRUCTURE
			},

			noname: function(json_data){

				// ADD HEADER
				if(lctn.suburl.length-mrk.matchCount>0){

					if(json_data.title_url_l1 == lctn.suburl[mrk.matchCount]){
						//cnsl.log('build', 'mrk.add.content.noname: '+json_data.title_l1);
						mrk.add.content.headers(json_data);
						if (!imageview){
							mrk.add.content.media(json_data);

							// ADD 
							$.each(text, function(i, txt){
								// IF MATCH IS FOUND
								if(json_data.media && json_data.sqlid == txt.item_id){
									// ASSIGN LAYER
									var layer = (txt.layer==99||txt.layer>settings.layertotal-1)? Math.floor(Math.random() * (mrk.layers.length-1))+1: txt.layer;
									$(mrk.layers[layer]).find('.innerlayer').append(
										$('<div/>').addClass('frameBody').data('data',txt).css({ zIndex: 30, width: txt.w, left: txt.l, top: txt.t+ settings.tune_item_top }).html(txt.content)
									)
								}
							});
						}
					}
				}
				// OR ADD ITEM
				else {

					// CREATE PRE-URL
					var href = '';
					$.each(lctn.suburl, function(i, s){
						if(i==0) href += lctn.root+'/';
						else href += s+'/';
					})

					// CREATE TITLE CONTAINER
					var tc = $('<a/>').addClass('titlecontainer').attr({href: 'http://'+href+json_data.title_url_l1 }).hover(
						function(){
							$.each($(this).find('.titlepart'), function(i, e){
								
								var xy = $(e).css('backgroundPosition').split('px ');
								var xy = (xy[1])? xy: $(e).css('backgroundPosition').split('pt '); //firefox being mad
								var x = xy[0];
								var y = xy[1]; // has px string on end!
								
								// alert(xy)
								$(e).css({ backgroundPosition: x-668+'px '+y });
							});
						},
						function(){
							$.each($(this).find('.titlepart'), function(i, e){
								var xy = $(e).css('backgroundPosition').split('px ');
								var xy = (xy[1])? xy: $(e).css('backgroundPosition').split('pt '); //firefox being mad
								var x = xy[0];
								var y = xy[1]; // has px string on end!
								// alert(y)
								$(e).css({ backgroundPosition: (x*1)+668+'px '+y });
							});
						}
					);


					function addTitle(i,w,z){
						tc.append(
							$('<div/>').addClass('titlepart').css({ zIndex: z, height: t_h-t_h_tune, width: (w*1), backgroundImage: 'url('+settings.titlepath+image+')', backgroundPosition: '0 '+(-bt-(i*t_h)-bt_tune)+'px' })	
						)
					}

					// ADD MULTI-LINE TITLE
					var t_sw = json_data.t_sw.split(',');

					if(json_data.category){
						var t_h_tune = 16;
						var bt_tune = 9;
						var t_h = 54;
						var image = cached.categorylinetitles;
						var bt = json_data.jsnr*t_h*4;
						var postertype = 'c_poster';
						$.each(t_sw, function(i,w){ addTitle(i,(w*1+3),t_sw.length-i) })
					}
					if(json_data.subcategory){
						var t_h_tune = 14;
						var bt_tune = 9;
						var t_h = 43;
						var image = cached.subcategorylinetitles;
						var bt = json_data.jsnr*t_h*4;
						var postertype = 'sc_poster';
						$.each(t_sw, function(i,w){ addTitle(i,(w*1+1),t_sw.length-i) })
					}
					if(json_data.media){
						var t_h_tune = 9;
						var bt_tune = 5;
						var image=cached.itemstitles;
						var t_h = 29;
						var bt = (json_data.jsnr-1)*t_h;
						var postertype = 'p_poster';
						addTitle(1, json_data.t_w,(t_sw*1+1).length);
						
					}


					// alert(json_data.t_l*t_h+60)
					// ADD SUBTITLES
					var stc = $('<div/>').css({ top: json_data.t_l*(54-14)+70 }).addClass('subtitlecontainer');
					// alert(json_data.subcategory)

					if(json_data.subcategory||json_data.category){
						var z = 20;
						if(json_data.children){
							$.each(json_data.children, function(i,chld){
								// var z = json_data.children.length-i;
								if(!chld.hidden){
									stc.append(
										$('<a/>').addClass('titlepart').attr({ href: 'http://'+lctn.path+'/'+json_data.title_url_l1+'/'+chld.title_url_l1 }).css({ height: 43-13, width: (chld.t_w*1)+4, zIndex: z--, backgroundImage: 'url('+settings.titlepath+cached.subcategorytitles+')', backgroundPosition: '0 '+(-(chld.jsnr)*43-7)+'px' }).hover(
											function(){ $(this).css({ backgroundPosition: '-668px '+(-(chld.jsnr)*43-7)+'px' }); },
											function(){ $(this).css({ backgroundPosition: '-0 '+(-(chld.jsnr)*43-7)+'px' }); }
										)
									)
								}
							})
						}
						$.each(items, function(i,item){
							if(json_data.nr==item.container&&!item.hidden){
								stc.append(
									$('<a/>').addClass('titlepart').attr({ href: 'http://'+lctn.path+'/'+json_data.title_url_l1+'/'+item.title_url_l1, 'timo': 'test' }).css({ height: 29-9, width: (item.t_w*1)+1, zIndex: z--, backgroundImage: 'url('+settings.titlepath+cached.itemstitles+')', backgroundPosition: '0 '+ (-(item.jsnr)*29-5)+'px' }).hover(
										function(){ $(this).css({ backgroundPosition: '-668px '+ (-(item.jsnr)*29-5)+'px'}); },
										function(){ $(this).css({ backgroundPosition: '0 '+ (-(item.jsnr)*29-5)+'px' }); }
									)
								)
							}
						})
					}
					// if(json_data.media){
					// 	$.each(json_data.media, function(i,sc){
					// 		stc.append($('<div/>').text(sc.title_l1))
					// 	})
					// }

					// ADD ITEM

					var layer = (json_data.layer==99||json_data.layer>settings.layertotal-1)? Math.floor(Math.random() * (mrk.layers.length-1))+1: json_data.layer;

					$(mrk.layers[layer]).find('.innerlayer').append(
						$('<div/>').addClass('item').data('data',json_data).css({
							width: json_data.w, 
							height: json_data.h, 
							top: json_data.t + settings.tune_item_top, 
							left: json_data.l,
							backgroundImage: 'url('+settings.contentpath+json_data.poster+postertype+'.jpg)',
							zIndex: 30
						}).append(
							// $('<a/>').attr({'href': 'http://'+href+title.replace(/ /g,'_').replace(/;/g,'').replace(/:/g,'_').toLowerCase() }).text(title.replace(/;/g,'').replace(/:/g,' ')),
							tc,
							stc
						)
					)
				}				
			},

			media: function(item){

				$.each(item.media, function(i, m){
					var t = i*settings.layeroffset/2;
					var l = -mrk.winw/2+(mrk.layers.length-i)*settings.layeroffset*2;
					//142 it is. 753 - 1122/2
					// if(m.layer==99) alert(m.layer)
					var layer = (m.layer==99)? Math.floor(Math.random() * (mrk.layers.length-1))+1: m.layer;
					// cnsl.log(lyr)
					$(mrk.layers[layer]).find('.innerlayer').append(
						$('<div/>').css({ 
							left: m.l, 
							top: m.t + settings.tune_item_top, 
							width: m.w, 
							height: m.h,
							zIndex: 30
						}).addClass('item').data('data', m).append(
							$('<img/>').attr({ src: settings.contentpath+m.version_filename+'_large.jpg' }),
							$('<a/>').addClass('mediatitle').attr({'href': '#' }).html(m.title)
						).bind('click',goToImage)
					)
				});
				mrk.helpers.setLayerHeight(item);
			},

			headers: function(json_data){

				// title = (json_data.title_l1)? json_data.title_l1: json_data.title;
				// title = title.replace(/_/g, ' ').replace(/&nbsp;/g, ' ');

				var headline = '<h'+(mrk.matchCount+1)+' />';
				var image = '';
				var t_bt = false;
				switch (mrk.matchCount+1){
					case 1: 
						t_h = 114; 
						t_t = 0; 
						t_bt = 0;
						t_w = 668;  
						t_tag = '<h1>'; 
						image='mrkmln_logo.png'; 
					break;
					case 2: 
						t_h = 54; 
						t_t = -13; 
						t_w = json_data.t_w+10;  
						t_tag = '<h2>'; 
						if(json_data.media) image=cached.itemstitles;
						if(json_data.subcategory) image = cached.subcategorytitles;
						if(json_data.category) image = cached.categorytitles;
						// else image = cached.categorytitles; 
					break;
					case 3: 
						t_h = (json_data.media)? 29: 43;
						t_t = (json_data.media)? -5: -10; 
						t_w = json_data.t_w+7; 
						t_tag = '<h3>'; 
						if(json_data.media) image=cached.itemstitles;
						if(json_data.subcategory) image = cached.subcategorytitles;
						if(json_data.category) image = cached.categorytitles;
						// else image = cached.categorytitles;
					break;
					case 4: 
						t_h = 29; 
						t_t = -5; 
						t_w = json_data.t_w+5; 
						t_tag = '<h4>'; 
						if(json_data.media) image=cached.itemstitles;
						if(json_data.subcategory) image = cached.subcategorytitles;
						if(json_data.category) image = cached.categorytitles;
						// else image = cached.categorytitles;
					break;
				}
				t_bt = (t_bt)? t_bt: -json_data.jsnr*t_h; 

				var href = 'http://'
				var i = 0;
				while(i<mrk.matchCount+1){
					if(i==0) href += lctn.root+'/';
					else href += lctn.suburl[i]+'/';
					i++
				}

				$.each(mrk.layers, function(i, layer){

					// if(i>mrk.layers.length/2){

						// COMPENSATE LAYER EXPANSION
						var l = -mrk.winw/2+i*settings.layeroffset/2;
						var t = i*settings.layeroffset/2;

						var tkzt = (parallaxOn)? 'Disable Viewport': 'Enable Viewport';
						var prllxtitle = (parallaxOn)? 'Stop the viewport effect.': 'Turn the viewport effect back on.';

						// TOP-LAYER-BACKLINKS
						if( i == settings.layertotal-1 && mrk.matchCount != lctn.suburl.length-1){
							$(layer).find('.headlines:first').append(
								$('<a/>').attr({ href: href }).addClass('header active').css({  width: t_w, marginTop: t_t, height:t_h, backgroundImage: 'url('+settings.titlepath+image+')', backgroundPosition: '-1336px '+t_bt+'px ' }).hover(
									function(){
										$(this).css({ backgroundPosition: '-668px '+t_bt+'px' });
									},
									function(){
										$(this).css({ backgroundPosition: '-1336px '+t_bt+'px' });
									}
								)
							)
						}

						// TOP-LAYER-CURRENT-PAGE
						else if(i == settings.layertotal-1){
							$(layer).append(
								$('<div/>').addClass('parallaxonoff').attr('title', prllxtitle).text(tkzt).click(function(){
									var state = (parallaxOn)? 'false': 'true';
									$.cookie('parallaxOn', state, { path: '/' });
									window.location.reload();
								})
							).find('.headlines:first').append(
								// $(headline).text(title).css({ color: 'pink', 'left': 0, 'top': 0, width: mrk.winw })
								$('<div/>').addClass('header active').css({  width: t_w, marginTop: t_t, height: t_h, backgroundImage: 'url('+settings.titlepath+image+')', backgroundPosition: '-1336px '+t_bt+'px ' })
							)
						}
						// NORMAL LAYER
						else{
							$(layer).find('.headlines:first').append(
								// $(headline).text(title).css({ 'left': 0, 'top': 0, width: mrk.winw })
								$('<div/>').addClass('header plain').css({  width: t_w, marginTop: t_t, height: t_h, backgroundImage: 'url('+settings.titlepath+image+')', backgroundPosition: '0 '+t_bt+'px ' })
								// $('<img/>').attr({ src: settings.titlepath+'mrkmln_logo.png' }).css({ width: 671/*item.t_w*/ })
							)
						}
					// }
				});
			}
		}
	},

	helpers: {

		setsize: function(){
			var w = $(window).width();
			mrk.winw = 1006-2*settings.tunesize;
			mrk.winh = $(window).height();
			mrk.parallax.css({ left: mrk.winw/2 })
			mrk.scrollport.css({ width: 1006, top: settings.tunesize });
			// mrk.mouseport.css({ height: mrk.winh });
		},

		setLayerHeight: function(json_data){

			//cnsl.log('build', 'mrk.helpers.setLayerHeight: '+json_data.page_h+' ('+json_data.title_l1+')')
			// console.log(json_data.title_l1+': '+json_data.page_h+settings.tune_item_top+20)
			// mrk.scrollport.height(json_data.page_h)
			$.each($('.innerlayer'), function(i,il){
				$(il).css({ 'height': json_data.page_h+settings.tune_item_top+20 })
			});
			$.each($('.define_bl'), function(i,bl){
				$(bl).css({ 'top': json_data.page_h + (i*settings.layeroffset) })
			});
		}
	}
}
