// JavaScript Document

// need this to deal with possible namespace clashes involving other plugins
jQuery.noConflict();

// alow for regex in selectors
jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}

jQuery(document).ready(function($) {
	$("input:text, input:password").addClass("textField");
	$("input:button, input:submit").addClass("button");

    // give blog authors the ability to make an image a link to a new page instead of opening fancybox
	$(".wp-caption a").not('.directlink').fancybox();
    $(".gallery-item a").fancybox();

	var numEntries = $('.entry').length;
    var numGalleries = $('.gallery').length;

	var incr = '0';
	var galleryIncr = '0';

	addRelToThumbs();
    addRelToGallery();

	function addRelToThumbs() {
		if (incr < numEntries){
            $('.entry:eq(' + incr + ') img:regex(class, wp-image-*)').parent().not('.directlink').attr('rel', 'fancyThumbs' + incr);
            incr++;
			if (incr != numEntries) {
				addRelToThumbs();
			}
		}
	}

	function addRelToGallery(){
		if (incr < (numEntries + numGalleries)) {
			$('.gallery:eq(' + galleryIncr + ') img').parent().attr('rel', 'fancyThumbs' + incr + '');
            galleryIncr++;
			incr++;
			if (incr != numEntries + numGalleries) {
				addRelToGallery();
			}
		}
	}
});

