var cishop =
{
    connectZIPCity: function( inputDef, outputDef )
    {
        var inputField = $(inputDef);
        var outputElement = $(outputDef);
        var svalbardZips = [ '9170', '9171', '9172', '9173', '9174', '9175', '9176', '9177', '9178', '8099' ];
        
        inputField.keyup( function()
        {
            if ( inputField.val().length == 4 )
            {
                $.getJSON( 'http://fraktguide.bring.no/fraktguide/postalCode.json?pnr='+ inputField.val() +'&callback=?', function( data )
                {
                    if ( data.result != 'Ugyldig postnummer' )
                    {
                        outputElement.val( data.result );
                    }
                });
                if ( svalbardZips.indexOf( inputField.val() ) != -1 )
                {
                    $('select.ezcca-user_country option[value="SJ"]').attr('selected', 'selected');
                }
            }
        } );
    },
    
    tab: function( arg )
    {
        var matches = /#(.*)$/.exec( arg.href );
        if ( !matches )
        {
            return true;
        }

        var id = matches[1];
        var link = $('#tabs ul.tab-menu a[href="#' + id + '"]');
        if ( link && link.length )
        {
            $('#tabs ul.tab-menu a').removeClass( 'selected' );
            link.addClass( 'selected' );
            $('#tabs > div.tab-content').hide();
            $('#tabs div#' + id).show();
        }

        return false;
    },  

    init: function()
    {      
        $('a.fancybox').fancybox({
            'titlePosition': 'inside',
            'titleFormat': function( title, currentArray, currentIndex, currentOpts ) { return '<span>Bilde ' + ( currentIndex + 1 ) + ' av ' + currentArray.length + ' ' + title + '</span>'; }
        });
        
        if ( location.hash )
        {
            cishop.tab( { href: location.hash } );
        }        
    }
}

$(document).ready( cishop.init );

