		
// Set the name of this window so other windows can post back here
window.name = "FWMainWindow";

// Adds images to the selection list
var dblClkTimer = null;
var CONST_DBLCLK_TIMEOUT_MS = 300;

function selectThumb(imageNumber, foxtoken) {
    var chk = "#fck" + imageNumber;
    var cell = "#imgCell" + imageNumber;
    $(chk).attr('checked', !$(chk).attr('checked'));
    if ($(chk).attr('checked')) {
	    $(cell)[0].className = 'listeObjektValgt';
    } else {
        $(cell)[0].className = 'listeObjekt';
    }
    addRemoveSelectionList($(chk).attr('checked'), foxtoken, imageNumber);
}

function selectSelectinList(foxtoken) {
    var imageNumber = $("#hack" + foxtoken).html()
    if (imageNumber != null) {
        selectThumb(imageNumber, foxtoken);
    } else {
        addRemoveSelectionList(false, foxtoken);
    }
}

function addRemoveSelectionList(add, foxtoken) {
    if (add != null) {
        var fwx = add ? "AddToSelection.fwx" : "RemoveFromSelection.fwx";
    } else {
        var fwx = "GetSelection.fwx";
    }
    $.ajax({
        type: "GET",
        url: "cmdrequest/" + fwx,
        dataType: "xml",
        data: "sendXML=1&fileId=" + foxtoken + "&dp=" + new Date,
        success: function(xml) {
            var count = 0;
            document.getElementById("np1_tabSelList").innerHTML = ""; //Raskere enn .empty() og .html("")
            $(xml).find('Item').each(function(){
                count++;
                var pvToken = $(this).attr("pvToken");
                var foxtoken = $(this).attr("foxToken");
		        var width = $(this).attr("width");
		        var height = $(this).attr("height")
                $("<div class='fwSelectedImage'><div>").html("<img width='" + width + "' height='" + height + 
                    "' border='0' alt='' src='/fotoweb/fwbin/preview.dll?D=" + pvToken + "' />").
                    appendTo('#np1_tabSelList').
                    wrap("<div onclick=\"selectSelectinList('" + foxtoken + "')\"></div>").
                    parent().prepend('<div class="selectionlist-icon" style="filter: alpha(opacity = 0);"></div>');
            });
            if (count > 0) {
                $("#np1").show();
                $("#clearSel").css("display", "inline"); // Hack
                $("#clearSel").css("visibility", "visible"); // Hack
				$('.selectionlist-icon',this).fadeTo('fast', 0);
				
                $('.fwSelectionList > div').hover(function(){
                    $('.selectionlist-icon',this).stop().fadeTo('fast',1);
                    $('.fwSelectedImage',this).stop().fadeTo('fast',0.7);
                }, function(){
                    $('.selectionlist-icon',this).stop().fadeTo('fast',0);
                    $('.fwSelectedImage',this).stop().fadeTo('fast',1);
                });
                $('#listeContainer').height($('#np1').height());
            } else {
                $("#np1").hide();
            }
            $("#selectionCnt").html(count);
        }
    });
}

// jQuery
$(function(){
	
    // Finds out which site it's on and adds an active class
    var pagename = window.location.pathname.match(/.*\/(.*)$/)[1];
    if(pagename == "Grid.fwx") {
        if($.query.get('doSearch') == "Search") {
            $('#searchbox span').addClass('active');
        } else {
            $('#main-fwx').addClass('active');
        }
    }
    /*
	if(pagename == "#") {
		$('#newestuploads-fwx').addClass('active');
	}
	if(pagename == "daybyday.fwx") {
		$('#daybyday-fwx').addClass('active');
	}
	*/
    if(pagename == "search.fwx") {
        $('#search-fwx').addClass('active');
    }
if(pagename == "tips_og_hjelp.fwx") {
    $('#tips_og_hjelp').addClass('active');
}	
    //console.log($.query.get('doSearch'));
	
	$('a.iframe').colorbox({
	    iframe:true,
	    title:false,
	    close:'lukk',
	    scrolling:true,
	    innerWidth:720,
	    innerHeight:500
	});
	
	$('.visBruksvilkaar').colorbox({
		width:600, 
		inline:true, 
		href:'#bruksvilkaar'
	});
	
	
    $('#topmenu a').blend();
	
    
    // Vertically centers the thumbnails
    $('img.thumb').each(function(i){
        var thumb = $('img.thumb').eq(i).height();
        var thumbcontain = $('img.thumb').eq(i).parent().height();
        var newtopmargin = (thumbcontain - thumb) / 2;
        $('img.thumb').eq(i).css('margin-top', newtopmargin);
    });
    
    // Adds tooltip to the icons
    $('.tooltip').tinytip();
    
    // Image count
    $('#imagecount').change(function(){
        window.location = $.queryString(window.location.href, 'SF_LASTSEARCH=1&columns=9&rows=' + $(this).val());
    });
    
    // Date range
    $('select[name="SF_FIELD3"]').change(function(){
        window.location = $.queryString(window.location.href, 'SF_LASTSEARCH=1&SF_FIELD3=' + $(':selected',this).val());
        $.cookie('dateRange', this.selectedIndex, {
            path:'/'
        });
    });
    if($.cookie('dateRange') != null) {
        $('select[name="SF_FIELD3"] :eq('+$.cookie('dateRange')+')').attr('selected','selected');
    }
    
    // Sorting click functions
    $('#ModifiedTimeAsc').click(function(){
        window.location = $.queryString(window.location.href, 'sorting=ModifiedTimeAsc');
        return false;
    });
    $('#ModifiedTimeDsc').click(function(){
        window.location = $.queryString(window.location.href, 'sorting=ModifiedTimeDsc');
        return false;
    });
        

    // Focus the search input and sets right style on focus and blur
    $('#searchInputField').focus().focus(function(){
        $(this).parent().addClass('focus');
    }).blur(function(){
        $(this).parent().removeClass('focus');
    });
});
