jQuery.noConflict();

function InputHelperIn ( obj, text )
{
    //если при фокусе значение поля равно значению подсказки, то чистим его и вешаем стили
    if ( obj.value == text )
    {
        jQuery( obj )
            .css ( { color: '#000', fontStyle: 'normal' } )
            .val ( '' );
    }
}

function InputHelperOut ( obj, text )
{
    //если при потере фокуса значение поля равно пустоте или значению по умолчанию,
        //то пихаем в него текст подсказки и вешаем стили подсказки
    if ( obj.value == '' || obj.value == text )
    {
        jQuery( obj )
            .css ( { color: '#b3b3b3', fontStyle: 'italic' } )
            .val ( text );
    }
}

function InputHelperCreate ( obj, text )
{
    //вешаем на поле эвенты. На фокус и потерю фокуса.
    jQuery( obj )
        .bind ( 'focus', function () {
            InputHelperIn ( this, text );
        } )
        .bind ( 'blur', function () {
            InputHelperOut ( this, text );
        } );

    //первоначальный инит
    InputHelperOut ( obj, text );
}

jQuery(document).ready(function(){
    
    jQuery("input.searchNoteBooks").autocomplete({
        source: function( request, response ) {
            jQuery.ajax({
                url: "/ajax/searchNoteBooks.php",
                dataType: "json",
                data: {
                    query: request.term
                },
                success: function( data ) {
                    response( jQuery.map( data, function( item ) {
                        return {
                                id: item.id,
                                request: request.term,
                                label: item.value,
                                value: item.id && item.id != "notfound" ? item.value : request.term
                        }
                    }));
                }
            });
        },
        minLength: 2,
        select: function( event, ui ) {            
            if(ui.item.id == "notfound")
            {
                return;    
            }
            else if(ui.item.id == 0)
            {
                window.location = '/search/?searchstring=' + ui.item.request;    
            }
            else
            {
                window.location = '/product/' + ui.item.id + '/';
            }
        }
    });
    
    jQuery('input.searchNoteBooks').each(function() {
        InputHelperCreate(this, this.title);
    });
    
    jQuery('#sendOrder').click(function(){
        
        var qString =  jQuery('form#order').serialize();
        
        jQuery('#notification').hide();
   
        jQuery.post('/ajax/sendOrder.php', qString, function(data){
            var response = jQuery.parseJSON(data);
            
            if(response.error)
            {
                jQuery('#responseMessage').html(response.error);    
            }
            else
            {
                jQuery('#responseMessage').html(response.data);
                
                jQuery('form#order input').val('');    
                jQuery('form#order textarea').val('');   
            }
            
            jQuery('#notification').show();
        });
        
        return false;
    });
    
    jQuery('table.sTable2 tr:even').attr("class", "even");
	
	
	/**
	 * Message Notify Drop Down
	**/
	jQuery('.messagenotify .wrap, .alertnotify .wrap').click(function(){
		var t = jQuery(this).parent();
		var url = t.attr('href');
		if(t.hasClass('showmsg')) {
			t.removeClass('showmsg');
			t.find('.thicon').removeClass('thiconhover');
			t.parent().find('.dropbox').remove();
			
		} else {
			
			jQuery('.topheader li').each(function(){
				jQuery(this).find('.showmsg').removeClass('showmsg');
				jQuery(this).find('.thicon').removeClass('thiconhover');
				jQuery(this).find('.dropbox').remove();
			});
			
			t.addClass('showmsg');
			t.find('.thicon').addClass('thiconhover');
			t.parent().append('<div class="dropbox"></div>');
						
			jQuery.post(url,function(data){
				jQuery('.dropbox').append(data);
			});
		}
		return false;
		
	});
	
	
	/**
	 * Sidebar accordion
	**/
	jQuery('#accordion h3').click(function() {
		if(jQuery(this).hasClass('open')) {
			jQuery(this).removeClass('open');
			jQuery(this).next().slideUp('fast');
		} else {
			jQuery(this).addClass('open');
			jQuery(this).next().slideDown('fast');
		} return false;
	});		
	
	/**
	 * Widget Box Toggle
	**/
    /*
	jQuery('.widgetbox h3, .widgetbox2 h3').hover(function(){
		jQuery(this).addClass('arrow');
		return false;
	},function(){
		jQuery(this).removeClass('arrow');
		return false;
	});
    */ 
	
    /*
	jQuery('.widgetbox h3, .widgetbox2 h3').toggle(function(){
		jQuery(this).next().slideUp('fast');
		jQuery(this).css({MozBorderRadius: '3px', 
						  WebkitBorderRadius: '3px',
						  borderRadius: '3px'});
		return false;
	},function(){
		jQuery(this).next().slideDown('fast');
		jQuery(this).css({MozBorderRadius: '3px 3px 0 0', 
						  WebkitBorderRadius: '3px 3px 0 0',
						  borderRadius: '3px 3px 0 0'});
		return false;
	});
    */
	
	
	/**
	 * Notification
	**/
	jQuery('.notification .close').click(function(){
		jQuery(this).parent().fadeOut();
	});	
	
	
	/** Make footer always at the bottom**/
	if(jQuery('body').height() > jQuery(window).height()) {
		jQuery('.footer').removeClass('footer_float');
	}	
});
