window.addEvent('domready', function()
{
    MochaUI.Modal = new MochaUI.Modal();
    
    if( $('sp_more') && $('sp_hide') )
    {
        $('sp_more').addEvent( 'click', function( fn )
        {
            $('sp_block').style.height = 'auto';
        
            $('sp_more').style.display = 'none';
            $('sp_hide').style.display = 'block';
        });
        $('sp_hide').addEvent( 'click', function( fn )
        {
            $('sp_block').style.height = '181px';
        
            $('sp_more').style.display = 'block';
            $('sp_hide').style.display = 'none';
        });
    }
});

/* funkcje */
function createReview( pid )
{
    new MochaUI.Window({
			id: 'reviewWindow',
			title: 'Dodaj recenzję dla tego produktu',
			loadMethod: 'xhr',
			contentURL: '/reviews/' + pid + ',new.html',
			type: 'modal',
			width: 570,
			height: 360
  		      });
}

function deleteReview( rid )
{
    if( confirm( 'Czy napewno chcesz skasowac ta recenzje ?' ) )
    {
        new MochaUI.Window({
			id: 'reviewManagementWindow',
			title: 'Zarzadzanie recenzjami',
			loadMethod: 'xhr',
			contentURL: '/reviews/' + rid + ',delete.html',
			type: 'modal',
			width: 570,
			height: 360
  		      });
    }
}
function deleteUser( rid )
{
	if( confirm( 'Czy napewno chcesz skasowac tego użytkownika ?' ) )
    {
		x=0;
    
		if( confirm( 'Czy chcesz usun±ć wszystkie jego recenzje?' ) )
			x=1;
	    
        new MochaUI.Window({
			id: 'reviewManagementWindow',
			title: 'Zarzadzanie recenzjami',
			loadMethod: 'xhr',
			contentURL: '/accounts/delete/' + rid +'/'+x+'/',
			type: 'modal',
			width: 570,
			height: 360
  		      });
    }
}

function reviewSave( pid )
{
   //alert('ok');
   if( $('reviewsForm') )
   {
      var myHTMLRequest = new Request.HTML({
                                            url:'/reviews/' + pid + ',new.html',
                                            onSuccess: function(html, elem, code, scripts)
                                            {
                                               if( code == 'ok' )
                                               {
                                                  //MochaUI.closeWindow($('reviewWindow'));
                                                  document.location.href = document.location.href;
                                               }
                                               else
                                               if( code == 'captcha' )
                                               {
                                                  $('confirm').style.backgroundColor = '#ff0000';
                                                  $('confirm').style.color = '#ffffff';
                                                  
                                                  $('confirm').morph({backgroundColor: '#ffffff', color: '#000000'});

                                                  $('captcha').src = '/captcha/' + Math.ceil( Math.random() * 100 );
                                               }
                                               //else
                                               //    MochaUI.closeWindow($('ratesWindow'));

		                            }
                                           }).post($('reviewsForm'));
   }
   else
   {
      //alert('!reviewsForm');
   }
}

function rateObject( pid )
{
    new MochaUI.Window({
			id: 'ratesWindow',
			title: 'Oceń ten produkt',
			loadMethod: 'xhr',
			contentURL: '/rates/' + pid + '.html',
			type: 'modal',
			width: 570,
			height: 200
  		      });
}

function rateSave( pid )
{
   //alert('ok');
   if( $('ratesForm') )
   {
      var myHTMLRequest = new Request.HTML({
                                            url:"/rates/" + pid + ".html",
                                            onSuccess: function(html, elem, code, scripts)
                                            {
                                               //alert(code);

                                               if( code == 'ok' )
                                               {
                                                  //MochaUI.closeWindow($('ratesWindow'));
                                                  document.location.href = document.location.href;
                                               }
                                               else
                                               if( code == 'captcha' )
                                               {
                                                  $('confirm').style.backgroundColor = '#ff0000';
                                                  $('confirm').style.color = '#ffffff';
                                                  
                                                  $('confirm').morph({backgroundColor: '#ffffff', color: '#000000'});

                                                  $('captcha').src = '/captcha/' + Math.ceil( Math.random() * 100 );
                                               }
                                               //else
                                               //    MochaUI.closeWindow($('ratesWindow'));

		                            }
                                           }).post($('ratesForm'));
   }
   else
   {
       //alert('!ratesForm');
   }
}

function initRates()
{
         $$('div.slider.advanced').each(function(el, i)
         {
		var slider = new Slider(el, el.getElement('.knob'), 
                {
			steps: 100,  // Steps from 0 to 255
			wheel: true, // Using the mousewheel is possible too
			onChange: function()
                        {
				$('points').value = this.step;

                                // Based on the Slider values set an RGB value in the color array
				//color[i] = this.step;
				// and update the output to the new value
				//updateColor();
			}
		}).set(50);
	});
}

function setSort( sortType )
{
    if( $('sortForm') )
    {
        $('setSort').value = sortType;
        $('sortForm').submit();
    }
}

function searchAutor( author )
{
    if( $('searchKeyword') )
    {
        $('searchKeyword').value = author;
        $('searchForm').submit();
    }
}
