jQuery(function() {
  
  $('a.external').click(function(e){e.preventDefault(); window.open($(this).attr('href')); });
  
	/*
	Show global Ajax Loading Indicator. Currently disabled
	var loader = jQuery('<div id="loader"><img src="/images/loading.gif" alt="loading..." /></div>')
		.appendTo("#button_holder")
		.hide();
	jQuery().ajaxStart(function() {
		loader.show();
	}).ajaxStop(function() {
		loader.hide();
	}).ajaxError(function(a, b, e) {
		throw e;
	});*/
	
	$("a.colorbox_video").colorbox({opacity:0.8, width: 700, height: 425});
	$("a.colorbox_factbox").colorbox({width:950, height:600, iframe: true, opacity:0.8});
	
	// Slideshow effect
	if ($("div.image_scroller_module").length > 0) {
		var instance = $("div.image_scroller_module").scrollable({
			easing: 'easeOutQuad',
			size: 1,
			items: ".scroller .images",
			loop: false,
			prev: ".controls .previous",
			next: ".controls .next",
			keyboard: true,
			speed: 'slow',
			last_page_indicator: '.controls .page_indicator .last_page',
			current_page_indicator: '.controls .page_indicator .current_page'
		});
	}
	
	// Livesearch
	jQuery('.search_form_wrapper input[name="search"]').liveSearch({url: '/search?q='});
	
	// Infield-Labels
	$("label.inline").inFieldLabels();
});

function truncate_words(trunc, limit) {
	if (trunc.length > limit) {
	    trunc = trunc.substring(0, limit);
	    trunc = trunc.replace(/\w+$/, '');
	    trunc += '...';
	 }
	return trunc;
};


function merge_hash_with_string(my_hash, string) {
	for (var property in my_hash) {
    string = string.replace(":" + property, my_hash[property]);
  }
	return string;
}


function activateMedaScroller(div) {
	// Slideshow effect
	if ($(div).length > 0) {
		var instance = $(div + " div.additional_videos_and_controls").scrollable({
			easing: 'easeOutQuad',
			size: 1,
			items: ".scroller .bestcases",
			loop: false,
			prev: ".previous_control .previous",
			next: ".next_control_and_page_indicator .next",
			keyboard: true,
			speed: 'slow',
			last_page_indicator: '.next_control_and_page_indicator .page_indicator .last_page',
			current_page_indicator: '.next_control_and_page_indicator .page_indicator .current_page'
		});
	}
}


function detectVideoSupport() {
    var detect = document.createElement('video') || false;
    this.html5 = detect && typeof detect.canPlayType !== "undefined";
    this.mp4 = this.html5 && (detect.canPlayType("video/mp4") === "maybe" || detect.canPlayType("video/mp4") === "probably");
    this.ogg = this.html5 && (detect.canPlayType("video/ogg") === "maybe" || detect.canPlayType("video/ogg") === "probably");
    return this;
}

function replaceVideoWithObject(video_id) {    
    if(!video_id){ return false; }
    var video = document.getElementById(video_id);
    if(video){
        var obj = video.getElementsByTagName("object")[0];
        if(obj){
            var obj_copy = obj.cloneNode(true);
            video.parentNode.insertBefore(obj_copy, video);
						$("#" + video_id).remove();
        }
    }
}


function openColorBoxVideo(clip_id) {
	$.getJSON('/flowworks/clip/' + clip_id, function(data) {
    if (data.clips != null && data.clips.length > 0)
    {
      // Show first video
      var first_clip = data.clips[0];
      var url = first_clip.url;
			var link = "/flowworks/player_clip?url=" + url;
			$.fn.colorbox({href:link, opacity:0.8, width: 700, height: 425, iframe: true, scrolling: false});
    }
  });
}