// JavaScript Document


$(window).load(function()
	{
		/*
		$('img').load(function()
			{
				alert('image load');
				$(this).fadeIn(600);
			}).each(function(){ 	// IE workaround as load doesn't fire on cached images
				if(this.complete && (jQuery.browser.msie)) {
					$(this).trigger("load");
				}
			});
		*/
	});


function showPage() {
	//
	$('body').fadeIn(600);
	//
}

function getAnchor() {
	//
	// Requires jquery.url.js plugin
	//
	var anch = jQuery.url.attr("anchor") ? jQuery.url.attr("anchor") : false;
	return anch;
	//
}

function limitWinHeight() {
	//
	if ($(window).height() < 700) {
		$('#content-wrapper').css("top", 350);
		$('#logo').css("top", 649);
		$('#logo').css("position", 'absolute');
		$('#nav').css("position", 'absolute');
		$(window).bind('scroll', positionNav);
		positionNav();
	} else {
		$(window).unbind('scroll', positionNav);
		$('#content-wrapper').css("top", '50%');
		$('#logo').css("position", 'fixed');
		$('#nav').css("position", 'fixed');
		$('#logo').css("top", '');
		$('#logo').css("bottom", 25);
		$('#logo').css("left", 25);
		$('#nav').css("left", 25);
	}
}

function positionNav() {
	//
	$('#nav').css("left", $(window).scrollLeft()+25);
	$('#logo').css("left", $(window).scrollLeft()+25);
	//
	/*
	$('#nav, #logo').animate(
		{
			left: $(window).scrollLeft()+25
		},
		{
			duration: 300,
			easing: 'easeInOutQuad'
		});
	*/
}



function jumpTo(project_name, offset) {
	//
	$.scrollTo( '#'+project_name, 1200 , { offset:offset });
	//
}

function getCurrentProject(setHash) {
	//
	$('.intro').each(function() {
		if (testProjectPosition($(this).attr("id"))) {
			//alert("curr project = "+$(this).attr("id"));
			var curr = $(this).attr("id");
			$('#project-nav a').each(function() {
				if ($(this).attr("href") == '#'+curr) {
					$(this).attr("class", "selected");
					//if (setHash) { window.location.hash = "#"+curr; }
				} else {
					$(this).attr("class", "");
				}
			});
			return;
		}						  
	});
}

function testProjectPosition(project_name) {
	//
	if ($('#'+project_name).offset().left <= $(window).scrollLeft()+($(window).width()/2) && $('#end-'+project_name).offset().left > $(window).scrollLeft()+($(window).width()/2)) {
		return true;
	}
	return false;
}


// JS versions of PHP functions

function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/in_array
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
    return false;
}

function nl2br (str, is_xhtml) {
    // Converts newlines to HTML line breaks  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/nl2br
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Maximusya
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');
    // *     returns 1: 'Kevin\nvan\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '\nOne\nTwo\n\nThree\n'
    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br>';
 
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}

function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/str_replace
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}
