var book = 0;

function info_open()
{
    book = $(this).find('span.tip').eq(0).css('display', 'block');
}

function info_close()
{
    if (book) 
    {
        book.css('display', 'none');
    }
}

function resetField(element, value)
{

    if (element.value == value) 
    {
        element.value = "";
    }
}

function blurField(element, valueOld)
{
    if (element.value == "") 
    {
        element.value = valueOld;
    }
}


(function($)
{
    // Main function for scrollitems plugin
    $.fn.scrollitems = function(options)
    {
        var methods = 
        {
            updateActiveItem: function()
            {
                showActiveScrollitem.call(this, true);
            }
        };
        
        // Set options
        opts = $.extend({}, $.fn.scrollitems.defaults, options);
        
        // Shows the scrollitems
        // action: init, left, right, number or undefined/null
        function showScrollitems(action)
        {
            var direction = 0;
            // Check for action
            if (action && action != 'init') 
            {
                this.manualScroll = true;
                $('.scrollitem', this).clearQueue();
                
                if (isNaN(action)) 
                {
                    direction = (action == 'left' ? -1 : +1);
                    action = this.activeScrollitem + direction;
                }
                else 
                {
                    direction = this.activeScrollitem < direction ? +1 : -1;
                }
                direction = 0;
                
                this.activeScrollitem = action;
            }
            // Make sure the active item's index in the range of the array
            this.activeScrollitem = this.activeScrollitem % this.scrollitems.length;
            if (this.activeScrollitem == 0) 
            {
                this.activeScrollitem = this.scrollitems.length;
            }
            
            // Remove all scrollitems from DOM
            $(".scrollitem", this).detach();
            // Add all currently visible scollitems to DOM
            for (i = this.activeScrollitem - opts.itemsLeft - 1; i < this.activeScrollitem + opts.itemsRight + 1; i++) 
            {
                var idx = i < 0 ? this.scrollitems.length + i : (i >= this.scrollitems.length ? (i % this.scrollitems.length) : i);
                
                $('.scrollitems_items', this).append(this.scrollitems[idx]);
            }
            
            // Show current content container
            showActiveScrollitem.call(this, action != 'init');
            
            // Set horizontal position of scrollitems
            var scrollitemPos = 0;
            //var scrollCount = 0;
            $('.scrollitem', this).each(function(num)
            {
            	$(this).css("position", "absolute");
                $(this).css("left", scrollitemPos + "px");
                
                $(this).css("visibility", "visible");
                /*
                // New way: visibility based on count             
                if (scrollCount < 9) {
                	$(this).css("visibility", "visible");
                } else {
                	$(this).css("visibility", "hidden");
                }
                scrollCount++;
                */
                
                /*
                // Old way: visibility based on width 
                if ((scrollitemPos + $(this).width()) <= 910) {
                	$(this).css("visibility", "visible");
                } else {
                	$(this).css("visibility", "hidden");
                }
                */
                scrollitemPos += $(this).width();
            });
            
            // Automatic scrolling?
            if (!this.manualScroll) 
            {
                // Scroll first item and start scrolling again
                $(".scrollitem:first", this).delay(this.opts.autoScrollDuration).animate(
                {
                	left: "-="+($(".scrollitem:first", this).width())
                }, $.proxy(function()
                {
                    this.activeScrollitem++;
                    showScrollitems.call(this);
                }, this));
                // Scroll all other items
                $(".scrollitem:gt(0)", this).delay(this.opts.autoScrollDuration).animate(
                {
                	left: "-="+($(".scrollitem:first", this).width())
                });
            }
        };
        
        // Shows the active content
        function showActiveScrollitem(fade)
        {
            // Check whether this scrollitem container is visible
            if (!$(this).is(':visible')) 
            {
                return;
            }
            
            // Get the id
            var id = $(this.scrollitems[this.activeScrollitem - 1]).attr('id');
            // Fade...
            if (fade) 
            {
                $('.top-banner-box-item').clearQueue();
                $('.top-banner-box-item:visible').fadeOut(this.opts.fadeDuration);
                $('#top-banner-box-item-' + id.replace('scrollitem-', '')).delay(this.opts.fadeDuration).fadeIn(this.opts.fadeDuration, $.proxy(function()
                {
                    // Make sure the current item is really visible - and only this one
                    showActiveScrollitem.call(this);
                }, this));
            }
            // ...or just show it
            else 
                if (id) 
                {
                    $('.top-banner-box-item').hide();
                    $('#top-banner-box-item-' + id.replace('scrollitem-', '')).css('opacity', '1').show();
                }
        };
        
        // Initialize the scrollitems
        return this.each(function()
        {
            // Check for a method call
            if (typeof options == 'string') 
            {
                if (methods[options]) 
                {
                    methods[options].call(this);
                }
                return;
            }
            
            // Set options
            this.opts = opts;
            this.activeScrollitem = this.opts.activeScrollitem;
            this.manualScroll = false;
            
            // Get scrollitems from DOM
            this.scrollitems = $(".scrollitem", this);
            
            // For all scrollitems...
            this.scrollitems.each($.proxy(function(num, item)
            {
                var $this = $(item);
                // ...align vertical
                $this.css("top", (($('.scrollitems_items', this).height() - $("img", $this).height()) / 2) + "px");
            }, this));
            
            // Left arrow
            $(".scrollitems_left a").bind('click', $.proxy(function(event)
            {
                event.preventDefault();
                showScrollitems.call(this, 'left');
            }, this));
            
            // Right arrow
            $(".scrollitems_right a").bind('click', $.proxy(function(event)
            {
                event.preventDefault();
                showScrollitems.call(this, 'right');
            }, this));
            
            // Start the animation
            showScrollitems.call(this, 'init');
        });
    };
    
    // Default options
    $.fn.scrollitems.defaults = 
    {
        activeScrollitem: 8,
        itemsLeft: 7,
        itemsRight: 1,
        autoScrollDuration: 3500,
        fadeDuration: 500
    };
})(jQuery);

$(window).load(function () 
{
    //	$('.top-banner-box-item').css("filter","alpha(opacity=0)") ;
    // Init scrollitems
  
    $('.scrollitems').scrollitems()
    
    // Hide all scrollitem containers except the first
    $('.scrollitems:gt(0)').hide();
    
    
    // Observe clicks on tabs
    $('#scrollmenu a').bind('click', function(event)
    {
        event.preventDefault();
        var id = $(this).attr('id').replace('scrollitem-tab-', '');
        $('#scrollmenu li').removeClass('active');
        $(this).closest('li').addClass('active');
        $('.scrollitems:visible').hide();
        $('#scrollitems-' + id).show();
        // Show active item for newly selected tab
        $('#scrollitems-' + id).scrollitems('updateActiveItem');
    });
});

$(function()
{
    $('#main .create').click(function()
    {
        $('#main .create').addClass('activeButton');
        $('#main .createRight').removeClass('activeButton');
        $('#main .contentContactCoupon').hide();
        $('#main .sentCoupon').show();
    });
    $('#main .createRight').click(function()
    {
        $('#main .create').removeClass('activeButton');
        $('#main .createRight').addClass('activeButton');
        $('#main .contentContactCoupon').show();
        $('#main .sentCoupon').hide();
    });
});
