jQuery.noConflict();
jQuery(document).ready(function() {
	init();
});

function init() {
	// open external links in new window (checking domain first)
	jQuery("a[href^='http']:not([href*='mind.sh'])").each(function() {
		jQuery(this).attr("target", "_blank");
	});
	
	jQuery("#suckerfishnav li:last-child ").css("margin-right","0");

	var mouse_is_inside = false;
	var panel_is_open = false;

	// Expand Panel
	jQuery("#open").click(function(){
		jQuery("div#panel").slideDown("slow");
		panel_is_open = true;
	});	
	// Collapse Panel
	jQuery("#close").click(function(){
		jQuery("div#panel").slideUp("slow");
		panel_is_open = false;
	});	
	
	jQuery('#toppanel').hover(function(){ 
		mouse_is_inside = true; 
	}, function(){ 
		mouse_is_inside = false; 
	});
	
	jQuery("body").mouseup(function(){
		//console.log(panel_is_open);
		if(!mouse_is_inside && panel_is_open) {
			jQuery("div#panel").slideUp("slow");
			jQuery("#toggle a").toggle();
			panel_is_open = false;
		}
	});
		
	// Switch buttons from "Log In | Register" to "Close Panel" on click
	jQuery("#toggle a").click(function () {
		jQuery("#toggle a").toggle();
	});
	
	jQuery('a[href=#top]').click(function(){
        jQuery('html, body').animate({scrollTop:0}, 400);
        return false;
    });
	
	jQuery('#slides').jcarousel({
		auto: 6,
		scroll: 4,
		wrap: 'circular',
		buttonPrevHTML: null,
		buttonNextHTML: null,
		initCallback: mycarousel_initCallback
	});	
	
	if (typeof(mcms_query) != 'undefined') {
		var area;
		var i;
		var s;
		for(s in mcms_areas) {
			area = jQuery(mcms_areas[s]);
			if(area.length != 0) {
				for(var l = 0; l < area.length; l++) {
					for(i in mcms_query) {
						area.eq(l).highlight(mcms_query[i], 1, 'highlight term-' + i);
					}
				}
				break;
			}
		}
	}
	/*
	// work hover animation
    jQuery('#slides .portfolio-entry, #slides .post-entry').each(function(settings) {
        settings = {'durationIn': 180, 'durationOut': 350, 'easingIn': 'easeOutQuad', 'easingOut': 'easeInCirc'};
        jQuery(this).hover(function() {
            h = jQuery('.entry-title', this).outerHeight();
            jQuery('.entry-title', this).stop().css({top: '-' + h + 'px', visibility: 'visible'}).animate({top:'0'}, settings.durationIn, settings.easingIn);
            jQuery('.more-link', this).animate({bottom:'-10px'}, settings.durationIn, settings.easingIn);
            if(!(jQuery.browser.msie && jQuery.browser.version < 9)) // exclude ie8- because of their poor support for png
                jQuery('.extra', this).delay(100).fadeIn('slow');
        }, function() {
            h = jQuery('.entry-title', this).outerHeight();
            jQuery('.entry-title', this).stop().animate({top: '-' + h + 'px'}, settings.durationOut, settings.easingOut);
            jQuery('.more-link', this).animate({bottom:'-40px'}, settings.durationOut, settings.easingOut);
            if(!(jQuery.browser.msie && jQuery.browser.version < 9)) // exclude ie8- because of their poor support for png
                jQuery('.extra', this).fadeOut();
        });
    });
	
	// belt animation (max one belt per page)
    belt = jQuery('ul#slides');
    belt_slots = jQuery('li', belt).length;
    left_slots = belt_slots - 4;
    jQuery('#slides .more').click(function() {
        duration = 'slow';
        easing = 'easeOutQuad';
        if(left_slots > 0) {
            belt.animate({marginLeft: '-=960px'}, duration, easing);
            left_slots -= 4;
        }
        else if(belt_slots > 4){
            belt.animate({marginLeft: '0px'}, duration, easing);
            left_slots = belt_slots - 4;
        }
    }
    );*/
	
}


// search term hilighter
jQuery.fn.extend({
	highlight: function (term, insensitive, span_class) {
		var regex = new RegExp('(<[^>]*>)|(\\b' + term.replace(/([-.*+?^${}()|[\]\/\\])/g, "\\$1") + ')', insensitive ? 'ig' : 'g');
		return this.html(this.html().replace(regex, function (a, b, c) {
			return (a.charAt(0) == '<') ? a : '<span class="' + span_class + '">' + c + '</span>';
		}));
	}
});

function mycarousel_initCallback(carousel) {
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});
	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});
	
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

/* jQuery replaceText - v1.1 - 11/21/2009, (c) 2009 Ben Alman, http://benalman.com/about/license/ */
(function($){$.fn.replaceText=function(b,a,c){return this.each(function(){var f=this.firstChild,g,e,d=[];if(f){do{if(f.nodeType===3){g=f.nodeValue;e=g.replace(b,a);if(e!==g){if(!c&&/</.test(e)){$(f).before(e);d.push(f)}else{f.nodeValue=e}}}}while(f=f.nextSibling)}d.length&&$(d).remove()})}})(jQuery);

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-16159409-18']);
_gaq.push(['_setDomainName', '.mind.sh']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
