jQuery.expr[':'].Contains = function(a,i,m){
    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};


$(document).ready(function() {
	
	// buildBase(json.structure);
	takemejs();
	activateSubCat();
	activateButtons();
	addQuotes();
	highlightQuote();
	// jQuery.easing.def = "easeInOutSine";
});


function takemejs(){

	$('body:first').append(
		$('.takemejs')
	)
}
function highlightQuote () {

	var url = window.location.toString();
	var quotenr = url.split('&&')[1];

	if(quotenr){
		findQuote(quotenr);
	}else{
		var quotestring = url.split('#')[1];
		if(quotestring){
			var quotenr = parseFloat(quotestring); // CHECK IF NUMBER
			if(quotenr){
				findQuote(quotenr);
			}
		}
	}
}


var out;

function activateSubCat(){

	$('.subcat .bl_home').hover(function(){

		clearTimeout(out);
		// $(".overlay").stop(); $('.sheet:first').stop();
		$('.overlay').animate({ marginLeft: 100}, 250, "easeInOutSine" );
		$('.sheet:first').animate({ marginLeft: 100}, 250, "easeInOutSine" );
		$('.bl_home:first').animate({ marginLeft: 100}, 250, "easeInOutSine" );

	}, function(){

		out = setTimeout(function(){
			$(".overlay").stop(); $('.sheet:first').stop(); $(".bl_home:first").stop();
			$('.overlay').animate({ marginLeft: 0}, 150, "easeInOutSine" );
			$('.sheet:first').animate({ marginLeft: 0}, 150, "easeInOutSine" );
			$('.bl_home:first').animate({ marginLeft: 0}, 150, "easeInOutSine" );
		},10);
	});

	$('#subcatmap1').hover(function(){

		clearTimeout(out);
		$(".overlay").stop(); $('.sheet:first').stop(); $(".bl_home:first").stop();
		$('.overlay').animate({ marginLeft: 100}, 250, "easeInOutSine" );
		$('.sheet:first').animate({ marginLeft: 100}, 250, "easeInOutSine" );
		$('.bl_home:first').animate({ marginLeft: 100}, 250, "easeInOutSine" );
		
	}, function(){

		out = setTimeout(function(){
			$(".overlay").stop(); $('.sheet:first').stop(); $(".bl_home:first").stop();
			$('.overlay').animate({ marginLeft: 0}, 150, "easeInOutSine" )
			$('.sheet:first').animate({ marginLeft: 0}, 150, "easeInOutSine" );
			$('.bl_home:first').animate({ marginLeft: 0}, 150, "easeInOutSine" );
		},10);
	});


	$('#subcatmap2').hover(function(){

		clearTimeout(out);
		$(".overlay").stop(); $('.sheet:first').stop(); $(".bl_home:first").stop();
		$('.overlay').animate({marginLeft: -20}, 250, 'easeInOutSine' );
		$('.sheet:first').animate({ marginLeft: -20}, 250, 'easeInOutSine' );
		$('.bl_home:first').animate({ marginLeft: -20}, 250, "easeInOutSine" );
	}, function(){

		out = setTimeout(function(){
			$(".overlay").stop(); $('.sheet:first').stop(); $(".bl_home:first").stop();
			$('.overlay').animate({ marginLeft: 0}, 200, "easeInOutSine" )
			$('.sheet:first').animate({ marginLeft: 0}, 200, "easeInOutSine" );
			$('.bl_home:first').animate({ marginLeft: 0}, 200, "easeInOutSine" );
		},10);
	});
}




function activateButtons () {

	$('.button').hover(function(){
		clearTimeout(out);
		$(this).addClass('active').find('.buttoncontent:first').animate({ marginLeft: -25}, 250, "easeInOutSine" );
		console.log('IN');
	}, function(){
		$(this).removeClass('active').find('.buttoncontent:first').stop().animate({ marginLeft: 0}, 250, "easeInOutSine" );
		console.log('OUT');
	});
}


// <a id='nieuws' href='/project/nieuws/' class='button level2' rel='".$nieuws['id']."' style='top:".$nieuws['t']."px;left:".$nieuws['l']."px;width:".$nieuws['w']."px;height:".$nieuws['h']."px'>
// 	<div class='buttoncontent'>
// 		Donec suscipit nulla in mauris lacinia et aliquam metus vehicula. Sed bibendum ipsum vel turpis porta convallis. Aliquam nunc elit, lobortis nec mollis eget, tincidunt vitae neque. Curabitur dapibus imperdiet odio, nec varius metus fermentum id. Phasellus a mauris nisl, id fringilla lacus. Fusce fringilla molestie tortor vel laoreet. Donec convallis leo et augue sollicitudin bibendum. Praesent sed lectus ut dui condimentum adipiscing. Proin auctor sagittis mauris, eu gravida nunc eleifend sed. Nullam cursus, nisl vel pulvinar cursus, libero neque accumsan sem, vitae ultrices quam dui quis neque. Aliquam eu odio sit amet metus sagittis imperdiet vitae id mauris. Nam mollis feugiat lacus sit amet pretium. Nullam purus elit, vehicula id fringilla id, adipiscing vitae dui.
// 	</div>
// 	<div class='cut'>
// 		<div class='bl'></div>
// 		<div class='tr'></div>
// 	</div>
// 	<div class='l'></div>
// 	<div class='buttontitle'>".$nieuws['title_l1']."</div>
// </a>
var iqhreftime;
var qtimeout;
var json;
function addQuotes () {
if(!json)return false;
if(json.structurequotes){
	$.each(json.structurequotes, function(i,sq){

		if(sq.structure_id == pagequoteid){

			var qts = (sq.image)? json.artist_media: json.quotes;
			var qnr = Math.floor( Math.random() * qts.length );
			var q = qts[qnr];

			// try to prevent quoting the current page
			if (window.location.toString().match(q.url) ){
				var qnr = Math.floor( Math.random() * qts.length );
				var q = qts[qnr];
				if (window.location.toString().match(q.url) ){
					var qnr = Math.floor( Math.random() * qts.length );
					var q = qts[qnr];
					if (window.location.toString().match(q.url) ){
						var qnr = Math.floor( Math.random() * qts.length );
						var q = qts[qnr];
					}
				}
			}
			var leftorright = (sq.l<($(window).width()/2))? 'left': 'right'; 
			var clas = (sq.image)? ' imageq':' textq';

			if(sq.image){
				var rndm = Math.floor(Math.random() * 2);
				var leftorright = (rndm)? 'left': 'right';
			}
			else{
				var leftorright = (sq.l<($(window).width()/2))? 'left': 'right';
			}

			if(leftorright=='right'){
				var innercut = 
				$('<div/>').addClass('cut'+clas).append(
					$('<div/>').addClass('bl'),
					$('<div/>').addClass('tr')
				);
			}
			else{
				var innercut = 
				$('<div/>').addClass('cut'+clas).append(
					$('<div/>').addClass('tl'),
					$('<div/>').addClass('br')
				);
			}

			if(sq.image){
				var qcontent = 
				$('<div/>').addClass('quoteoverflow').append(
					$('<a/>').addClass('quotecontent').attr('href', q.url).append(
						$('<img/>').attr({ src:'/www/_images/content/'+q.static_filename+'_small.jpg' })
					)
				);
			}
			else{
				var mark = (q.section == 'reageer')? '#': '&&';
				var qcontent = 
				$('<div/>').addClass('quoteoverflow').append(
					$('<a/>').addClass('quotecontent '+q.section).attr('href', q.url+mark+q.sqlid).append(
						'&ldquo;'+q.content+'&rdquo;'
					).click(function(){
						setTimeout(function(){
							highlightQuote();
							hashnav.setlocation();
						})
					})
				);
			}



			$('body').append(

				$('<div/>').addClass('quote '+leftorright+clas).attr('rel',sq.sqlid).bind('click', gotoArtist).hover(function(){
					clearTimeout(qtimeout)
					$(this).addClass('superquote');
					var w = (sq.image)? $(this).find('.quotecontent').width()/3: $(this).find('.quotecontent').width() ;
					qtimeout = setTimeout(function(){
						$('.superquote').find('.quoteoverflow').animate({ width: $('.superquote').width()+w}, 250, "easeInOutSine" );
					},200);
				},function(){
					var w = $(this).width();
					clearTimeout(qtimeout);
					$('.superquote').find('.quoteoverflow').animate({ width: w*0.85}, 250, "easeInOutSine" );
					$('.superquote').removeClass('superquote');
				}).css({ top:sq.t, left: sq.l }).append(
					qcontent,
					innercut
				)
			)
		}
		
		

	})
}
else{
	
}
}

function gotoArtist () {
	if( $(this).hasClass('imageq') ) window.location.href = $(this).find('.quotecontent').attr('href');
}


var match;
var matchcount = 0;

function findQuote (quotenr) {

	var quote;

	$.each( json.quotes, function(i,q){

		if(q.sqlid.toString() == quotenr.toString()){
			quote = q.content.replace(/&quot;/g, '"').replace(/&apos;/g, "'");
		}
	})

	// WALK DOWN DOM FOR MATCHES
	$('*:not(.quotecontent)').each(function(){
		if( $(this).text().indexOf(quote)>0 ){
			match = $(this);
			matchcount++;
		}
	})

	// CANCEL IF NOT FOUND!
	if(matchcount<1) return false;


	/////////////////////////////////////// HIGHLIGHT

	// IF QUOTE ISNT SPREAD IN TAGS
	if( $(match).html().indexOf(quote)>0 ){
		$(match).html(
			$(match).html().replace(quote, '<span class="searchresult">'+quote+'</span>')
		)
		// alert('something is highlighted')
	}
	// ELSE SPLIT INTO WORDS
	else{

		function lookAgain(quotepiece, quote){

			var space = ( quotepiece.substr(quotepiece.length-2,1)== ' ' )? 'true': 'false';
			var quotepiece = ( quotepiece.substr(quotepiece.length-2,1)== ' ' )? quotepiece.substr(0,quotepiece.length-2): quotepiece.substr(0,quotepiece.length-1);

			if( $(match).html().indexOf(quotepiece)>0 ){

				$(match).html(
					$(match).html().replace(quotepiece, '<span class="searchresult">'+quotepiece+'</span>')
				)
				quotepiece = quote.substr(quotepiece.length, quote.length-quotepiece.length) + 'x';
				quote = quotepiece;
				lookAgain(quotepiece,quote);
			}

			else if( quotepiece.length>1 ) lookAgain(quotepiece, quote);
		}
		lookAgain(quote, quote);
	}
	$(document).scrollTop( $('.searchresult').offset().top-50 );
}



function str_replace(search, replace, subject) {

    var f = search, r = replace, s = subject;
    var ra = is_array(r), sa = is_array(s), f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;

    while (j = 0, i--) {
        while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
    };

    return sa ? s : s[0];
}

function is_array( mixed_var ) {

    return ( mixed_var instanceof Array );
}

