var searchfieldtext = '';

$(document).ready(function() {

    if ($('#search').length > 0) {
	$("#search").autocomplete("/custom/search.php", {
	    onItemSelect: gotourl,
	    cacheLength: 0,
	    minChars: 2
	});

	$("#search").bind("focus", function(e) {
	    searchfieldtext = $("#search").val();
	    $("#search").val('');
	});

	$("#search").bind("blur", function(e) {
	    $("#search").val(searchfieldtext);
	});
    }

    if ($(".shop_images_chooser").length > 0) {
    
	$(".shop_images_chooser").bind("click", function(e) {
	
	    $("#CONTENT_shop_left_html2 > img").attr('src', this.value);
	});
    }

    if ($('#mycarousel').length > 0) {

        $('#mycarousel').jcarousel({
            visible: 1,
            scroll : 1,
            wrap : 'circular',
            itemLoadCallback: mycarousel_itemLoadCallback,
            itemVisibleOutCallback: {
            onAfterAnimation: mycarousel_itemVisibleOutCallback,
            buttonNextHTML: null,
            buttonPrevHTML: null

            }
        });
    }

    externalLinks();

	if ($('#shop_map_popup').length > 0) {
		
		$('#shop_map_popup').bind("click", function(e) {
			openWin(e.target);
			e.preventDefault();
		});
	}



    });



function gotourl(li) {
    $("#search").val('');
   
    location.href = $(li).attr('extra')[0];
    
    return false;

}

function mycarousel_itemLoadCallback(carousel, state) {

	if (state == 'init') {
	    return;
        }

        $.getJSON('/custom/windowajaxload.php', {
            index: carousel.first
        }, function(json) {
            addObject(carousel, carousel.first, json.window);
			externalLinks();
        });


}

function addObject(carousel, item, afterData) {

    carousel.add(item, afterData);

}

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {

        carousel.remove(i);

}

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
            anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}

function openWin(element) {
   
         var width  = 700;
         var height = 570;
         var left   = (screen.width  - width)/2;
         var top    = (screen.height - height)/2;
         var params = 'width='+width+', height='+height;
         params += ', top='+top+', left='+left;
         /*
         params += ', directories=no';
         params += ', location=no';
         params += ', menubar=no';
         params += ', resizable=no';
         params += ', scrollbars=no';
         params += ', status=no';
         */

        window.open(element.href,"mywindow",params);
        return false;
    

    

}
