Tag Archive for textmate

Jquery – Slideshow

// slideshow
 var currentPosition = 0;
 var slideWidth = 700;
 var slides = $('.slide');
 var numberOfSlides = slides.length;
 // Remove scrollbar in JS - It is added in CSS for user how does not have js enable
 $('.slidesContainer').css('overflow', 'hidden');
 // Wrap all .slides with #slideInner div
 slides // variable slides = .slide
 .wrapAll('<div id="slideInner"></div>').css({'float' : 'left','width' : slideWidth});
 // Float left to display horizontally, readjust .slides width
 /* 
 Set #slideInner width equal to total width of all slides
 #slideInner wraps all of our slides that has a width equal to total width of all .slide div.
 */
  $('#slideInner').css('width', slideWidth * numberOfSlides);
  
  // Insert left and right controls (with class names of .control) for user navigation;
    $('.slideshow')
      .prepend('<span class="control" id="leftControl">Move left</span>')
      .append('<span class="control" id="rightControl">Move right</span>');

    // Hide left arrow control on first load
    manageControls(currentPosition);

    // Create event listeners for .controls clicks
    $('.control')
      .bind('click', function(){
      // Determine new position
        currentPosition = ($(this).attr('id')=='rightControl')
      ? currentPosition+1 : currentPosition-1;

        // Hide / show controls
        manageControls(currentPosition);
        // Move slideInner using margin-left
        $('#slideInner').animate({
          'marginLeft' : slideWidth*(-currentPosition)
        });
      });

    // manageControls: Hides and shows controls depending on currentPosition
    function manageControls(position){
      // Hide left arrow if position is first slide
      if(position==0){ $('#leftControl').hide() }
      else{ $('#leftControl').show() }
      // Hide right arrow if position is last slide
      if(position==numberOfSlides-1){ $('#rightControl').hide() }
      else{ $('#rightControl').show() }
      }

});

source

objects into array

function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();
   
    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }
   
    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}

source

rand64

#include "stdlib.h"
#include "stdint.h"

uint64_t rand64()
{
	// Combin 4 parts of low 16-bit of each rand()
	uint64_t R0 = (uint64_t)random() << 48;
	uint64_t R1 = (uint64_t)random() << 48 >> 16;
	uint64_t R2 = (uint64_t)random() << 48 >> 32;
	uint64_t R3 = (uint64_t)random() << 48 >> 48;
	return R0 | R1 | R2 | R3;
}

#define SEGS 100
#define TIMES 50000

// Show segments
int main (int argc, char const *argv[])
{
	const uint64_t SEG = UINT64_MAX / SEGS;
	int f[SEGS] = { 0 };
	for(int i = 0; i < TIMES; ++i)
	{
		uint64_t r = rand64();
		f[r / SEG]++;
	}
	for (int i = 0; i < SEGS; ++i)
	{
		printf("#%2d: (%20llu ~ %20llu) %dn", i, i * SEG, (i + 1) * SEG - 1, f[i]);
	}
	return 0;
}

source

deblank

// Remove multiple space characters in a string.
// Ansi string and UTF-8 only!

void deblank(char* string)
{
	char* s = string;
	char* c = string;
	do
	{
		while (*c == ' ') c++;
		*s++ = *c;
		if (*c == ' ') break;
		if (*++c == ' ')
		{ 
			*s++ = ' ';
			c++;
		}
	} while (1);
}

int main()
{
	char s[] = "abc def  ghj   klmn.";
	deblank(s);
	printf("%sn", s);
	return 0;
}

source

substr.c

// Extract a substring from an existing string, return the length of the substring
// dst: receive the substring
// src: the source string to extract from
// start: extract start with the 'src' + 'start' char, zero based; no bound check
// len: length of the substring; may be shorter than expected.
// Ansi string only!

int substr(char* dst, char* src, int start, int len)
{
	char* d = dst;
	char* s = src + start;
	while (len-- > 0)
	{
		if (*s == ' ')
		{
			*d = '';
			return d - dst; 
		}
		*d++ = *s++;
	}
	*d = ' ';
	return d - dst;
}

int main()
{
	char s[1024] = { ' ' };
	printf("%dt%sn", substr(s, "0123456789", 3, 10), s);
}

source

convert int to binary in c

#define WIDTH 32

const char *int_to_binary ( int x ) 
{ 
	static char b[WIDTH + 1] = {0};
	for (int z = WIDTH; z > 0; z--) 
	{ 
		b[z - 1] = ((1 & x) == 1) ? '1' : '0'; 
		x >>= 1; 
	}
	return b; 
}

int main() 
{ 
	unsigned int i = 0; 
	i = ~i; 
	printf("%s
", int_to_binary(i)); 
	return 0; 
}

source

jQuery dynamic loader – load jquery using pure js javascript if jQ is undefined or does not exist

// jQuery dynamic loader - load jQuery using pure javascript js
// if jQuery is undefined or does not exist, then load it

if ( !window.jQuery ) {
	var jqscript = document.createElement('script');
	jqscript.setAttribute("type", "text/javascript");
	jqscript.setAttribute("src", "<a href="http://code.jquery.com/jquery.min.js");" >http://code.jquery.com/jquery.min.js");</a>
	document.getElementsByTagName("head")[0].appendChild(jqscript);
};

source

javascript function within a function to repeat itself js jquery jq loop looping

// animation functions
		zlpFX = {
			show1: function() {
				$('div#mainBackground2').fadeOut(3000);
				$('div#transMoving').animate({
					paddingLeft: "180px",
					paddingTop: "20px"
				},
				4000).find('h3').fadeOut(2000);
				setTimeout("zlpFX.show2()", 14000);
			},
			show2: function() {
				$('div#mainBackground2').fadeIn(4000);
				$('div#transMoving').animate({
					paddingLeft: '220px',
					paddingTop: '75px'
				},
				4500).find('h3').fadeIn(6000);
				setTimeout("zlpFX.show1()", 14000);
			}
		};




		// now start the animation functions
		setTimeout("zlpFX.show1()", 11000);

source

Detect Ajax

/* AJAX check  */
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
	/* special ajax here */
	die($content);
}

source

Generate thumbnail with ImageTool

ImageTool imgTool = new ImageTool()
imgTool.load($1.getBytes())
imgTool.thumbnail(260)
return imgTool.getBytes("JPEG")

source