Array.prototype.inArray = function (value) {
    var i;
    for (i=0; i < this.length; i++) {
        if (this[i]===value) {
            return true;
        }
    }
    return false;
};

Array.prototype.removeItem = function (item) {
    var i=0;
    for (i=0; i < this.length; i++) {
        if (this[i]===item) {
            this.splice(i, 1);
            i--;
        }
    }
}

String.prototype.trim = function () {
    var str = this.replace(/^\s+|\s+$/g, '');
    for (var i = str.length - 1; i > 0; i--) {
        if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return str;
}

function default_value(c)
{
    if (c.value.trim() == c.defaultValue)
        { c.value = ''; }
    c.className=c.className+' darker';
};

function new_value(c)
{
    if (c.value.trim() == '') 
        { c.value = c.defaultValue; }
    else
        { c.value = c.value.trim(); }

    if(c.className.indexOf('darker') > -1)
    {
        var classArray = c.className.split(' ');
        classArray.removeItem('darker');
        c.className = classArray.join(' ');
    }
};

getPreviousSibling = function(el) {
    var tempNode = el.previousSibling;
    while(tempNode.nodeType != 1){
        tempNode=tempNode.previousSibling;
    }
    return tempNode;
}

checkSubmitValue = function(s) {
    var box = getPreviousSibling(s);
    if ((box.value.trim() == box.defaultValue) || (box.value.trim() == '')) {
        box.focus();
        return false;
    } else {
        return true;
    }
};

$(document).ready(function () {
    $('.gallery .media').scroller({itemClass: 'galleryline', perPage:15, auto: false});
    $('#content .gallery').galleryDialog({});
    
	if($('.blendcalendar').length)
	{
		//Rewire calendar links to enable calendar discovery behavior
		$('.boxes-link').each(function () {
			$(this).closest('td').css('cursor','hand');
			$(this).closest('td').data('rel', $(this).attr('rel'));
			$(this).closest('td').click(function () {
				$('.ezagenda_selected').removeClass('ezagenda_selected');
				$(this).addClass('ezagenda_selected');
				$('.quickview-list .day').fadeOut('fast');
				if($('#day_' + $(this).data('rel')).length > 0)
				{
					$('#day_' + $(this).data('rel')).fadeIn('slow');					
				}
				else
				{
					$('#day_none').fadeIn('slow');
				}
				return false;
			});
		});
		
		$('.quickview-list .day').hide();
		
		/*
		if($('#day_' + $('.ezagenda_selected .boxes-link').attr('rel')).length > 0)
		{
			$('#day_' + $('.ezagenda_selected .boxes-link').attr('rel')).show();			
		}
		else
		{
			$('#day_none').show();
		}
		*/
		$('.ezagenda_current .boxes-link').click();
		
		$('.calendar-select').change(function () { $(this).closest('form').submit(); })
		$('.calendar-submit').hide();
	}
    
    
});

jQuery.fn.galleryDialog = function (opts)
{
    var nextLink = opts.nextLink || '.next-image-link',
        prevLink = opts.prevLink || '.previous-image-link',
        linkSelector = opts.linkSelector || 'a.dlg',
        dialogWidth = opts.dialogWidth || 700,
        dialogHeight = opts.dialogHeight || 700,
        dialogClass = opts.dialogClass || 'gallery-dialog',
        dlg = null,
        nextButton = $('<a href="#">Next</a>');
        prevButton = $('<a href="#">Previous</a>');
        obj = this;
        
    $(linkSelector, this).click(function () {
        obj.openDialog($(this).attr('href'));
        return false;
    });
        
    this.openDialog = function (href){
        var a = $(this),
            widget = null,
            titlebar = null;
            
        if(dlg === null)
        {
            dlg = $('<div class="dialog-container" style="display: none"></div>'),            
            $(document).append(dlg);
            dlg.dialog({
                    modal: true,
                    dialogClass: dialogClass,
                    width: dialogWidth,
                    height: dialogHeight,
                    autoOpen: false
            });

            widget = dlg.dialog('widget');
            titlebar = $('.ui-dialog-titlebar', widget);
            
            titlebar.prepend(nextButton);
            $(nextButton).button({icons: {secondary: 'ui-icon-triangle-1-e'}});
            
            $(nextButton).click(function () {
                obj.loadDialogContent($(this).attr('href'));
                return false;
            });
            
            titlebar.prepend(prevButton);
            $(prevButton).button({icons: {primary: 'ui-icon-triangle-1-w'}});
            
            $(prevButton).click(function () {
                obj.loadDialogContent($(this).attr('href'));
                return false;
            });
        }
        
        obj.loadDialogContent(href);

    };
    
    this.loadDialogContent = function (href){
        dlg.load(href, null, function (data){
            if($(nextLink, dlg).length > 0)
            {
                $(nextButton).show();
                $(nextButton).attr('href', $(nextLink, dlg).attr('href'));
                $(nextLink, dlg).hide();
            }
            else
            {
                $(nextButton).hide();
            }
            
            if($(prevLink, dlg).length > 0)
            {
                $(prevButton).show();
                $(prevButton).attr('href', $(prevLink, dlg).attr('href'));
                $(prevLink, dlg).hide();
            }
            else
            {
                $(prevButton).hide(); 
            }
            
            $(nextLink, dlg).hide();
            $(prevLink, dlg).hide();
            
            dlg.dialog('open');
            
        });
        
    };
}
