﻿/*
*************************************************************************
* File:        jquery.gr8round.js                                       *
* Version:     1.0                                                      *
* Author:      Rob du Preez (www.pandasam.co.za)                        *
*                                                                       *
* Copyright 2011 Pandasam cc (www.pandasam.co.za), all rights reserved. *
*                                                                       **************************************************************************

This jQuery plugin creates rounded corners around an html structure by using small memory
based GIF's that are automatically modified to display the corners in the background color
of the parent element.

The advantage of this approach is that the resultant html will display exactly the same no
matter which browser the user is viewing the site with.

Format of call:

		$(sel).gr8round({size:sz,corners:'cnr',bgcolor:'clr',imgpath:'pth',image:bool,action:'act'});
		
		where:

			sz:		the size of the rounded border (integer between 0 and 48) - default is 12
					or element with width and height (eg. {width:12,height:6} )
			cnr:	corners to make (string) - default is 'all' - can include
					'topleft topright bottomleft bottomright'
			color:	default color of the rounded corners if no bgcolor found - default is #000000
			pth: 	relative path to images for IE7 and before - default is 'pics/'
			bool:	if rounding an image then set to true (default is false). If true, this
					forces the corner images to be placed absolutely over the element, otherwise
					a table structure is created for the corner images
			act:	Call with action:'resize' to clear all abs positioned images

		all parameters are optional.Examples of call:

		$('.buttons').gr8round();		$('.buttons').gr8round({size:10});		$('.buttons').gr8round({size:8,bgcolor:'#109FD9'});
		$('.title_round').gr8round({size:{width:12,height:8},corners:'topleft topright'});This approach works for Firefox, Chrome, IE8 and above.

If IE7 or below is detected then the GIF images are loaded directly from the server and must
be created manually in the required colors and have the names:

		round_tl_color.gif
		round_tr_color.gif
		round_bl_color.gif
		round_br_color.gif
		
		where color = the hex color code (lowerCase) without the # (eg. round_tl_109fd9.gif)
		Notes:
To create a new type of corner instead of the default rounded corners, create gif files
of size 48 x 48 with the corner shapes in white and the content space as transparent for
each corner and export them as ADAPTIVE gif files with COLOR TABLE of size 8. Then use a
hex editor such as HexEdit (freeware) and copy the bytes and paste into the corresponding
variable value in the code.If using with image: true and the corners position incorrectly after the window is resizedthen use the following code to clear and then recreate them:	$(window).resize( function() { 
		$('.roundimg').gr8round({action:'resize'});
		$('.roundimg').gr8round({size:12,image:true});
	});

*/
(function($) {
	$.fn.gr8round = function(options) {

		var colimg={};
		
		var keyString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

		var toBase64 = function(input) {
			var output = "";
			var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
			var i = 0;
			while (i < input.length) {
				chr1 = input.charCodeAt(i++);
				chr2 = input.charCodeAt(i++);
				chr3 = input.charCodeAt(i++);
				enc1 = chr1 >> 2;
				enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
				enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
				enc4 = chr3 & 63;
				if (isNaN(chr2)) { enc3 = enc4 = 64; } else if (isNaN(chr3)) { enc4 = 64; }
				output = output + keyString.charAt(enc1) + keyString.charAt(enc2) + keyString.charAt(enc3) + keyString.charAt(enc4);
			}
			return output;
		};
		
		var toHex = function(data) {
			var s='';
			for (var i=0; i<data.length; i++) {
				d='00'+data.charCodeAt(i).toString(16); d=d.substr(d.length-2);
				s+=d+' ';
			}
			return s.substr(s,s.length-1);
		};

		var colorToHex = function (c) {
		try {

			if (c===undefined) return c;
			if (c.indexOf('rgba')>=0) {
				var m = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+),\s*(\d+)/.exec(c);
				if (m[4]==0) return 'transparent';
			}
			var m = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(c);
			return m ? '#' + (1 << 24 | m[1] << 16 | m[2] << 8 | m[3]).toString(16).substr(1) : c;
		
		} catch(e) { 
			alert('colorToHex: '+e.message+'\n\n'+c); return '#FFFFFF';
		}
		};

		var makeImg = function (gifsrc,bgcolor,cnr) {
		
			var bcolor=cnr+bgcolor.substr(1);
			if (!runningInGr8stuff()) {
				if ( $.browser.msie ) {
					if( parseInt($.browser.version, 10)<8 ) { 
						if (colimg[bcolor]===undefined) {
							colimg[bcolor]='<img src="'+options.imgpath+'round_'+cnr+'_'+bgcolor.substr(1).toLowerCase()+'.gif" width='+options.size.width+' height='+options.size.height+'>';
						}
						return colimg[bcolor];
					}
				}
			}
			if (colimg[bcolor]===undefined) {
				var ts=gifsrc.split(' '), t='';
				var r=bgcolor.substr(1,2), g=ts[14]=bgcolor.substr(3,2), b=ts[15]=bgcolor.substr(5,2);
				var j=13;
				for (var i=0; i<1; i++) { ts[j]=r; ts[j+1]=g; ts[j+2]=b; j+=3; }
				for (var i=0; i<ts.length; i++) {  t+=String.fromCharCode(parseInt('0x'+ts[i])); }
				if (!runningInGr8stuff()) {
					colimg[bcolor]='<img src="data:image/gif;base64,'+toBase64(t)+'" width='+options.size.width+' height='+options.size.height+'>';
				} else {
					var f=options.imgpath+'round_'+cnr+'_'+bgcolor.substr(1).toLowerCase()+'.gif';
					var bf=parent.baseLoc(f), fd=parent.fileDir(bf), fd64=parent.fileDir(bf+'.b64');
					if (fd=='' && fd64=='') { parent.saveBinary(bf,toHex(t)); }
					colimg[bcolor]='<img src="'+options.imgpath+'round_'+cnr+'_'+bgcolor.substr(1).toLowerCase()+'.gif" width='+options.size.width+' height='+options.size.height+'>';
				}
			}
			return colimg[bcolor];
		};

		// Define default options
		// ----------------------
		var defaults = {
			size: 12,			// size of the rounded border (integer between 0 and 48)
								// or an element defining width and height
								// eg. size: {width:12, height:6}
			corners: 'all',		// corners to make (string) - default is all - can include
								// 'topleft topright bottomleft bottomright'
			bgcolor: '#000000',	// default bgcolor of the rounded corners if no bgcolor found
			imgpath: 'pics/',	// relative path to images for IE7 and before
			image: false,		// if rounding an image then set to true
			action: ''			// Call with action: resize to clear all abs positioned images
		};
		var options = $.extend(defaults, options);
		
		if (options.action=='resize') {
			$('.gr8round_image').each( function() { $(this).remove(); });
			return;
		}
		if (typeof(options.size)=='number') {
			if (options.size==0) return;
			var sz=options.size;
			options.size={width:sz,height:sz};
		} else {
			if (options.size.width===undefined || options.size.width==0) return;
			if (options.size.height===undefined || options.size.height==0) return;
		}
		if (options.corners=='all') {
			options.corners={tl:true,tr:true,bl:true,br:true};
		} else {
			var cnrs=options.corners;
			options.corners={tl:false,tr:false,bl:false,br:false};
			if (cnrs.indexOf('topleft')>=0) options.corners.tl=true;
			if (cnrs.indexOf('topright')>=0) options.corners.tr=true;
			if (cnrs.indexOf('bottomleft')>=0) options.corners.bl=true;
			if (cnrs.indexOf('bottomright')>=0) options.corners.br=true;
		}
		

		// Create the default corner gifs
		// ------------------------------
		var tl="47 49 46 38 39 61 30 00 30 00 b3 ff 00 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c0 c0 c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 21 f9 04 01 00 00 08 00 2c 00 00 00 00 30 00 30 00 40 04 88 10 c8 49 ab bd 38 4b a4 3b 40 60 28 8e e4 e8 7d 65 aa ae d7 ea be 29 05 cf 34 b8 d5 f8 7b ee 37 ce f7 39 56 26 18 ac 10 8f 36 14 12 a9 5c 1e 7f d0 89 2e 6a 59 52 9d a6 1d 36 a6 d9 ba 30 5e 58 35 2c 96 92 67 e6 73 59 4d 03 b2 59 6f 34 75 ee e6 d0 bb ed bb 8c a8 6f f2 e7 5e 51 67 3f 6c 5a 6f 1e 71 49 43 89 76 2d 8c 8a 46 8f 90 75 8c 91 92 21 96 97 8d 7e 97 69 9a 8a 9f 22 94 9d 9c 9a a5 a6 a1 26 a9 ab 29 11 00 3b 00";
		var tr="47 49 46 38 39 61 30 00 30 00 b3 ff 00 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c0 c0 c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 21 f9 04 01 00 00 08 00 2c 00 00 00 00 30 00 30 00 40 04 87 10 81 49 ab bd 38 eb 89 ba ff 60 08 6e 24 20 9e 68 2a 95 6a eb 8e d7 2b cb d5 6c bb dc 5d ee f5 dd f2 14 9f 6f 23 2c 76 2c c6 e4 4a 99 34 a9 80 50 a4 31 9a 61 7e a8 4e 2b 6c a7 4d 91 ba 3f 0c 18 27 1d 87 73 e6 73 fa 85 5e 3f dd b8 10 76 1e a4 d1 89 c2 7b b9 a8 cf 2a e9 63 54 69 50 6e 3c 70 2b 78 70 5f 87 47 55 8c 8d 31 8f 90 3d 92 93 7e 92 7b 95 94 95 90 9c 57 6d 9e a0 9c a2 a3 9e 23 a6 57 a8 1e 00 11 00 3b 00";
		var bl="47 49 46 38 39 61 30 00 30 00 b3 ff 00 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c0 c0 c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 21 f9 04 01 00 00 08 00 2c 00 00 00 00 30 00 30 00 40 04 86 10 c9 49 ab bd 38 4f 00 b4 ff 20 c7 81 64 29 89 68 67 ae 56 ea 8a ac f9 ce 74 5c a9 76 4e c1 ba 9e f6 36 1a 0e e8 11 ce 88 37 a3 f2 58 1a 22 57 ce 67 33 2a fd f0 aa a4 1f d6 aa dd 66 6a de 96 31 bc 59 8e b6 e6 d7 33 ad cc b1 df e0 1d 39 34 e7 d6 35 e7 fb 25 af 4f f6 f7 7c 7f 08 28 82 65 81 7d 5d 7f 2e 85 6a 8a 8d 7a 42 90 91 75 4b 73 69 61 6f 68 70 55 70 84 44 9d 8f 31 a0 71 32 a3 66 5c a6 a9 00 11 00 3b 00";
		var br="47 49 46 38 39 61 30 00 30 00 b3 ff 00 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c0 c0 c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 21 f9 04 01 00 00 08 00 2c 00 00 00 00 30 00 30 00 40 04 87 10 c9 49 ab bd 38 4b a0 bb ff 00 f7 8d 24 12 86 65 7a 9d ac a8 8e 6d 7c be 95 6c df 74 6e b9 3a 3d f7 af 16 10 86 1b 52 6e 45 22 72 99 34 aa 78 ce 12 34 0a 9a 52 35 bf 6b 47 a8 cd c8 ba 2b 1b 78 c2 ec 32 59 ce b3 d8 a7 2e d7 da 70 e4 18 34 f7 58 eb 47 bc f7 ae 47 e9 c3 7f 3b 7e 81 1b 59 84 68 84 26 88 81 31 87 8d 7f 6b 75 72 92 93 60 67 96 6a 5a 71 54 71 83 3d 9d 8b 41 a0 8f 29 a3 4d 18 a6 a9 aa 9d 11 00 3b 00";
		
		var tblstruc='<TABLE cellSpacing=0 cellPadding=0 width="100%"{style}><TR><TD height={height} width={width}>{img_tl}</TD><TD height={height}></TD><TD height={height} width={width}>{img_tr}</TD></TR><TR><TD></TD><TD{attr}>{boxcontent}</TD><TD></TD></TR><TR><TD height={height} width={width}>{img_bl}</TD><TD height={height}></TD><TD height={height} width={width}>{img_br}</TD></TR></TABLE>';

		var img_tl='', img_tr='', img_bl='', img_br='';
var x=true;
		return this.each(function() {

			// Find the parent background color
			// --------------------------------
			var p=$(this).parent(), bgcolor=colorToHex($(p).css('background-color'));
			while (bgcolor===undefined || bgcolor=='transparent') {
				p=$(p).parent();
//				if(p.html()===null) { bgcolor=options.bgcolor; break; }
				if(p.html()===null) { return; }
				bgcolor=colorToHex($(p).css('background-color'));
			}
//if (x) { x=false; alert('bgcolor='+bgcolor); }
		
			// Create base64 gifs using background color
			// ------------------=----------------------
			if (options.corners.tl) img_tl=makeImg(tl,bgcolor,'tl'); else img_tl='';
			if (options.corners.tr) img_tr=makeImg(tr,bgcolor,'tr'); else img_tr='';
			if (options.corners.bl) img_bl=makeImg(bl,bgcolor,'bl'); else img_bl='';
			if (options.corners.br) img_br=makeImg(br,bgcolor,'br'); else img_br='';

			if (options.image) {
				// Create absolutely positioned corner images and position over image
				// -----------------------------------------------------------------
				var style=' style="position:absolute; top:{top}; left:{left}" class="gr8round_image"',
					p=$(this).offset(),ist='';
				if (img_tl!='') {
					ist=style.replace(/\{top\}/g,p.top).replace(/\{left\}/g,p.left);
					img_tl=img_tl.replace(/\>/,ist+'>');
					$('body').append(img_tl);
				}
				if (img_tr!='') {
					ist=style.replace(/\{top\}/g,p.top).replace(/\{left\}/g,p.left+$(this).width()-options.size.width);
					img_tr=img_tr.replace(/\>/,ist+'>');
					$('body').append(img_tr);
				}
				if (img_bl!='') {
					ist=style.replace(/\{top\}/g,p.top+$(this).height()-options.size.height).replace(/\{left\}/g,p.left);
					img_bl=img_bl.replace(/\>/,ist+'>');
					$('body').append(img_bl);
				}
				if (img_br!='') {
					ist=style.replace(/\{top\}/g,p.top+$(this).height()-options.size.height).replace(/\{left\}/g,p.left+$(this).width()-options.size.width);
					img_br=img_br.replace(/\>/,ist+'>');
					$('body').append(img_br);
				}

			} else {

				// Replace with corners table
				// --------------------------
				var attr='', style='';
				if ($(this).attr('class')!==undefined) attr+=' class="'+$(this).attr('class')+' gr8rounded'+'"'; else attr+=' class="gr8rounded"';
				if ($(this).attr('align')!==undefined) attr+=' align="'+$(this).attr('align')+'"';
				var bc=$.trim($(this).html().replace(/\t/g,'')), a='', ae='',y=0;
//				if (!$.browser.msie) {
					if (bc.toLowerCase().indexOf('\<a')==0) {
						y=bc.indexOf('>'); a=bc.substr(0,y)+'>';
						bc=bc.substr(y+1).replace(/\<\/a\>/i,''); ae='</a>';
						style=' style="cursor:pointer"';
//if (x) { x=false; alert($.trim($(this).html().replace(/\t/g,''))+'\n**\n'+a+'\n'+bc+'\n'+ae); }
					}
//				}
				var nhtm=a
//						+tblstruc.replace(/\{size\}/g,options.size)
						+tblstruc.replace(/\{height\}/g,options.size.height)
						.replace(/\{width\}/g,options.size.width)
						.replace(/\{style\}/g,style)
						.replace(/\{attr\}/g,attr)
						.replace(/\{img_tl\}/g,img_tl)
						.replace(/\{img_tr\}/g,img_tr)
						.replace(/\{img_bl\}/g,img_bl)
						.replace(/\{img_br\}/g,img_br)
						.replace(/\{boxcontent\}/g,bc)
						+ae
				;
				$(this).html(nhtm);
//if (x) { x=false; alert($(this).css('width')); }
				if ($(this).css('width')===undefined) $(this).width($(this).width()+options.size.width);

//if (x) { x=false; alert(nhtm); }
//if (x) { x=false; alert($(this).parent().html()); }
			}
		});
    };
	
})(jQuery); 

/*
*************************************************************************
* File:        jquery.gr8titletips.js                                       *
* Version:     1.0                                                      *
* Author:      Rob du Preez (www.pandasam.co.za)                        *
*                                                                       *
* Copyright 2011 Pandasam cc (www.pandasam.co.za), all rights reserved. *
*                                                                       **************************************************************************

*/(function($) {
	$.fn.gr8titletips = function(options) {

		// Define default options
		// ----------------------
		var defaults = {
			color: '#FFFFFF',					// Text color in full hex format
			bgcolor: '#000000',					// bgcolor of the titletip in full hex format
			padding: '5px 5px 5px 5px',			// padding style for text
			textstyle: 'letter-spacing: 1px',	// Additional style css
			shadowcolor: '#C0C0C0',				// shadow color in hex format
			shadowpadding: '3px 3px 3px 3px',	// padding style for shadow
			offset: 5,							// offset from element in pixels
			position: 'bottomleft'				// bottomleft, bottomcenter, bottomright
												// topleft, topcenter, topright
												// left, right
		};
		var options = $.extend(defaults, options);
		
		if ($('#gr8titletip').size()<1) $('body').prepend('<div id=gr8titletip style="position:absolute; top:0px; left:0px; background-color: transparent; display:none"></div>');
		return this.each(function() {
			if ($(this).attr('title')!==undefined && $(this).attr('title')!='') {
				$(this).data('title',$(this).attr('title')); $(this).attr('title','');
				$(this).data('options',options);
				$(this).mouseover(function() {
					var ts=options.textstyle;
					if (ts!='') ts='; '+ts;
					var tt=$('#gr8titletip'), top=0, left=0,
						htm='<div style="background-color:'+options.shadowcolor+
						'; padding:'+options.shadowpadding+'">'+
						'<div style="background-color:'+options.bgcolor+'; color:'+options.color+
						'; padding:'+options.padding+ts+'">'+$(this).data('title')+'</div>'+
						'</div>'
						;
					tt.css('top',0).css('left',0);
					tt.html(htm), ttw=tt.width(), tth=tt.height();
					var e=$(this), p=e.offset(), w=e.width(), h=e.height();
					switch (options.position) {
					case 'left': { top=p.top+((h-tt.height())/2); left=p.left-ttw-options.offset; break;}
					case 'right': { top=p.top+((h-tth)/2); left=p.left+w+options.offset; break;}
					case 'bottomleft': { top=p.top+h+options.offset; left=p.left; break;}
					case 'bottomcenter': {  top=p.top+h+options.offset; 
											if (w<ttw) { left=p.left-((ttw-w)/2);
											} else {
												if (w=ttw) { left=p.left;
												} else { left=p.left+((w-ttw)/2); }
											}
											break;}
					case 'bottomright': { top=p.top+h+options.offset; left=p.left+w-ttw; break;}
					case 'topleft': { top=p.top-options.offset-tth; left=p.left; break;}
					case 'topcenter': { top=p.top-options.offset-tth; left=p.left;
										if (w<ttw) { left=p.left-((ttw-w)/2);
										} else {
											if (w=ttw) { left=p.left;
											} else { left=p.left+((w-ttw)/2); }
										}
										break;}
					case 'topright': { top=p.top-options.offset-tth; left=p.left+w-ttw; break;}
					default: { top=p.top+h+options.offset; left=p.left; }
					}
					if (left<0) left=0;
					if (top<0) top=0;
					tt.css('top',top).css('left',left).css('display','inline');
//					alert($(this).data('title'));
				});
				$(this).mouseout(function() {
					$('#gr8titletip').css('display','none');
				});
				if ($.browser.msie) {
					$(this).click(function() {
						if ($(this).find('.gr8rounded').size()>0) {
							if ($(this)[0].nodeName.toLowerCase()=='a') {
								var href=$.trim($(this).attr('href'));
								if (href.toLowerCase().indexOf('javascript:')==0 || href.toLowerCase().indexOf('jscript:')==0) {
									href=href.substr(href.indexOf(':')+1);
									$.globalEval(href);
								} else { window.location.href=href; }
							}
						}
					});
				}
			}
		});
	};
})(jQuery); 

