﻿/* Tag functions */
function getTagsCloudy(placeHolderID,scale,percentClassPrefix,keepFreq,overwriteCSS){
      var list = $('#'+placeHolderID+' li');
      // overwrite CSS
      list.css(overwriteCSS);
      
      var max=0, min=0;
      //First pass : calc max and min 
      list.each(function (i) {
         //extract and parse frequency 
         freq = getTagFrequency($(this));
         //Calc max and min
         if (freq>max) max= freq;
         if (freq<min || min==0) min= freq;
      });
      
      //Second pass : calc percent
      list.each(function (i) {
         //extract and parse frequency 
         freq = getTagFrequency($(this));
         // Remove tags frequency ? 
         if (!keepFreq) $(this).html($(this).html().replace(/\n/g,'').replace(/\(\s*\d+\s*\)/g, '') );
         //Calc percentage 
         var percent = Math.ceil( ((freq-min+1)/(max-min+1)) * scale ); 
         $(this).attr('class',percentClassPrefix+percent);
      });
   }
   function getTagFrequency(x) {
      var matchs= x.text().replace(/\n/g,'').match(/\s*\d+\s*/g);
      if (!matchs) return 0;
      var freq = parseInt(matchs[matchs.length -1]);
      return freq>0? freq : 0;
   }

/* a basic contents menu plugin by Joel Birch. Demo page to come.
 * okay, so maybe the demo page will never come. Here is a simple usage:
 * $('#mainContentArea h2').contentMenu();
 * This would generate a list of all h2 elements text and link to those h2 elements
 * From there, you should find the other options useful for customising the markup generated
 * and also for changing where the list appears in the source order.
 *
 * updated 4th March 2007 to replace switch with 'object[method]()'
 * so now even less code
 *
 * updated 5th June 2007 to add hooks for enhancements (plugins to this plugin)
 * now stores menu widgets in array, $.cm and each menu widget stores its index as elem.serial expando
 */

(function($){
	$.fn.contentMenu = function(o){
		o = $.extend({
			"head"		: "<h4>Story Highlights</h4>",
			"beforeLink": "",
			"divClass"	: "contentMenu",
			"aClass"	: "inPage",
			"insertMethod"	: "before",
			"insertTarget"	: this.eq(0) }, o || {});
		$.cm = ($.cm) ? $.cm : [];
		var $list = $("<ul>"),
			lastInd = this.length-1,
			lis = '',
			s = $.cm.length,
			menu = $.cm[s] = $('<div class="'+o.divClass+'"></div>').append(o.head,$list);
			menu[0].serial = s;
		o.insertTarget[o.insertMethod](menu);
		return this.each(function(i){
			this.id = this.id || "menu"+s+"-el"+i;
			lis += '<li><a href="#'+this.id+'" class="'+o.aClass+'">'+o.beforeLink+'<em>'+$(this).text()+'</em></a></li>';
			if (i==lastInd){ $list.append(lis); }
		});
	};
})(jQuery);

  
/*
    CssBakery.com Captions - Javascript File
<img class="cssbakery_caption white cc" src="earthround.jpg" 
       alt="Blue Planet: Countries from three different continents, surrounded by Mediterranean, 
       Black, Red Seas, Pacific and Indian Oceans" />    
 */
 
function setUpImageCaption(imgClass, imgWidth, bg, floatCss){
     var cssbakery_colors = { medblue: '#4084e1', tealblue: '#3fbff4', marineblue: '#3f3ff4', 
           twig: '#855434', eggplant: '#5d2f4d', violet: '#c261e2', darkpink: '#e261a4'};

    
     $("img." + imgClass).each(function() {
    
       var theid = $(this).attr('id');     
       var saveclass = $(this).attr('class').replace(imgClass,'');
       var theclass = ' '+saveclass+' ';
       //theclass = theclass.replace(' cc ','');
       //theclass = theclass.replace(/ /g,''); // note use of global replace
       theclass = bg;
       if (theclass == '') theclass='black';
       if (!(typeof cssbakery_colors[theclass]=="undefined")) {
           theclass = cssbakery_colors[theclass];
       } 

       var src = $(this).attr('src');
       var caption = $(this).attr('alt');
       var idx = caption.indexOf(':');
       if (idx >= 0) {
           caption = "<strong>"+caption.substr(0,idx+1)+"</strong>"+caption.substr(idx+1);
       }  
       var w = imgWidth;
       if(w == null || w == '0'){ w = $(this).width();}
     // alert(w);
       var pb = $(this).css('padding-bottom'); 
       var pl = $(this).css('padding-left');
       if (theid != '') {
           theid = "id='"+theid+"' ";
       }
       var markup = "<div "+theid+" class= '" + floatCss + " cssbakery_image_wrapper "+saveclass+"' style='width:"+w+"px'>\n<img src='"+src+"' />\n<div class='cssbakery_transbox' style='background-color: "+theclass+"; left:"+pl+"; bottom:"+pb+";'></div>           <p style='left:"+pl+"; bottom:"+pb+";'>"+caption+"</p></div>";

       $(this).before(markup).remove();
       $('.cssbakery_image_wrapper').children('p').each( function() {
           var h = $(this).height();
           $(this).siblings('div').height(h+20);
       });
    }); 
}

$(document).ready(function(){
     var cssbakery_colors = { medblue: '#4084e1', tealblue: '#3fbff4', marineblue: '#3f3ff4', 
           twig: '#855434', eggplant: '#5d2f4d', violet: '#c261e2', darkpink: '#e261a4'};

     $("img.cssbakery_caption").each(function() {
       var theid = $(this).attr('id');     
       var saveclass = $(this).attr('class').replace('cssbakery_caption','');
       var theclass = ' '+saveclass+' ';
       theclass = theclass.replace(' cc ','');
       theclass = theclass.replace(/ /g,''); // note use of global replace
       if (theclass == '') theclass='black';
       if (!(typeof cssbakery_colors[theclass]=="undefined")) {
           theclass = cssbakery_colors[theclass];
       } 

       var src = $(this).attr('src');
       var caption = $(this).attr('alt');
       var idx = caption.indexOf(':');
       if (idx >= 0) {
           caption = "<strong>"+caption.substr(0,idx+1)+"</strong>"+caption.substr(idx+1);
       }  
       var w = $(this).width();
       var pb = $(this).css('padding-bottom');
       var pl = $(this).css('padding-left');
       if (theid != '') {
           theid = "id='"+theid+"' ";
       }
       var markup = "<div "+theid+" class='cssbakery_image_wrapper "+saveclass+"' style='width:"+w+"px'>\n<img src='"+src+"' />\n<div class='cssbakery_transbox' style='background-color: "+theclass+"; left:"+pl+"; bottom:"+pb+";'></div>           <p style='left:"+pl+"; bottom:"+pb+";'>"+caption+"</p></div>";

       $(this).before(markup).remove();
       $('.cssbakery_image_wrapper').children('p').each( function() {
           var h = $(this).height();
           $(this).siblings('div').height(h+20);
       });
    }); 
});

