/*
 $Revision: 81 $
 $Date: 2011-09-06 14:30:37 +0200 (Di, 06 Sep 2011) $
*/

$(document).ready(function() {

    /* _________________________________ Klasse für letzte Einträge */
    $('li:last-child , .column:last-child, .section:last-child').addClass('last');

    /* _________________________________ Vorschaubilder korrekt skalieren */
    //	$('.image-container img').imagesLoaded(function(){
    //		$('.image-container img').each(function() {
    //			$(this).aeImageResize({
    //			  height: 70,
    //			  width: 70
    //			});
    //			$(this).error(function() {
    //			  $(this).attr('src', '/assets/img/noimg_100.gif');
    //			});
    //			
    //			// Zoom Icon & Mouseover-event deaktivieren, wenn Größe != 100px oder "noimg_100.gif"
    //			$(this).imagesLoaded(function() {
    //			  if( ($(this).width() < 70 && $(this).height() < 70) || $(this).attr('src').match('noimg_100') ) {
    //				$(this).closest('a').prev().remove();
    //				_imgsrc = $(this).closest('a').html();
    //				$(this).closest('.image-container').unbind('mouseover');
    //			  } 
    //			});
    //		});
    //	});

    //        $('.ean-box img').imagesLoaded(function(){
    //                $('.ean-box img').each(function() {
    //                        $(this).aeImageResize({
    //                          height: 95,
    //                          width: 95
    //                        });
    //                        $(this).error(function() {
    //                          $(this).attr('src', '/assets/img/noimg_100.gif');
    //                        });
    //                });
    //        });

    /* _________________________________ Bildvorschau */
    //	$('.entry .image-container').bind('mouseover', function() {
    //		var object = $(this).closest('.entry').find('.image-container img');
    //		var large_image = object.attr('src');
    //		var html = '<div class="enlarged-image"><img id="largeimg" src="' + large_image + '" alt="Grosses Produktfoto" /></div>';
    //		$('body').append(html);
    //		$('#largeimg').aeImageResize({
    //			height: 300,
    //			width: 300
    //		});
    //		$('.enlarged-image').fadeIn();
    //	});

    //	$(document).mousemove(function(event) {
    //		var height_object = $('.enlarged-image').height();
    //		var width_object = $('.enlarged-image').width();
    //		var width_content = $('#container').width();
    //		if((width_object + event.clientX + 25) < width_content) {
    //			$('.enlarged-image').css('top', (event.pageY));
    //			$('.enlarged-image').css('left', (event.clientX) + 35);
    //		} else {
    //			$('.enlarged-image').css('top', (event.pageY));
    //			$('.enlarged-image').css('left', ((event.clientX) - width_object) - 35);
    //		}
    //	});
    //
    //	$('.entry .image-container').bind('mouseout', function() {
    //		$('.enlarged-image').remove();
    //	});

    //	/* _________________________________ Bildvorschau EAN */
    //        $('.ean-box-inner').bind('mouseover', function() {
    //                var object = $(this).find("img");
    //                var large_image = object.attr('src');
    //                var html = '<div class="enlarged-image"><img id="largeimg" src="' + large_image + '" alt="Grosses Produktfoto" /></div>';
    //                $('body').append(html);
    //                $('#largeimg').aeImageResize({
    //                        height: 300,
    //                        width: 300
    //                });
    //                $('.enlarged-image').fadeIn();
    //        });

    //        $(document).mousemove(function(event) {
    //                var height_object = $('.enlarged-image').height();
    //                var width_object = $('.enlarged-image').width();
    //                var width_content = $('#container').width();
    //                if((width_object + event.clientX + 25) < width_content) {
    //                        $('.enlarged-image').css('top', (event.pageY));
    //                        $('.enlarged-image').css('left', (event.clientX) + 35);
    //                } else {
    //                        $('.enlarged-image').css('top', (event.pageY));
    //                        $('.enlarged-image').css('left', ((event.clientX) - width_object) - 35);
    //                }
    //        });
    //
    //        $('.ean-box-inner').bind('mouseout', function() {
    //                $('.enlarged-image').remove();
    //        });

    /* _________________________________ Accordioneffekt Produktbeschreibung */	
    // Akkordeon-Effekt
    $('#listview').click(function() {

        //		if(openAccordeon) {
        //			// zuklappen, wenn "auf"
        //			$("#changeview a#listview").css("background-position", "0px 0");
        //			$('.toolbar').addClass('contract');
        //                	$(".description").each(function() {
        //                        	prodid = $(this).attr("name");
        //                        	$(this).load('/proddescription?short=true&id='+prodid+'&term='+encodeURI(searchterm), function() {
        //				       if(highlight == false) {
        //                 			       $(".pr_highlight").css("text-decoration", "underline");
        //                			}
        //				});
        //                	});
        //			openAccordeon = false;
        //			setAjaxCookie('extendedAccordeon', 0);
        //		} else {
        //			// aufklappen, wenn "zu"
        //			$("#changeview a#listview").css("background-position", "-34px 0");
        //			$('.toolbar').removeClass('contract');
        //                	$(".description").each(function() {
        //                        	prodid = $(this).attr("name");
        //                        	$(this).load('/proddescription?short=false&id='+prodid+'&term='+encodeURI(searchterm), function() {
        //					if(highlight == false) {
        //                                               $(".pr_highlight").css("text-decoration", "underline");
        //                                        }
        //				});
        //                	});
        //			openAccordeon = true;
        //			setAjaxCookie('extendedAccordeon', 1);
        //		}
        });

    $('.toolbar').addClass('contract');
	
    /* _________________________________ MF_EqualHeight Plugin */
    (function($) {
        $.fn.MF_EqualHeight = function() {
            var heights = 0;
            this.each(function(index) {
                if(heights < $(this).height()) {
                    heights = $(this).height();
                }
            });
            this.height(heights);
        };
    })(jQuery);

    $('#footer .column').MF_EqualHeight();
    $('.content-box .column').MF_EqualHeight();

    /* _________________________________ AutoComplete */
    $(function() {
        $("#search input").autocomplete({
            source: '/complete',
            minLength: 3
        });
    });
	
    $("#search input").bind("autocompleteopen", function(event, ui) {
        var search_input = $(this).val();
        $('.ui-menu-item a').each(function(index) {   
            var haystack = new RegExp(search_input, "gi");
            var new_text = $(this).text().replace(haystack,"<strong>"+search_input.charAt(0).toUpperCase() + search_input.substr(1).toLowerCase()+"</strong>");
            $(this).html(new_text);
            // hat der User "ENTER" gedrückt?
            $("#searchform").keyup(function(e) {
                if(e.keyCode == 13) {
                    $("#searchform").submit();
                }
            });
        });
    });

    /* _________________________________ Highlight */
    if(highlight == false) {
        highlightTerm(1);		
    //$("#marker").css("background-position", "-17px 0");
    }
        
    if (openAccordeon == false){
        loadDescription(1);
    }

    /* _________________________________ Toolbar Text kürzen */
    tb_max = 30;
    tb_text = $("#toolbar-text").text();
    if( tb_text.length > tb_max ) {
        $("#toolbar-text").css('font-size', '12px');
    } 

    /* _________________________________ Expander */
    /*$(".text-desc").expander({
			slicePoint: 100,
			preserveWords: true,
			widow: 4,
			expandText: '<span style="font-weight:bold">(mehr)</span>',
			expandPrefix: '… ',
			collapseTimer: 0,
			expandEffect: 'fadeIn',
			expandSpeed: 250,
			collapseEffect: 'fadeOut',
			collapseSpeed: 200,
			userCollapse: true,
			userCollapseText: '&nbsp;<span style="font-weight:bold">(weniger)</strong>',
			userCollapsePrefix: ' ',
	});
	*/

    // beim laden ausklappen, wenn cookie gesetzt
    if(openAccordeon == true) { 
        $('.toolbar').removeClass('contract');
    }
});

