/*
 * Couponmonkey // Main JS
 * 
 * This file contains all required functions and configurations
 * for the Couponmonkey website.
 *
 * Copyright 2008 Stefan Heckler
 * 
 * @author: Stefan Heckler
 * @version: $Id: cm.js 29 2008-10-26 02:06:20Z sh $
 */

// general configuration
var cmConfig = new Array();
	
	// url definitions
	cmConfig['baseurl'] = '/';
	cmConfig['mediaurl'] = cmConfig['baseurl']+'media/';
 

/*
 * cufón configuration
 * 
 * We replace all h1 and h2 tags on the website with "Georgia Italic"
 * h1-Element on rootpage has various fontsize
 */

Cufon.replace('.MainFooter h3, .HomeTeaser h2, .HomeTeaser em, .HomeTeaserSearchText, .ContentBox h2, .ContentBox h1, .HomeText h3');

function showPopdown()
{
    window.open('/newsletter_popdown.php', 'Meingutscheincode.de Newsletter', 'width=350,height=350,resizable=false');
}


$(document).ready(function(){
    window.onBlur = window.focus();
	$(".ShopAlternativeRedirect .ShopLink").click(function(){

		$(".ShopAlternativeRedirect .CouponCode").css('visibility', 'visible');
		$(".ShopAlternativeRedirect .CouponCode").css('float', 'none');
		$(".ShopAlternativeRedirect .CouponCodeNote").remove();

		return true;
	});


    // input reset function
    $('.HomeTeaserSearchInput, .QuickSearch input[type="text"]').focus(function(){
        if($(this).val() == 'z.B. Zooplus, MyDays, Neckermann ...' || $(this).val() == 'z.B. Zooplus, MyDays, ...') $(this).val('');
        $('ul.ui-autocomplete').addClass('from-' + $(this).attr('class'));
    });


    $('.RedeemVoucher').click(function(){

       if(!$(this).hasClass("ShopLink"))
       {
           pageTracker._trackPageview("/success");
       }
    	

    });

    /**
     * Search Autocomplete
     */
    $.fn.overflows = function() { 
    	var $this = $(this);
    	return {
    	overflow : ($this.innerHeight() !== $this.attr('scrollHeight')), 
    	scrollOffset : ($this.innerWidth() - $this.attr('scrollWidth'))
    	};
    	};
    
	$.widget("custom.catcomplete", $.ui.autocomplete, {
		_renderMenu: function( ul, items ) {
			var self = this,
				currentCategory = "";
			ul.css('width', '200px');
			$.each( items, function( index, item ) {
				self._renderItem( ul, item );
			});
		},
		_renderItem: function( ul, item ) {
			
			var elem = $( "<li></li>" )
					.data( "item.autocomplete", item )
					.addClass(item.shop_id)
					.append( "<a href='/" + item.shop_alias + "'>&raquo; " + item.title + "</a>" )
					
			if(ul.children('#' + item.shop_id).length === 0) {
				ul.append( "<li class='ui-autocomplete-category' id='" + item.shop_id + "'>" + item.shop_title + "</li>" );
				ul.append(elem);
			} else {
				ul.children('.' + item.shop_id).last().after(elem);
			}
			
			return elem;
		}
	});
	

		var searchCache = {};
		$('.HomeTeaserSearchInput, .QuickSearch .text').catcomplete({
			delay: 0,
			select: function(event, ui) {
				// redirect to homepage
				window.location = '/' + ui.item.shop_alias + '#' + ui.item.title;
			},
			open: function(){
				var widget = $(this).catcomplete('widget'),
				overflow = widget.overflows();
				if(overflow.overflow){
					widget.width(widget.width() + overflow.scrollOffset);
				}
			},
			source: function(request, response) {
				if ( request.term in searchCache ) {
					response( searchCache[ request.term ] );
					return;
				}
				
				$.ajax({
					url: "/suche/" + request.term + '/',
					dataType: "json",
					data: request,
					success: function( data ) {
						searchCache[ request.term ] = data;
						response( data );
					}
				});
			}
		});


});

