var ALBUM_OVER_CLASS_NAME = "ngg-album_over";
var PHOTO_OPACITY = 0.4;

/**
 * Decodes string from get method.
 */
function decodeStringFromGet( string )
{
    decodedString = string.replace( /\+/g, " " );
    
    return decodedString;
}

/**
 * Returns parameter value by its name.
 */
function GetParamValue( paramName )
{ 
    var loc = location.search.substring( 1, location.search.length );
    var param_value = false;
    
    var params = loc.split("&");
    for ( i=0; i < params.length; i++ )
    {
        param_name = params[i].substring(0,params[i].indexOf('='));
        if (param_name == paramName)
        {
            param_value = params[i].substring(params[i].indexOf('=')+1)
        }
    }    
    if ( param_value )
    {
        return decodeStringFromGet( param_value );
    }
    else
    {
        return false;
    }
}

function GeneratePhotoOverAppendText( img )
{
    var centerIconLeftOffset = img.offset().left + ( img.css( "width" ).replace( "px", "" ) - 33 ) / 2;

    var appendText = "<div class='photo_over_block' style='" + 
        "width:" + img.css( "width" ) + 
        ";height:" + img.css( "height" ) +
        ";top:" + img.offset().top + "px" +
        ";opacity:" + PHOTO_OPACITY +
        ";filter: alpha(opacity=" + PHOTO_OPACITY * 100 + ")" +
        ";'></div>";
    appendText += "<div class='photo_over_block_center_icon' style=" +
        ";top:" + img.offset().top + "px" +
        ";left:" + centerIconLeftOffset + "px" +
    "></div>";
    
    return appendText;
}

$(document).ready(function(){
   
    $( ".ngg-album" ).hover(function(){
            $(this).addClass( ALBUM_OVER_CLASS_NAME );            
        }, function(){
            $(this).removeClass( ALBUM_OVER_CLASS_NAME );
    }).click(function(){
        window.location = $(this).find("a").attr('href');
    });
    
    $( ".ngg-gallery-thumbnail a" ).hover(function(){
            var img = $(this).find( "img:first-child" );
            
            $(this).append( GeneratePhotoOverAppendText( img ) );
            $(this).find( "photo_over_block" ).css( "opacity", PHOTO_OPACITY );
        }, function(){
            $(this).find( ".photo_over_block" ).remove();
            $(this).find( ".photo_over_block_center_icon" ).remove();
    });
    
    function SubmitPaginationForm()
    {
        $( "#album" ).val( GetParamValue( "album" ) );
        $( "#gallery" ).val( GetParamValue( "gallery" ) );
        $( "#pagination_form" ).submit();
    }
    
    $( "#pagination_option" ).change( SubmitPaginationForm );
    $( "#top_pagination_option" ).change( function(){
        $( "#pagination_option" ).find( "option" ).each(function(){
            if ( $( this ).val() == $( "#top_pagination_option" ).val() )
            {
                $( this ).attr( "selected", "selected" );
            }
        });
        SubmitPaginationForm();
    });

    InitSelect( $( "#pagination_option" ), $( "#pagination_select_surface" ) );
    InitSelect( $( "#top_pagination_option" ), $( "#top_pagination_select_surface" ) );

    
    $("a[rel='show_gallery']").colorbox({
        onOpen: function(){
            $( ".basic_select" ).hide();
        },
        onClosed: function(){
            $( ".basic_select" ).show();
        }
    });

    $( "area" ).click(function(){
        if ( !$( this ).data( "link" ) )
        {
	        var tmpLink = document.createElement('a');
	        $( tmpLink ).attr( "href", $( this ).attr( "href" ) );
	        $( tmpLink ).colorbox({
		        onOpen: function(){
		            $( ".basic_select" ).hide();
		        },
		        onClosed: function(){
		            $( ".basic_select" ).show();
		        }
		    });
		    $( this ).data( "link", tmpLink );
	    }
	    $( $( this ).data( "link" ) ).click();
        
        return false;
    });
});
