var currentSearchTerm = "";

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).outerHeight(true);
	var ph = $(this).parent().outerHeight(true);
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};
})(jQuery);

jQuery.fn.liveSearch = function (conf) {
    var config = jQuery.extend({
        url:            '/search-results.php?q=', 
        id:                'jquery-live-search', 
        duration:        400, 
        typeDelay:        200,
        loadingClass:    'loading', 
        onSlideUp:        function () {}, 
        uptadePosition:    false
    }, conf);

    var liveSearch    = jQuery('#' + config.id);

    // Create live-search if it doesn't exist
    if (!liveSearch.length) {
        liveSearch = jQuery('<div id="' + config.id + '"></div>')
                        .appendTo(document.body)
                        .hide()
                        .slideUp(0);

        // Close live-search when clicking outside it
        jQuery(document.body).click(function(event) {
            var clicked = jQuery(event.target);

            if (!(clicked.is('#' + config.id) || clicked.parents('#' + config.id).length || clicked.is('input'))) {
                liveSearch.slideUp(config.duration, function () {
                    config.onSlideUp();
                });
            }
        });
    }

    return this.each(function () {
        var input                            = jQuery(this).attr('autocomplete', 'off');
        var liveSearchPaddingBorderHoriz    = parseInt(liveSearch.css('paddingLeft'), 10) + parseInt(liveSearch.css('paddingRight'), 10) + parseInt(liveSearch.css('borderLeftWidth'), 10) + parseInt(liveSearch.css('borderRightWidth'), 10);

        // Re calculates live search's position
        var repositionLiveSearch = function () {
            var tmpOffset    = input.offset();
            var inputDim    = {
                left:        tmpOffset.left, 
                top:        tmpOffset.top, 
                width:        input.outerWidth(), 
                height:        input.outerHeight()
            };

            inputDim.topPos        = inputDim.top + inputDim.height;
            inputDim.totalWidth    = inputDim.width - liveSearchPaddingBorderHoriz;

            liveSearch.css({
                position:    'absolute', 
                left:        inputDim.left + 'px', 
                top:        inputDim.topPos + 'px',
                width:        inputDim.totalWidth + 'px'
            });
        };

        // Shows live-search for this input
        var showLiveSearch = function () {
            // Always reposition the live-search every time it is shown
            // in case user has resized browser-window or zoomed in or whatever
            repositionLiveSearch();

            // We need to bind a resize-event every time live search is shown
            // so it resizes based on the correct input element
            $(window).unbind('resize', repositionLiveSearch);
            $(window).bind('resize', repositionLiveSearch);

            liveSearch.slideDown(config.duration);
        };

        // Hides live-search for this input
        var hideLiveSearch = function () {
            liveSearch.slideUp(config.duration, function () {
                config.onSlideUp();
            });
        };

        input
            // On focus, if the live-search is empty, perform an new search
            // If not, just slide it down. Only do this if there's something in the input
            .focus(function () {
                if (this.value !== '') {
                    // Perform a new search if there are no search results
                    if (liveSearch.html() == '') {
                        this.lastValue = '';
                        input.keyup();
                    }
                    // If there are search results show live search
                    else {
                        // HACK: In case search field changes width onfocus
                        setTimeout(showLiveSearch, 1);
                    }
                }
            })
            // Auto update live-search onkeyup
            .keyup(function () {
                // Don't update live-search if it's got the same value as last time
                if (this.value != this.lastValue) {
					currentSearchTerm = this.value;
					
                    input.addClass(config.loadingClass);

                    var q = this.value;

                    // Stop previous ajax-request
                    if (this.timer) {
                        clearTimeout(this.timer);
                    }

                    // Start a new ajax-request in X ms
                    this.timer = setTimeout(function () {
                        jQuery.get(config.url + q, function (data) {
							if(currentSearchTerm == q) {
								input.removeClass(config.loadingClass);
	
								// Show live-search if results and search-term aren't empty
								if (data.length && q.length) {
									liveSearch.html(data);
									showLiveSearch();
								}
								else {
									hideLiveSearch();
								}
							}
                        });
                    }, config.typeDelay);

                    this.lastValue = this.value;
                }
            });
    });
};

$.fn.cycle.transitions.scrollHorz = function($cont, $slides, opts) {
	$cont.css('overflow','hidden').width();
	opts.before.push(function(curr, next, opts, fwd) {
		$.fn.cycle.commonReset(curr,next,opts);
		opts.cssBefore.left = fwd ? (next.cycleW-1) : (1-next.cycleW);
		opts.animOut.left = fwd ? -curr.cycleW : curr.cycleW;
	});
	opts.cssFirst = { left: 0 };
	opts.cssBefore= { top: 0 };
	opts.animIn   = { left: 0 };
	opts.animOut  = { top: 0 };
};

jQuery(document).ready(function() {
	var $slideshowContainer = jQuery("#slideshow");
	
	
	/* vertically center slides */
	$slideshowContainer
		.find(".slide_container img")
			/*.vAlign()*/
		.end()
		.find("ul.slides")
			.css({'visibility':'visible'});
	/* end vertically center slides */
								
															
	/* setup text shadows for IE */
	if($.browser.msie) {
		jQuery("body")
			.find("#navigation")
				.find("a")
					.textShadow()
				.end()
			.end()
			.find("h1")
				.textShadow()
			.end()
			.find("#sidebar")
				.find("h2")
					.textShadow()
				.end()
			.end()
		.end();
	}
	/* END setup text shadow for IE */
	
	
	jQuery(".gallery.home")
		.click(function() {
			var url = jQuery(this).find("a").attr("href");
			if(url && url != "") window.location = url;
		})
	.end();
	
	jQuery("#sidebar")
		.find("li.current_page_item")
			.each(function() {
				var height = jQuery(this).children("a").innerHeight();
				if(height && height != "") {
					jQuery(this)
						.append("<div id='current_arrow'></div>")
						.find("#current_arrow")
							.css("top", (Math.floor(height / 2) - 1) + "px");
				}
			})
		.end()
	.end();
	
	
	$slideshowContainer
		.find(".slides")
			.cycle({
				fx: 'fade',
				timeout: slideDisplayTime,
				speed: 500,
				pause: true,
				next: '#slideshow_button',
				cleartype: false
			})
		.end()
	.end();		
								
	jQuery("#searchform")
		.find("#searchsubmit")
			.val("")
		.end()
		.find("#s")
			.liveSearch({
				url: (siteurl + "/live-search.php?s="),
				id: "jquery-live-search",
				duration: 200,
				typeDelay: 150
			})
		.end()
	.end();
	
	jQuery("a[class^='lightbox']").lightBox();
	jQuery("a[class^='sec_lightbox']").lightBox();
});
